turn off two different alarms with one pushbutton

Thread Starter

bianca2345

Joined Jun 7, 2021
2
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? here is my code
Code:
#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);
}
 

trebla

Joined Jun 29, 2019
542
If you reset this variable just before checking value of the same variable it will be give zero. Try reset this variable after all checkings are done.
 
Top