#include <Bounce2.h>
byte led1State = LOW;
byte timer1Active = 0;
const byte led1Pin = LED_BUILTIN;
const byte buttonPin1 = 6;
const byte buttonPin2 = 7;
unsigned int timer1Counter = 0;
const unsigned long debouncerInterval = 50;
const unsigned long oneSecond = 1000;
unsigned long previousMillis = 0;
Bounce debouncer = Bounce();
Bounce debouncer2 = Bounce();
void setup(){
Serial.begin(9600);
debouncer.attach(buttonPin1, INPUT_PULLUP);
debouncer.interval(debouncerInterval);
debouncer2.attach(buttonPin2, INPUT_PULLUP);
debouncer2.interval(debouncerInterval);
pinMode(led1Pin, OUTPUT);
}
void loop(){
unsigned long currentMillis = millis();
// 4 hour, 30 second timer.
if((currentMillis - previousMillis >= oneSecond) && (timer1Active == 1)){
previousMillis = currentMillis;
timer1Counter++;
if(timer1Counter < 31){
//if(led1State == LOW){
led1State = HIGH;
//}
//else{
//led1State = LOW;
//}
digitalWrite(led1Pin, led1State);
}
else if(timer1Counter == 31){
digitalWrite(led1Pin, LOW);
}
// 60 sec X 60 min X 4 hours + 30 seconds.
if(timer1Counter > 14430){
timer1Counter = 0;
}
}
// Button 1
if(debouncer.update()){
if(debouncer.read() == 0){
Serial.println("Start 30 second ON, 4 hour OFF timer.");
timer1Active = 1;
timer1Counter = 0;
}
}
// Button 2
if(debouncer2.update()){
if(debouncer2.read() == 0){
Serial.println("Start 1 minute ON, 4 hour OFF timer.");
timer1Active = 0;
digitalWrite(led1Pin, LOW);
}
}
}
Based on this part of your last comment "I’ve a trick! ‘const unsigned long oneSecond = 1000;’ should be edited to ‘const unsigned long oneSecond = 10;’ this make the led be as a constant ON.", it sounds like you want the LED to remain ON constantly for the 30 or 60 seconds. I have "commented out" 5 lines of the above sketch that will make this happen.