Use a pushbutton to turn off 2 LEDs

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;
int m1 = 51;
int h2 = 15;
int m2 = 52 ;

int stop_buton = 9;
int state_stop_buton = 0;

int ledPin = 6;
int ledState = LOW;
int pushpressed = 0;

int ledPin2 = 7;
int ledState2 = LOW;



void setup()
{

  Wire.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  rtc.adjust(DateTime(2021, 03, 27, 15, 50, 50));
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //  rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
  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();
  first_alarm();
  stopPins();
  second_alarm();
 
state_stop_buton = digitalRead(stop_buton);
}



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);

}



void stopPins() {
  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();
  }
}

void startBuzz() {


  if (pushpressed == 0) {
    
    if (ledState == LOW) {
      ledState = HIGH;
    }
    digitalWrite(ledPin, ledState);
  }


  else if (pushpressed == 1) {

    ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}



void first_alarm() {
  DateTime t = rtc.now();
  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");
    startBuzz();
    delay(5000);

  }
}

void second_alarm() {
  DateTime t = rtc.now();
  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");
    startBuzz2();
    delay(5000);

  }
}

void startBuzz2() {
  if (pushpressed == 0) {
    if (ledState2 == LOW) {
      ledState2 = HIGH;
    }
    digitalWrite(ledPin2, ledState2);
  }
  else if (pushpressed == 1) {
    ledState2 = LOW;
    digitalWrite(ledPin2, ledState2);
  }
}


Shouldn't 'pushpressed' get set back to 0 at some point?

Steve

before voidBuzz end, I wrote "pushpressed == 0" but it is still not working

==     means compare
=      means assign

Hello
Where do you read the states of the buttons?

thank you! I replaces "==" with "=" but it is still not working

hello! I have a single stop button for the 2 LEDs. In void loop i wrote "state_stop_buton = digitalRead(stop_buton);"

Always.

Show us your current sketch.

I edited it now and put the sketch

Please do not edit previous posts except for minor things like spelling and punctuation.

    if (ledState2 == LOW) {
      ledState2 = HIGH;
    }

Did you mean:

    ledState2 = !ledState2;

@Community: I guess we (the users that have experience with the forum) should explicitly write

"post your modified and actual sketch in a new posting
keep the former posted code as it is and don't edit former postings except for typos.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

image

1 Like

thank you Stefan for your advices! I will pay attention in the future

well one way of thinking is: "I don't want to bloat the thread by repeated posting of slighly modified code. Which makes sense in a certain way.
But leaves out that a repeatedly modified code at the start of a thread makes responds look like nonsense.

best regards Stefan