when I on 1 push button LED on-time 3 seconds, and I press it again, it will automatic off, that the way when I press 2 pushbuttons LED on time is 5 seconds and if I press 3 push button LED on time is 10 seconds. please, somebody, help me.
divyajeet_22:
please, somebody, help me.
The most effective way to get help, is for you to post the code you tried so far, and describe what's not/working, and say what you specifically need with.
Does this:
push button LED on-time 3 seconds, and I press it again, it will automatic off,
... mean that if you press the button again while the led is on (for the 3/5/10) seconds, it must be interrupted and turned off straight away?
You could refer to the below code:
#include <ezButton.h>
ezButton button1(6); // create Button object that attach to pin 6;
ezButton button2(7); // create Button object that attach to pin 7;
ezButton button3(8); // create Button object that attach to pin 8;
const int led_pin = 10;
const unsigned long led_on_interval = 0;
unsigned long previousMillis = 0;
int led_state = LOW;
bool is_trigger = false;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(100); // set debounce time to 100 milliseconds
button2.setDebounceTime(100); // set debounce time to 100 milliseconds
button3.setDebounceTime(100); // set debounce time to 100 milliseconds
inMode(led_pin, OUTPUT);
digitalWrite(led_pin, led_state);
}
void loop() {
button1.loop(); // MUST call the loop() function first
button2.loop(); // MUST call the loop() function first
button3.loop(); // MUST call the loop() function first
int btn1State = button1.getState();
int btn2State = button2.getState();
int btn3State = button3.getState();
int case = 0;
if(btn1State == LOW && btn2State == LOW && btn3State == LOW){ // three button
case = 3;
led_on_interval = 10000; // 10 seconds
}
else if(btn1State == LOW && btn2State == LOW){ // two button
case = 3;
led_on_interval = 5000; // 5 seconds
}
else if(btn1State == LOW){ // one button
case = 3;
led_on_interval = 3000; // 3 seconds
}
else { // no button
is_trigger = false;
}
if(case != 0 && is_trigger == false) {
is_trigger = true;
led_state = (led_state == LOW)? HIGH : LOW;
previousMillis = millis(); // save timestamp
digitalWrite(led_pin, led_state); // control led
}
if (millis() - previousMillis >= led_on_interval) {
led_state = LOW; // turn OFF LED after led_on_interval
digitalWrite(led_pin, led_state); // control led
}
}
The library is available here