Skip to main content
Tweeted twitter.com/StackArduino/status/1395167409095462912

Turning off a ledLED

I am trying to make 2 alarms for 2 different times using 2 ledsLED's which should turn on when the time corresponds to the set time and then if a pushbutton is pressed, the ledLED should turn off. For the first alarm, the first ledLED can be turn off with the pushbutton, instead for the second alarm, the second ledLED is turning on, but if the pushbutton is pressed is will not turn off, even though is the same code. What can I do to turn off the second ledLED?

Turning off a led

I am trying to make 2 alarms for 2 different times using 2 leds which should turn on when the time corresponds to the set time and then if a pushbutton is pressed, the led should turn off. For the first alarm, the first led can be turn off with the pushbutton, instead for the second alarm, the second led is turning on, but if the pushbutton is pressed is will not turn off, even though is the same code. What can I do to turn off the second led?

Turning off a LED

I am trying to make 2 alarms for 2 different times using 2 LED's which should turn on when the time corresponds to the set time and then if a pushbutton is pressed, the LED should turn off. For the first alarm, the first LED can be turn off with the pushbutton, instead for the second alarm, the second LED is turning on, but if the pushbutton is pressed is will not turn off, even though is the same code. What can I do to turn off the second LED?

Became Hot Network Question
added 511 characters in body
Source Link
Biancaaa
  • 61
  • 1
  • 5
#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 pin_buton1 = 8; int stop_buton =9; int state_stop_buton = 0;

int led1_pin = 6; int state_led = LOW; int pushpressed = 0;

int led2_pin = 7; int state_led2 = 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 Our");
lcd.setCursor(0, 1); lcd.print("device");

pinMode(pin_buton1, INPUT); pinMode(stop_buton, INPUT); pinMode(led1_pin, OUTPUT); pinMode(led2_pin, OUTPUT); delay(2000); Serial.begin(9600); }

void loop() { timeScreen(); first_alarm(); stopPins(); second_alarm(); stopPins2();

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() { //function to stop buzzing when user pushes stop push button if (state_stop_buton == 1) {

#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 pin_buton1 = 8;
int stop_buton =9;
int state_stop_buton = 0;

int led1_pin = 6;
int state_led = LOW;
int pushpressed = 0;

int led2_pin = 7;
int state_led2 = LOW;


void setup()
{

  Wire.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));           
  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 Our");                                     
  lcd.setCursor(0, 1);
  lcd.print("device");



  pinMode(pin_buton1, INPUT);
  pinMode(stop_buton, INPUT);
  pinMode(led1_pin, OUTPUT);
  pinMode(led2_pin, OUTPUT);
  delay(2000);
  Serial.begin(9600);
}




void loop() {
  timeScreen();
  first_alarm();
  stopPins();
  second_alarm();
  stopPins2();

  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("first message ");
    lcd.setCursor(0, 1);
    lcd.print("was read");
    delay(1200);
    lcd.clear();

} }

void startBuzz() {

if (pushpressed == 0) {

  }
}

void startBuzz() {                    

  
  if (pushpressed == 0) {
   
    if (state_led == LOW) {                 
      state_led = HIGH;
      digitalWrite(led1_pin, state_led);
     
    }
  }

} else if (pushpressed == 1) {

  else if (pushpressed == 1) {

   
    state_led = LOW;
    digitalWrite(led1_pin, state_led);

} }

void first_alarm() { DateTime t = rtc.now(); if (int(t.hour()) == h1 && int(t.minute()) == m1) {

  }
}



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 stopPins2() {
if (state_stop_buton == 1) {


  }
}



//

void stopPins2() {                   
  if (state_stop_buton == 1) {

    pushpressed = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("second message ");
    lcd.setCursor(0, 1);
    lcd.print("was read ");
    delay(1200);
    lcd.clear();

} }

void startBuzz2() {

if (pushpressed == 1) {

  }
}

void startBuzz2() {                   

  
  if (pushpressed == 1) {
   
 
    if (state_led2 == LOW) {                 
      state_led2 = HIGH;
      digitalWrite(led2_pin, state_led2);
  
    }

} else if (pushpressed == 0 ) { state_led2 = LOW; digitalWrite(led2_pin, state_led2); } }

void second_alarm() { DateTime t = rtc.now(); if (int(t.hour()) == h2 && int(t.minute()) == m2 ) {

  }
  else  if (pushpressed == 0 ) {
    state_led2 = LOW;
   digitalWrite(led2_pin, state_led2);
 }
}



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

  }
}

} }

#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 pin_buton1 = 8; int stop_buton =9; int state_stop_buton = 0;

int led1_pin = 6; int state_led = LOW; int pushpressed = 0;

int led2_pin = 7; int state_led2 = 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 Our");
lcd.setCursor(0, 1); lcd.print("device");

pinMode(pin_buton1, INPUT); pinMode(stop_buton, INPUT); pinMode(led1_pin, OUTPUT); pinMode(led2_pin, OUTPUT); delay(2000); Serial.begin(9600); }

void loop() { timeScreen(); first_alarm(); stopPins(); second_alarm(); stopPins2();

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() { //function to stop buzzing when user pushes stop push button if (state_stop_buton == 1) {

pushpressed =1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("first message ");
lcd.setCursor(0, 1);
lcd.print("was read");
delay(1200);
lcd.clear();

} }

void startBuzz() {

if (pushpressed == 0) {

if (state_led == LOW) {                 
  state_led = HIGH;
  digitalWrite(led1_pin, state_led);
 
}

} else if (pushpressed == 1) {

state_led = LOW;
digitalWrite(led1_pin, state_led);

} }

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 stopPins2() {
if (state_stop_buton == 1) {

pushpressed = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("second message ");
lcd.setCursor(0, 1);
lcd.print("was read ");
delay(1200);
lcd.clear();

} }

void startBuzz2() {

if (pushpressed == 1) {

if (state_led2 == LOW) {                 
  state_led2 = HIGH;
  digitalWrite(led2_pin, state_led2);

}

} else if (pushpressed == 0 ) { state_led2 = LOW; digitalWrite(led2_pin, state_led2); } }

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

} }

#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 pin_buton1 = 8;
int stop_buton =9;
int state_stop_buton = 0;

int led1_pin = 6;
int state_led = LOW;
int pushpressed = 0;

int led2_pin = 7;
int state_led2 = LOW;


void setup()
{

  Wire.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));           
  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 Our");                                     
  lcd.setCursor(0, 1);
  lcd.print("device");



  pinMode(pin_buton1, INPUT);
  pinMode(stop_buton, INPUT);
  pinMode(led1_pin, OUTPUT);
  pinMode(led2_pin, OUTPUT);
  delay(2000);
  Serial.begin(9600);
}




void loop() {
  timeScreen();
  first_alarm();
  stopPins();
  second_alarm();
  stopPins2();

  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("first message ");
    lcd.setCursor(0, 1);
    lcd.print("was read");
    delay(1200);
    lcd.clear();
  }
}

void startBuzz() {                    

  
  if (pushpressed == 0) {
   
    if (state_led == LOW) {                 
      state_led = HIGH;
      digitalWrite(led1_pin, state_led);
     
    }
  }
  else if (pushpressed == 1) {

   
    state_led = LOW;
    digitalWrite(led1_pin, state_led);
  }
}



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 stopPins2() {                   
  if (state_stop_buton == 1) {

    pushpressed = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("second message ");
    lcd.setCursor(0, 1);
    lcd.print("was read ");
    delay(1200);
    lcd.clear();
  }
}

void startBuzz2() {                   

  
  if (pushpressed == 1) {
   
 
    if (state_led2 == LOW) {                 
      state_led2 = HIGH;
      digitalWrite(led2_pin, state_led2);
  
    }
  }
  else  if (pushpressed == 0 ) {
    state_led2 = LOW;
   digitalWrite(led2_pin, state_led2);
 }
}



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

  }
}
Source Link
Biancaaa
  • 61
  • 1
  • 5

Turning off a led

I am trying to make 2 alarms for 2 different times using 2 leds which should turn on when the time corresponds to the set time and then if a pushbutton is pressed, the led should turn off. For the first alarm, the first led can be turn off with the pushbutton, instead for the second alarm, the second led is turning on, but if the pushbutton is pressed is will not turn off, even though is the same code. What can I do to 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 pin_buton1 = 8; int stop_buton =9; int state_stop_buton = 0;

int led1_pin = 6; int state_led = LOW; int pushpressed = 0;

int led2_pin = 7; int state_led2 = 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 Our");
lcd.setCursor(0, 1); lcd.print("device");

pinMode(pin_buton1, INPUT); pinMode(stop_buton, INPUT); pinMode(led1_pin, OUTPUT); pinMode(led2_pin, OUTPUT); delay(2000); Serial.begin(9600); }

void loop() { timeScreen(); first_alarm(); stopPins(); second_alarm(); stopPins2();

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() { //function to stop buzzing when user pushes stop push button if (state_stop_buton == 1) {

pushpressed =1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("first message ");
lcd.setCursor(0, 1);
lcd.print("was read");
delay(1200);
lcd.clear();

} }

void startBuzz() {

if (pushpressed == 0) {

if (state_led == LOW) {                 
  state_led = HIGH;
  digitalWrite(led1_pin, state_led);
 
}

} else if (pushpressed == 1) {

state_led = LOW;
digitalWrite(led1_pin, state_led);

} }

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 stopPins2() {
if (state_stop_buton == 1) {

pushpressed = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("second message ");
lcd.setCursor(0, 1);
lcd.print("was read ");
delay(1200);
lcd.clear();

} }

void startBuzz2() {

if (pushpressed == 1) {

if (state_led2 == LOW) {                 
  state_led2 = HIGH;
  digitalWrite(led2_pin, state_led2);

}

} else if (pushpressed == 0 ) { state_led2 = LOW; digitalWrite(led2_pin, state_led2); } }

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

} }