CCCPaste

"Alarm Activation" rev_01

/********* 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: "Alarm Activation"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-07-21 02:26:35

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Create a security system utilizing EasyButton and */
	/* Ultrasonic libraries. The system should monitor */
	/* distance with an HC-SR04 sensor and activate an */
	/* alarm when either button not  connected to pin D2 */
	/* or or HC-SR04 sensor sense human pressence. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   //https://github.com/evert-arias/EasyButton
#include   //https://github.com/ErickSimoes/Ultrasonic

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t PushButton1_PIN_D2 = 2;
const uint8_t HCSR04_Echo_PIN_D4 = 4;
const uint8_t PushButton2_PIN_D5 = 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t HCSR04_Trigger_PIN_D3 = 3;
const uint8_t Alarm_PIN_D6 = 6;  // Pin for the alarm

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool HCSR04_Trigger_PIN_D3_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float HCSR04_Trigger_PIN_D3_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button1(PushButton1_PIN_D2);  // Initialize EasyButton for button 1
EasyButton button2(PushButton2_PIN_D5);  // Initialize EasyButton for button 2

Ultrasonic ultrasonic(HCSR04_Trigger_PIN_D3, HCSR04_Echo_PIN_D4); // Initialize Ultrasonic sensor

void onButton1Pressed() {
  Serial.println("Button1 pressed");
  activateAlarm();
}

void onButton2Pressed() {
  Serial.println("Button2 pressed");
}

void setup(void) {
  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.println();
  Serial.println(">>> Security System Example <<<");

  pinMode(PushButton1_PIN_D2, INPUT_PULLUP);
  pinMode(HCSR04_Echo_PIN_D4, INPUT);
  pinMode(PushButton2_PIN_D5, INPUT_PULLUP);
  pinMode(HCSR04_Trigger_PIN_D3, OUTPUT);
  pinMode(Alarm_PIN_D6, OUTPUT);  // Set alarm pin as output

  button1.begin();
  button2.begin();
  button1.onPressed(onButton1Pressed);
  button2.onPressed(onButton2Pressed);
}

void loop(void) {
  // put your main code here, to run repeatedly:
  button1.read();  // Read the state of button 1
  button2.read();  // Read the state of button 2

  updateOutputs();  // Refresh output data

  int distance = ultrasonic.read();  // Read distance from ultrasonic sensor
  Serial.print("Distance in CM: ");
  Serial.println(distance);

  if (distance < 50) {  // If distance is less than 50 cm, activate alarm
    activateAlarm();
  }

  delay(1000);  // Delay for 1 second
}

void updateOutputs() {
  digitalWrite(HCSR04_Trigger_PIN_D3, HCSR04_Trigger_PIN_D3_rawData);
}

void activateAlarm() {
  digitalWrite(Alarm_PIN_D6, HIGH);  // Activate alarm
  delay(5000);  // Keep alarm on for 5 seconds
  digitalWrite(Alarm_PIN_D6, LOW);  // Deactivate alarm
}

/* END CODE */