Led should blink only when button is pushed again

Led should blink 3 times if button is pressed (HIGH) and then stop, but if button stays pressed (HIGH) for longer period of time, LED should still blink only 3 times and then stop until new press of a button. Now i made counter for a pushbutton that increments every time INPUT is high and is % by 2 so if 0 is left, then it should blink otherwise it stays low, but for some reason it doesn't work. Is my strategy wrong and there is other way to make what i want? Here is my code:

const int buttonPin = 3;
const int ledPin = 6;
int buttonPushCounter = 1;
int buttonState = 0;
int lastButtonState = 0;

void setup() {

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (buttonState == HIGH){
buttonPushCounter++;
}

lastButtonState = buttonState;

delay(50);

if (buttonPushCounter % 2 == 0) {

digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);

} else {
digitalWrite(ledPin, LOW);
}
}
}

Dont you just need to do the blink x3 every time the button becomes pressed. Atm its only going every 2nd time.

Mind you Im reading un- code-tagged code on my mobile which is a bit messy.

anthonyHope:
Dont you just need to do the blink x3 every time the button becomes pressed. Atm its only going every 2nd time.

Mind you Im reading un- code-tagged code on my mobile which is a bit messy.

Yes it should blink every time button is pressed, it should be %1 instead of 2. Anyway it blinks 3 times when button is HIGH but it blinks 3 times again when button becomes LOW. Why is that

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

If you are pulling the input HIGH, do you have a 10K pulldown resistor between the input and gnd?

Thanks.. Tom.. :slight_smile:

You need to debounce the button.

EDIT: Sorry, did not see the debouncing "delay(50)".

If I were you Id lose the counter and just move the blinky code up to where you determine theres been a new press, ie where you currently do the ++.

And why didnt you just continue this discussion in your other thread?

Hi,
Try this code, I have changed to a bool value for the flash.
Also added Serial.print for debugging.
Set the IDE monitor to 115200 baud.
Make sure you have a pulldown resistor I mentioned in post #3.

const int  buttonPin = 3;
const int ledPin = 6;
int buttonPushCounter = 1;
int buttonState = 0;
int lastButtonState = 0;
bool flashState = false;


void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.println(" == Button Press - Three Flash V1.0 == ");
}


void loop()
{
  buttonState = digitalRead(buttonPin);
  Serial.print("buttonState = ");
  Serial.print(buttonState);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      flashState = true;
    }
  }
  Serial.print("\t flashState = ");
  Serial.print(flashState);
  lastButtonState = buttonState;


  if (flashState == true)
  {


    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    flashState = false;


  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
  Serial.println(" ");
}

Tom... :slight_smile:

TomGeorge:
Hi,
Try this code, I have changed to a bool value for the flash.
Also added Serial.print for debugging.
Set the IDE monitor to 115200 baud.
Make sure you have a pulldown resistor I mentioned in post #3.

const int  buttonPin = 3;

const int ledPin = 6;
int buttonPushCounter = 1;
int buttonState = 0;
int lastButtonState = 0;
bool flashState = false;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.println(" == Button Press - Three Flash V1.0 == ");
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  Serial.print("buttonState = ");
  Serial.print(buttonState);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      flashState = true;
    }
  }
  Serial.print("\t flashState = ");
  Serial.print(flashState);
  lastButtonState = buttonState;

if (flashState == true)
  {

digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    flashState = false;

}
  else
  {
    digitalWrite(ledPin, LOW);
  }
  Serial.println(" ");
}




Tom... :)

Oh Thank you so much Tom. It works! I'll study this code and understand it completely now. People like you are really motivation for me. Thank you again and to all others.