/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Arduino Alarm"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-01-03 08:28:08

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Turn on buzzer when distance sensor detects an */
	/* object nearby */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>
#include <Ultrasonic.h>
#include <ezBuzzer.h>

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
  /* Turn on buzzer when distance sensor detects an */
  /* object nearby */
/****** END SYSTEM REQUIREMENTS *****/

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t distance_sensor_HC_SR04_Echo_PIN_D3 = 3;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t distance_sensor_HC_SR04_Trigger_PIN_D2 = 2;
const uint8_t buzzer_PassiveBuzzer_Signal_PIN_D4 = 4;

/****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
Ultrasonic ultrasonic(distance_sensor_HC_SR04_Trigger_PIN_D2, distance_sensor_HC_SR04_Echo_PIN_D3);
ezBuzzer buzzer(buzzer_PassiveBuzzer_Signal_PIN_D4);

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(distance_sensor_HC_SR04_Echo_PIN_D3, INPUT);
  pinMode(distance_sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  pinMode(buzzer_PassiveBuzzer_Signal_PIN_D4, OUTPUT);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  
  // Read distance from ultrasonic sensor
  unsigned int distance = ultrasonic.read();

  // Check if object is nearby
  if (distance < 20) { // Adjust the threshold distance as per your requirement
    // Turn on the buzzer
    buzzer.beep(200); // Adjust the beep time duration as per your requirement
  }

  // Add any other code for your system requirements here

  delay(100); // Adjust the delay time as per your requirement
}
