Blinking an LED after a certain number of button presses are made

Hie There Guys,

I hope i find you well. I am doing a small hobby project of blinking an LED bulb when a defined number of button presses are made. Basically it is suppose to work like this: If one button press is detected, turn led light high else if two button presses are detected, turn led light high else if three button presses are detected, turn led light high

Here is the source code i had compiled. Unfortnuately when i run it on the arduino it is not giving the expected results when button clicks are made. The LED light doesn't go on. The following is the code snippet:

int state = LOW; int lastState = LOW; int count = 0; int LED_ON = 13; // declaring the pin where the LED light is connected.

void setup() { // put your setup code here, to run once: pinMode(8,INPUT); pinMode(LED_ON,OUTPUT);

}

void loop() { // put your main code here, to run repeatedly:

if(state==HIGH && lastState==LOW) { count ++;

if(count = 1){

digitalWrite(LED_ON,HIGH); delay(2000); digitalWrite(LED_ON,LOW);

} if(count = 2){

digitalWrite(LED_ON,HIGH); delay(2000); digitalWrite(LED_ON,LOW);

} if(count = 3){

digitalWrite(LED_ON,HIGH); delay(2000); digitalWrite(LED_ON,LOW);

}

}

When i compile it, it doesnt give me any errors but after when i upload and try to make the button presses so as to blink the led after the set number of presses i get no response.

Your assistance will be greatly appricated,

First of all, use code tags please,

Second, you never ever actually read a pin... You just have a variable state and a variable last state. My tip, just use the Bounce2 library to read the button. It takes all the work of "becoming pressed" and debounce out of your hands. And please give all of your pins you use a name.

Also mind that because of your delays you can't see a button press while blinking the output...

how do you reset ? what happens after 4 button presses ?

couple of options.

if(count = 3){  //  hast to be  if(count == 3 ){  not count=3
  count=0;  // resets count to zero so it will be 1 on the next press

or maybe :

if(state==HIGH && lastState==LOW) {
  count ++;
  if(count==4){
     count=1;
     }

bty you are setting count to 3, not seeing if it is set to three, that is if(count ==3), with double ==

How do you know what "lastState" is ?

the typical way is that on the last line, you put in

lastState =state;  // set lastState to value of state

also, how you you know what state you are in ? the results are all the same.

Hie guys,

Thank you somuch for your feedback. I’m young budding hobbyist from Zimbabwe but with lots of passion and energies ;D . as advised, i have managed to attach the source code with changes you have suggested.

Just to explain a little bit further, basically what am trying to achieve with this code is to be able to blink the pin13 LED light for 2 seconds everytime when :
1.) 1 button press is made,
2.) 2 button presses are made
3.) 3 button presses are made
at the end of the code is suppose to clear and start again

Here is code below but i have also attached the source code file :slight_smile:

int state = LOW; // this is the state of the pin when not the switch is not pressed
int lastState = LOW; // we are delcaring the last state of the pin as low, since we are using a soft touvh
int count = 0;
int LED_ON = 13; // declaring the pin where the LED light is connected.

void setup() {
// put your setup code here, to run once:
pinMode(8,INPUT); // we are setting pin 8 as our input where we are connectign the soft touch switch
pinMode(LED_ON,OUTPUT); // this is the pin where the LED light will connected

}

void loop() {

lastState=state;
state=digitalRead(8);
if(state==HIGH && lastState==LOW) { // debouncing condition
count ++;

if(count == 1){
// if one button click is detected the code is suppose to blink LED on pin 13 for 2 seconds
digitalWrite(LED_ON,HIGH); // setting pin13 high after when 1 button press is detected
delay(2000); // delaying 2 seconds before pin13 is set lOW
digitalWrite(LED_ON,LOW); // setting pin13 low, thereby switching off the LED on pin13

}
else
if(count == 2){
// if one button click is detected the code is suppose to blink LED on pin 13 for 2 seconds
digitalWrite(LED_ON,HIGH);// setting pin13 high after when 1 button press is detected
delay(2000); // delaying 2 seconds before pin13 is set lOW
digitalWrite(LED_ON,LOW); // setting pin13 low, thereby switching off the LED on pin13

}
else
if(count == 3){
// if one button click is detected the code is suppose to blink LED on pin 13 for 2 seconds
digitalWrite(LED_ON,HIGH); // setting pin13 high after when 1 button press is detected
delay(2000); // delaying 2 seconds before pin13 is set lOW
digitalWrite(LED_ON,LOW); // setting pin13 low, thereby switching off the LED on pin13

}

}

blinking_code.ino (1.69 KB)

Just to explain a little bit further, basically what am trying to achieve with this code is to be able to blink the pin13 LED light for 2 seconds everytime when : 1.) 1 button press is made, 2.) 2 button presses are made 3.) 3 button presses are made at the end of the code is suppose to clear and start again

To summarise the requirement stated above.

Every time the button is pressed blink the LED for 2 seconds.

go here

http://forum.arduino.cc/index.php/topic,148850.0.html

scroll down to #7

come back and edit your posts

so, just three times ? it never resets and ANY button press over 3 is ignored. need a hard reset of the Arduino to have them see only 3 button presses. ?