0

Is it possible to let a led blink, for example 5 times with Arduino? Should I use a For loop?

Something like this works within the setup loop, but it will run continuesly 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 to 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 ledblink should again be initiated.

4
  • In your for-loop, which is not recommended btw, you'd need a second delay to see anything blinking. The miracle however is your new user, who will fill the bankValue. please elaborate... – DataFiddler Oct 26 '20 at 15:15
  • @jsotola, to refrase the question: How can I let a led Blink five times? In my defense, I added the code snippet to show what I have tried. – Niles Oct 27 '20 at 9:23
  • @DataFiddler, I know the delay is not recommended, better to use millis. It was just an example of what I have tried. To elaborate, the program is for some kind of CarWash. The program waits for some Coins/Tokens/eMoney, which is stored in the bankValue. When the bank is filled, the user can choose different WashPrograms. Relays will activate corrosponding the program. bankValue will decrease when using the WashProgram, until it is zero. All relays will deactviate. On this point I want a flashing LED as a sign that the bank is empty. Washing is done, and now waiting for new money input – Niles Oct 27 '20 at 9:30
  • 1
    @Niles the code, as you presented it, blinks 5 times, but it gets executed repeatedly because 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 as setup () {bool xyz = true;} ... then you check the state of the flag loop () { if (xyz) { blink leds; do other stuff; xyz = false; } } ... this will run only once, unless there is some code in loop() that sets the flag again – jsotola Oct 27 '20 at 17:30
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.

3
  • 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 helpfull – Niles Oct 27 '20 at 10:39
  • in the line if (bankValue == zero && ! blinkingDone) what is the meaning of the ! – Niles Nov 1 '20 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 Nov 1 '20 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.