Home > Net >  Using Pushbutton to toggle a blinking LED
Using Pushbutton to toggle a blinking LED

Time:01-05

I'm currently working on a mini project to have my pushbutton toggle a blinking LED, meaning: when I press the button, I want the LED to keep blinking until I press the button again.

I managed to toggle the LED without blinking (when I press the button, the LED lights up, and when I press the button again, the LED switches off, but no blinking).

I've appended my code below, and my logic is based on tracking the current button state and the previous button state, which I think is the correct logic (but if I'm wrong please criticise/correct me!!) to cause a change in LED output state, so I'm not sure what's wrong with my thinking/code. The problem I have now is that the LED will only blink twice (according to the line of code I've written in void loop) but it does not keep blinking forever until I want it to stop (what can I do to execute this?)

const int ledPin = 8;
const int buttonPin = 7;

int buttonState;
int ledState = HIGH;
int lastbuttonState = LOW;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastbuttonState) {
    if (ledState == HIGH) {
      digitalWrite(ledPin, HIGH);
      delay(200);
      digitalWrite(ledPin, LOW);
      delay(200);
    } else {
      digitalWrite(ledPin, LOW);
    } 
  }
   lastbuttonState = buttonState;
}

Will be extremely grateful if anyone can point out any mistakes!! Thank you!

CodePudding user response:

you can debounce using millis() function as:

#define ledOn LOW
#define ledOff HIGH 

unsigned long buttonTime = 0;       
unsigned long debounceTime = 400;    

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState != lastbuttonState && millis() - buttonTime > debounceTime) {
    ledState = !ledState;
    buttonTime = millis();
  }

  if (ledState == ledOn) {
    digitalWrite(ledPin, (millis() >> 9) & 3);  //Blink
  } else
    digitalWrite(ledPin, ledOff);

  lastbuttonState = buttonState;
}

CodePudding user response:

This part (buttonState != lastbuttonState) is logically incorrect as the state of the push button will immediately become false when you press the push button once and release it. The last state of the push button would be FALSE as per the logic that you have written.

Another way could be to implement would be to increment an counter variable on pressing the push button once and start blinking when the counter value is odd and stop blinking when the counter value is even.

const int ledPin = 8;
const int buttonPin = 7;

int buttonState=HIGH,reading;
int ledState = LOW;
int lastbuttonState = HIGH,icounter; 
unsigned long latchMillis,lastDebounceTime;  

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}  

void loop() 
{  
  unsigned long blinkdelay = 1000,debounceDelay=1;
 
 /***Debounce logic for the input ***/
  reading = digitalRead(buttonPin); //Check for the change in PB 
  if (reading != lastbuttonState) 
  { 
    lastDebounceTime = millis(); // reset the debouncing timer
  }
  lastbuttonState = reading; //Update previous button state

  if ((millis()-lastDebounceTime) > debounceDelay) //If debounce has elapsed then accept the input
  {
    if (buttonState != lastbuttonState) 
    {
      if (buttonState == LOW) 
      {
        icounter = icounter  1; //Increment counter on every rising edge of PB  
        latchMillis = millis(); //Latch the current millis
      } 
     buttonState = lastbuttonState;
    }
  }

   if ((icounter%2)!= 0) 
   {   
    if ((millis() - latchMillis) > blinkdelay)
    {
      ledState = !ledState;
      latchMillis = millis();
      digitalWrite(ledPin,ledState);
    }
    
   } 
   else 
   {
     digitalWrite(ledPin, LOW);
   }

} `

  •  Tags:  
  • Related