Is it possible to let a LED blink, for example 5 times, with an Arduino? Should I use a for
loop?
Something like this works in setup
, but it will run continuously in the void loop
, so my LED keeps blinking.
for (int i = 0; i <= 5; i++) {
led HIGH
delay(500);
led LOW
delay(500);
}
Also, the blink sequence has to be triggered, and have a reset after it is done.
The complete code is too big to share, but it runs a bit like the following:
There is a "bankValue", it is filled by a user, and counts back to zero.
If it's zero, then blink 5 times.
After the blink, the LEDs are off.
The sketch is waiting for a new user, who will fill the bankValue again and the LED blink should again be initiated.
1 Answer 1
Its really heavy to give a correct answer, if you don't show us the context where the for loop is used. But I'll try:
There should be a part in the void loop method where you detect that the "bankValue" is down to zero. Like:
if (bankValue == 0)
{
}
First you must be sure that, the for loop is called within this block.
if (bankValue == 0)
{
// I corrected the loop to run exact 5 times (<= -> <)
for (int i = 0; i < 5; i++)
{
led HIGH
delay(500);
led LOW
delay(500);
}
}
Then you need a flag int blinkingDone = 0;
to indicate that the for loop has executed. This flag must be initialize with a falsy value, set to trueish after the loop is done and reset if the user "fills" the bankValue again.
Then you could use it as a guard to enter the for loop. Like:
int blinkingDone = 0;
int bankValue = 0;
..... somewhere else
if ( coin inserted or so )
{
// fill bankValue
blinkingDone = 0;
}
..... somewhere else
if (bankValue == 0 && ! blinkingDone)
{
// I corrected the loop to run exact 5 times (<= -> <)
for (int i = 0; i < 5; i++)
{
// led HIGH
delay(500);
// led LOW
delay(500);
}
blinkingDone = 1;
}
There are potential better ways to do it. But I don't know your code. So this is the simplest way I can imagine.
-
This is really helpfull. I didn't expect a perfectly ready answer. Now I know I have to work with a Flag for blinkingDone. The code itself is on several tabs in Arduino and in total a few 100 lines. I imagine it is to much to add here. I don't know if I used the tabs correctly, but that is something for another thread Again, this is really helpfullNiles– Niles2020年10月27日 10:39:37 +00:00Commented Oct 27, 2020 at 10:39
-
in the line if (bankValue == zero && ! blinkingDone) what is the meaning of the !Niles– Niles2020年11月01日 07:01:00 +00:00Commented Nov 1, 2020 at 7:01
-
It's the not operator. It means "blinking is not done". Ahh and the line contains a typo zero must be 0. I should really think before writing ;-)Peter Paul Kiefer– Peter Paul Kiefer2020年11月01日 08:34:16 +00:00Commented Nov 1, 2020 at 8:34
new user, who will fill the bankValue
. please elaborate...loop()
repeats tens of thousands of times per second (if there are no delays, of course) .... to run the code only once, use a flag such assetup () {bool xyz = true;}
... then you check the state of the flagloop () { if (xyz) { blink leds; do other stuff; xyz = false; } }
... this will run only once, unless there is some code inloop()
that sets the flag again