Hi,
I am new to all is coding so i am looking for some help please. I have this code (which i go from examples) which makes the LED high 3 seconds after you have pressed and released the button, for 3 seconds. The LED then continues to flash every 3 seconds. I would like i to stop after the first flash but also be trigger again when the button it pressed and released. Could anyone please help.
Tom
const int buttonPin = 2;
const int ledPin = 8;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0)
{
digitalWrite(ledPin, LOW);
} else {
// time before purge
delay (3000);
digitalWrite(ledPin, HIGH);
// time purges for
delay (3000);
digitalWrite(ledPin, LOW);
}
}