Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

turn off two LEDs for 2 different alarms

Status
Not open for further replies.

bianca152036

New Member
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);
}
 
Code:
    pushpressed = 0;
    if (pushpressed == 0) {
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      }
      digitalWrite(ledPin2, ledState2);
    }
    else if (pushpressed == 1) {
      ledState2 = LOW;
      digitalWrite(ledPin2, ledState2);
    }

The above code never executes pushpressed == 1 because of the first line of code
in this snippet. I think just eliminate that 1'st line of code in the above snippet.

As an aside when posting code if you can post it with line numbers turned on makes
commenting on it a tad easier.

Regards, Dana.
 
The second section has this line just before the part that controls the LED

pushpressed = 0;

That means the button press can never be recognised in the last part of the code.

Edit - Snap! - Was one the phone & never got a popup that another answer had been posted..
 
Code:
    pushpressed = 0;
    if (pushpressed == 0) {
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      }
      digitalWrite(ledPin2, ledState2);
    }
    else if (pushpressed == 1) {
      ledState2 = LOW;
      digitalWrite(ledPin2, ledState2);
    }

The above code never executes pushpressed == 1 because of the first line of code
in this snippet. I think just eliminate that 1'st line of code in the above snippet.

As an aside when posting code if you can post it with line numbers turned on makes
commenting on it a tad easier.

Regards, Dana.


I deleted that line, but I still have a problem and I can not solve it. So, when the time hits the time set for the second alarm, the LED turn on, I press the button, it turn off, and then it continue to turn on, and I want to turn it off defintively. How can I do this? A thing which I don t understand is that why for the first alarm works, and for the second don t
 
The second section has this line just before the part that controls the LED



That means the button press can never be recognised in the last part of the code.

Edit - Snap! - Was one the phone & never got a popup that another answer had been posted..
I removed the line, but the LED won t turn off
 
I think you need to separate the time comparison from the LED control.

As it is, each section will continually turn the LED on for the minute the time comparison is valid.
But, once that is no longer valid, the button check never gets executed.

Try something like for each alarm (this is just in semi pseudocode, rather than duplicate every line)

Code:
// Time comparison

If (time == alarm_1_time) {
    alarm_1_triggered = true;
}
else {

    if (alarm_1_acknowledged == true) {
        alarm_1_triggered = false;
        alarm_1_acknowledged = false;
}

// Button & LED
if(alarm_1_triggered == true  && alarm_1_acknowledged == false) {
    LED ON;

    if(button_pressed) {
        alarm_1_acknowledged = true;
        LED OFF;
    }
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top