Different LED blinking rates toggled by debounce switch

The following code I have has a light blink at one speed when the output is high and a different speed when the output is low, but each output only blinks once before it goes high or low. My goal for this project is to have it so when I press the debounce switch, the light will blink at one speed continuously speed until the button is pressed again and then it will toggle to a different blinking speed for the light. So the debounce switch can toggle between two different blinking settings. I am new Arduino, so the more you can help the better!

const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}

void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
if (buttonState == LOW) {
ledState = !ledState;
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}

Check this guide out https://forum.arduino.cc/index.php?topic=503368.0
UKHeliBob explain how to multitask the arduino and this will help you understanding what you need to do for this project.

You do not want to use the delay function since that is blocking the program from running.

Here’s a version using liberaries.

#include <mechButton.h> 
#include <blinker.h>

#define BUTTON_PIN   2
#define LED_PIN      13
#define PERIOD_A     300   // Ms
#define PERIOD_B     100    // Ms


mechButton  ourButton(BUTTON_PIN);        // Our button/switch. All deboiunced and running on auto.
blinker     ourBlinker(LED_PIN);          // Our blinker.
bool        blinkA;                       // Our state variable.

void setup(void) {

   ourBlinker.setPeriod(PERIOD_A);        // Initialze our blinker to peroid "A".
   blinkA = true;                         // Note that it's set to "A".
   ourBlinker.setOnOff(true);             // Fire up the blinks.
   ourButton.setCallback(buttonClicked);  // A function to call when the button gets clicked.
}


// This gets called when the button gets clicked.
void buttonClicked(void) {

   if (!ourButton.trueFalse()) {          // If the pin has gone to ground.. (pressed)
      if (blinkA) {                       // If the lights are running period "A"..
         ourBlinker.setPeriod(PERIOD_B);  // Set to period "B"
      } else {                            // Else they are alredy period "B"..
         ourBlinker.setPeriod(PERIOD_A);  // So we set them to peroid "A".
      }
      blinkA = !blinkA;                   // Flip the state of the LED state variable.
   }
}


void loop(void) {
   
   idle();  // This lets things like the mechButton do their thing.
}

You will need to install LC_baseTools from the library manager to try it.

Good luck!

-jim lee

it's just a matter of toggling between two different timing intervals

while you try to detect a button state, you need to capture that state when it changes. and when it changes you need to check for the active state (LOW).

when there's a button pressure a flag can be toggled and used to select between different delays

while you check that the time since some last time is > some interval, the last time must be set to the current time.

there's no need to check for buttons when the time interval has expired.

and using shorter variable names makes the code easier to read and is less confusing

consider

#if 0       // my  hardware
const int buttonPin = A1;
#else
const int buttonPin = 2;
#endif

const int ledPin = 13;

unsigned long debounceDelay = 200;

byte butLst;
unsigned long msecLst;

int  mode = 0;

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (115200);

    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    byte but = digitalRead(buttonPin);

    if (butLst != but) {
        butLst = but;

        if (LOW == but)  {
            mode = ! mode;

            if (mode) 
                debounceDelay = 200;
            else
                debounceDelay = 500;
            Serial.println (debounceDelay);
        }

        delay (20);
    }

    unsigned long msec = millis ();
    if ((msec - msecLst) > debounceDelay) {
        msecLst = msec;
        digitalWrite (ledPin, ! digitalRead(ledPin));
    }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.