Turn off LED with push button

Hello!
I have little experience with Arduino and I am trying to solve a problem. I am making an alarm using DS3231 and LCD1602 i2c, 2 LEDs and a pushbutton. For the first alarm the first LED is turning on, and when the pushbutton is pressed, the LED turn off. But for the second alarm, the second LED turn on, but if I press again the pushbutton, it won’t turn off. How can I turn off the second LED?

#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int nowHr, nowMin, nowSec;
RTC_DS3231 rtc;
int h1 = 15;   //hour and minute for the first alarm
int m1 = 51;
int h2 = 15;  //hour and minute for the second alarm
int m2 = 52 ;
int stop_buton = 9; //the button used to stop the alarm
int state_stop_buton = 0;  //state of the stp button
int ledPin = 6;          //first led used for the first alarm
int ledState = LOW;      //state for the first led
int pushpressed = 0;    //variable used for stop button
int ledPin2 = 7;  //second led used for the second alarm
int ledState2 = LOW;  //state for the second led

void setup()
{
  Wire.begin();
  rtc.adjust(DateTime(2021, 03, 27, 15, 50, 50));
  lcd.backlight();
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Welcome To this");
  lcd.setCursor(0, 1);
  lcd.print("new device");
  pinMode(stop_buton, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  delay(2000);
  Serial.begin(9600);
}
void loop() {
  timeScreen();
  state_stop_buton = digitalRead(stop_buton);
  DateTime t = rtc.now();
  //  first alarm
  if (int(t.hour()) == h1 && int(t.minute()) == m1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("first ");
    lcd.setCursor(0, 1);
    lcd.print("text");
    delay(5000);
    if (state_stop_buton == 1) {
      pushpressed = 1;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("you saw  ");
      lcd.setCursor(0, 1);
      lcd.print("the text");
      delay(1200);
      lcd.clear();
    }
    if (pushpressed == 0) {
      if (ledState == LOW) {
        ledState = HIGH;
      }
      digitalWrite(ledPin, ledState);
    }
    else if (pushpressed == 1) {
      ledState = LOW;
      digitalWrite(ledPin, ledState);
    }
  }
  //second alarm
  if (int(t.hour()) == h2 && int(t.minute()) == m2) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("second ");
    lcd.setCursor(0, 1);
    lcd.print("text");
    delay(5000);
    if (state_stop_buton == 1) {
      pushpressed = 1;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("you saw  ");
      lcd.setCursor(0, 1);
      lcd.print("the text");
      delay(1200);
      lcd.clear();
    }
    pushpressed = 0;
    if (pushpressed == 0) {
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      }
      digitalWrite(ledPin2, ledState2);
    }
    else if (pushpressed == 1) {
      ledState2 = LOW;
      digitalWrite(ledPin2, ledState2);
    }
  }
}
void timeScreen() {
  DateTime now = rtc.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Time:");
  lcd.setCursor(6, 0);
  lcd.print(nowHr = now.hour(), DEC);
  lcd.print(":");
  lcd.print(nowMin = now.minute(), DEC);
  lcd.print(":");
  lcd.print(nowSec = now.second(), DEC);
  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(now.day(), DEC);
  lcd.print("/");
  lcd.print(now.month(), DEC);
  lcd.print("/");
  lcd.print(now.year(), DEC);
  delay(500);
}
3 Likes

i think this line where led state is possible problem

int ledState = LOW;      //state for the first led

:: note that i defined the state as "1" =low..as the modded line below..
notice i added  "1" to the line..

like you have 2=low..
int ledState2 = LOW;  //state for the second led


think it should be  ...
int ledState1 = LOW;      //state for the first led


and look over the code this may be needed to be  ckecked for led state"1" 
 any where else in the program
4 Likes

Your code is a bit odd logically, but I believe line 87 is your issue:

      ...
87    pushpressed = 0;
88    if (pushpressed == 0) {
      ...
94    else if (pushpressed == 1) {
      ...

I see you’ve used a lot of delays, and the way your code is at the moment it’ll be necessary for the user to hold the button down until they see a response. I’d recommend doing some research on switch debouncing and interrupts - implementing these two things will greatly improve your project.

And chapter 5.4:

3 Likes

if I delete line 87 the second led for the second alarm will not turn on

2 Likes

If alarm1 has gone off and you pressed the button then pushPressed will be 1 and the LED for alarm 2 will not turn on. You need to reset pushPressed after the processing for alarm1 is complete. However I believe you will need extra code to prevent alarm1 from retriggering before the minute has expired. You could try resetting pushPress if the check for a match with the alarm1 time fails.

2 Likes

and how can I reset pushPressed? I thought that if I write “pushpressed=0” (line 87) will work, but I still can not turn off the second led

1 Like

I find writing the program down as a flow chart helps :slight_smile:

Or try explaining your program to someone - even if it’s a rubber duck!

2 Likes

That statement would reset pushPressed, but it has to be done so that the processing of alarm2 is not affected. AFAICT the point of pushPressed is to avoid turning the LED back on if alarm1 has already gone off and the button has already been pressed once. Putting it where you now have it also ensures that alarm2 is not turned on. Instead, it needs to be reset after the alarm can no longer be triggered - after the minute has expired. You could add an else to the alarm1 time test so that if alarm 1 is not triggered then pushPressed is reset. Then repeat for alarm2 (setting pushPressed when alarm2 is triggered will have the same affect on alarm1 the next time it is triggered, so it also needs to be reset after alarm2 can no longer be triggered).

This sort of sequential event processing is much better handled using a state machine. It takes a bit of working out to get your head around, but the coding works out much simpler than the series of if statements.

5 Likes

thank you for your advice, i hope i can do it!

4 Likes