So I have been trying very unsuccessfully to remove the delay components ( delay(blinkTime); ) of this script minus the 10 second one which is necessary for the button push.
int buttonState = 0;
int blinkTime = 200;
int ledPin = 13;
void setup()
{
pinMode(2, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
buttonState = digitalRead(2);
if (buttonState == HIGH) {
for (int i = 0; i < 5; i++)
{
digitalWrite(ledPin, HIGH);
delay(blinkTime);
digitalWrite(ledPin, LOW);
delay(blinkTime);
}
} else {
digitalWrite(ledPin, LOW);
}
delay(10);
}
The desired effect is that when I press the button, the LED flashes 5 times then goes to low.
Just the same way as you flash the led without using delay, but add a state variable to count the flashes...
int count = 0
if (button pressed) {
count = 5
last_time = now
led = on
}
// flash the led, stop when count == 0
if (timer popped and count > 0) {
if (led == on) {
led = off
count = count - 1
} else {
led = on
}
last_time = now
}
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.
It is - but it also doesn't fulfil the OP's "only blink 5 times when I press the button" requirement?
I agree it is simpler from the code point of view using the ! (NOT) logic to invert the state of the pin. But it's also, in some ways, "more complex" for a beginner to understand.
And, if you want to do only 5 flashes, you then have to have a counter from 10 (or perhaps 9) to count half cycles for both ON and OFF periods, instead of whole cycles. Or introduce an IF anyway so as to only decrement count from 5 on the OFF edge....
Hence why I coded the "if its on... turn it off... else... turn it on...". Easy for beginner to understand, especially if they are not already familiar with boolean logic, the ! (NOT) operator, and that fact you can read from an output pin.
Not difficult concepts to grasp I grant you, but every little simplification helps for someone who is just starting out (in my book anyway, YMMV).
I've run into this before. The button must be held to continue to work. I'm hoping that I can simply engage the button and it will carry out 5 of the same on off sequences.
chipnod2020:
I've run into this before. The button must be held to continue to work. I'm hoping that I can simply engage the button and it will carry out 5 of the same on off sequences.
See post #2 for the pseudo code for this. Just put together the bits of code in this thread to implement it.