led signal flash code

Hi,
so I'm trying to make this code that makes a button flash once when pushed a certain amount of times.
in this code on the second press, I would like it to flash once here is the code const int buttonPin = 2; const int ledPin2 = 4; int count = 0; bool lastB - Pastebin.com
when I test the code the led terns on on the second press and dose not tern off
thanks for any help

Please post your code in a post using code tags; it's small enough; most people don't want to leave the site to download code.

Please read How to use this forum - please read - Website and Forum - Arduino Forum and pay specific attention to point #7 regarding the use of code tags.

I suggest that you add serial print statements to your code to see what happens, e.g. to print count at strategic places. E.g.

  if (buttonState == HIGH && lastButtonState == LOW) {
    lastButtonState = buttonState;
    count++;
    Serial.print("Count = ");
    Serial.println(count);
  }

How often do you see that come along and how often do you expect it to come along after a button press?

I suspect that bouncing is one of the problems; add a short delay (50ms or so) after the digitalRead for testing purposes.
Are you using a pull-down resistor on the button pin?

The below does not look very useful to me :wink:

  if (count == 1) {

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

Hi jude,

sterretje is right,

use the reply-function in this forum not the quick-reply.
IN the reply-menu use the

<|> - button to create a code-section

mark all lines inside your Arduino-IDE with Ctrl-a

copy itto the clip-board with Ctrl-c

set cursor into the code-section of the reply
paste with Ctrl-v.

we have to make sure that there is not the smallest type in the code you were compiling.

how is the led wired? if the LED is connected to +5V
the logic is inverted. digitalWrite(LOW) means light up
digitalWrite(HIGH) means LED off.

upload the following code to test the logic of the LED
after uploading open the serial monitor in the Arduino-IDE

const int ledPin =  4;

void setup() {
  pinMode(ledPin,    OUTPUT);
  Serial.begin(115200);
}

unsigned long TestTimer = 0;
int LedState = 0;

//nbt nonblockingtimer 
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}


void loop() {

  // only if time-period 0f 5 seconds has passed 
  // execute the code inside the if-condition
  // the Timer variable named "TestTimer" is updated automatically
  // through the call of the function "TimePeriodIsOver"
  if ( TimePeriodIsOver(TestTimer,5000) ) { 

    if (LedState == 0 ) {
      LedState = 1;            
      Serial.println("ledPin set HIGH");
      digitalWrite(ledPin,HIGH);
    }
    else {
      LedState = 0;            
      Serial.println("ledPin set LOW");
      digitalWrite(ledPin,LOW);      
    }    
  } // end of if ( TimePeriodIsOver(TestTimer,5000) )
}

The time constant for switching the LEd's state is set to 5 seconds so that you have really time to read the screen and take a look on the led

Analyse does the LED light up with message "ledPin set HIGH" or with message "ledPin set LOW" ?

best regards Stefan