Hello everyone,
I´m building a home automation project and it is working fine up to the point where I start an alarm using (buttonA). The LCD doesn´t change the text to read ("Manual Alarm"). Everything else works, so I know the wiring has no issues.
This is how it should work:
1 - When there are no alarms, the LCD reads "System Normal" (this works).
2 - When there is an alarm, the LCD should read ("Manual Alarm") (this is not working).
I have included my code in case it helps. Any help will be greatly appreciated!
#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(22, 23, 24, 25, 26, 27);
Servo myservo;
int buttonApin = 8; //This button will initialize the sequence for testing purpose
int buttonBpin = 9; // This button will stop the sequence for testing purpose
int NAC1 = 2; //the pin that will drive NAC 1
int NAC2 = 3; //the pin that will drive NAC 2
int NAC3 = 4;
int NAC4 = 5;
int NAC5 = 6;
int ALARM = 10;
void setup()
{
myservo.attach(11);
myservo.write(90);// move servos to center position -> 90°
pinMode(NAC1, OUTPUT); //initialize the pin as an output
pinMode(buttonApin, INPUT_PULLUP); //initialize pin as input for testing
pinMode(buttonBpin, INPUT_PULLUP); //initialize pin as input for testing
pinMode(NAC2, OUTPUT); //initialize the pin as an output
pinMode(NAC3, OUTPUT);
pinMode(NAC4, OUTPUT);
pinMode(NAC5, OUTPUT);
pinMode (ALARM, OUTPUT);
lcd.begin(16, 2);
}
void loop()
{
digitalWrite (ALARM, LOW);
if (digitalRead (ALARM) == LOW)
{
lcd.setCursor(0, 0);
lcd.print("SYSTEM NORMAL");
}
else
{
lcd.setCursor(0, 0);
lcd.print("MANUAL ALARM");
}
//This starts the Elevator Functionality with breaks so it checks for any alarm condition every second
//-----------------------------------------------------------------------------------------------------
while ((digitalRead (buttonApin) != LOW)) // This keeps the loop running until there is an alarm
{
myservo.write(90);// move servos to center position -> 90°
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
myservo.write(30);// move servos to center position -> 60°
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
myservo.write(90);// move servos to center position -> 90°
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
myservo.write(150);// move servos to center position -> 120°
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
delay(1000);
if (digitalRead (buttonApin) == LOW) {
break;
}
}
//This ends the Elevator Functionality
//-----------------------------------------------------------------------------------------------------
if (digitalRead (buttonApin) == LOW) // This condition starts an alarm
{
digitalWrite(ALARM, HIGH);
}
if (digitalRead (ALARM) == HIGH)
{
lcd.setCursor(0, 0);
lcd.print("MANUAL ALARM");
myservo.write(90); // brings the elevator back to 1st floor when there is an alarm
while ((digitalRead (buttonBpin) != LOW)) // This keeps the loop running until Button B is pressed
{
digitalWrite(NAC1, HIGH);
digitalWrite(NAC2, HIGH);
digitalWrite(NAC3, HIGH);
digitalWrite(NAC4, HIGH);
digitalWrite(NAC5, HIGH);
delay(200); //wait for 20ms
digitalWrite(NAC1, LOW);
digitalWrite(NAC2, LOW);
digitalWrite(NAC3, LOW);
digitalWrite(NAC4, LOW);
digitalWrite(NAC5, LOW);
delay(400); //wait for 40ms
}
}
if (digitalRead (buttonBpin) == LOW) // If Button B is pressed
{
digitalWrite(NAC1, LOW); // Turn OFF NACs
digitalWrite(NAC2, LOW);
digitalWrite(NAC3, LOW);
digitalWrite(NAC4, LOW);
digitalWrite(NAC5, LOW);
digitalWrite(ALARM, LOW);
}
}