1
const int buttonPin = 2;
boolean currentState = LOW;
boolean lastState = LOW;
boolean stateChange = false;
int currentButton = 0;
int lastButton = 4;
int ledArray[] = {9, 10, 11, 12};
long previousMillis = 0;
long interval = 1000 ;
void setup() {
 pinMode(buttonPin, INPUT);
 for (int i=0; i<5; i++){
 pinMode(ledArray[i],OUTPUT);
 }
 Serial.begin(9600);
}
void loop(){
 currentState = debounceButton();
 stateChange = checkForChange(currentState, lastState);
 currentButton = getButtonNumber(lastButton, currentState, stateChange);
 indicatorLight(currentButton);
 lastState = currentState;
 lastButton = currentButton;
}
boolean debounceButton()
 {
 boolean firstCheck = LOW;
 boolean secondCheck = LOW;
 boolean current = LOW; 
 firstCheck = digitalRead(buttonPin);
 delay(50);
 secondCheck = digitalRead(buttonPin); 
 if (firstCheck == secondCheck){
 current = firstCheck;
 }
 return current;
}
boolean checkForChange(boolean current, boolean last)
 {
 boolean change; 
 if (current != last){
 change = true;
 }
 else {
 change = false;
 } 
 return change;
}
int getButtonNumber(int button, boolean state, boolean change)
 { 
 if (change == true && state == LOW){
 button++;
 if (button > 3){
 button = 0;
 }
 Serial.println("g");
 
 }
 return button;
}
void indicatorLight(int button)
 {
 for (int i=0; i<4; i++) {
 digitalWrite(ledArray[i], LOW);
 }
 digitalWrite(ledArray[button], HIGH);
 }
the busybee
2,4089 silver badges18 bronze badges
asked Sep 9, 2021 at 15:26
1
  • 3
    Use millis() to capture a timestamp of when you are turning the LED on, then use that in loop() to check, if more than 30s passed. In that if statement turn off all LEDs. Refer to the BlinkWithoutDelay example, that comes with the Arduino IDE Commented Sep 9, 2021 at 15:54

1 Answer 1

1

You have the right idea to debounce the buttons, but there is a 50 ms delay per button which blocks the loop for a total of 200 ms with four buttons.

So consider using a non-blocking debouncer of which there are many examples on the internet, but here is a simple debouncer I put on GitHub. It could be implemented something like the following example:

#include "debouncer.h"
const uint16_t LED_OFF_TIME_DELAY_ms = 2000; // milliseconds. For testing.
//const uint16_t LED_OFF_TIME_DELAY_ms = 30000; // milliseconds.
struct ButtonLed
{
 Debouncer button;
 byte button_pin;
 byte led_pin;
};
const ButtonLed BUTTONS_LEDS[] = 
{
 { Debouncer(5, 50, LED_OFF_TIME_DELAY_ms), 5, 11 },
 { Debouncer(4, 50, LED_OFF_TIME_DELAY_ms), 4, 10 },
 { Debouncer(3, 50, LED_OFF_TIME_DELAY_ms), 3, 9 },
 { Debouncer(2, 50, LED_OFF_TIME_DELAY_ms), 2, 8 }
};
void setup()
{
 for (const auto& button_led : BUTTONS_LEDS)
 {
 pinMode(button_led.button_pin, INPUT_PULLUP);
 pinMode(button_led.led_pin, OUTPUT);
 digitalWrite(button_led.led_pin, LOW);
 }
 pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
 uint16_t timestamp = millis();
 //
 // TASK 1: Read the buttons and set their corresponding LEDs.
 //
 for (const auto& button_led : BUTTONS_LEDS)
 {
 button_led.button.Update();
 if (button_led.button.Fall())
 {
 digitalWrite(button_led.led_pin, HIGH);
 }
 else if (button_led.button.RepeatCount() == 1)
 {
 digitalWrite(button_led.led_pin, LOW);
 }
 }
 //
 // TASK 2: Blink the inbuilt LED.
 //
 const uint16_t LED_BLINK_INTERVAL = 100;
 static uint16_t led_blink_previous_timestamp = timestamp;
 static bool led_state = false;
 if (timestamp - led_blink_previous_timestamp >= LED_BLINK_INTERVAL)
 {
 led_state = !led_state;
 digitalWrite(LED_BUILTIN, led_state);
 led_blink_previous_timestamp += LED_BLINK_INTERVAL;
 }
}

And here it is in action in the Wokwi simulator:

answered Sep 10, 2021 at 16:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.