Run loop x times and stop until hard reset?

I know this has got asked many time and received many answers. But what is the best way to do this?
I have a situation where in the morning the lights(power) are turned on. When this happens Arduino will run function(sending IR remote signal) 3 times(to make sure it is received by the fixtures). Then it will rest until next morning - at night the power will turned off.
So should I use counter, blinkyBlinky, exit(0) or something else?

To do something once, you can put that code in setup() and keep the loop() empty.

And if you need to loop a few times, Just add at the beginning of the loop()

if (counter >= maxIteration) while(true) yield();

Where counter Is a global variable (a byte for example if maxIteration is less than 255)
Of course replace maxIteration by your value or define that as a global constant

I did it in the setup() at first, but found that sometimes the fixture did not read the message and it seemed to be logical to put the function in the loop() in order to run it several times. So counter it is then.
Do I need to set counter = 0; in the end? I think yield() takes care of stopping Arduino?

The only thing that stops an Arduino is to remove the power.

Where I live we get power failures pretty much every day. I wanted a warning to tell me the power came back on. I have an Arduino on the mains with a mobile phone charger and just buzz a buzzer three times in setup() with a for() loop.

How does the Arduino know when the lights are turned on?

No it just prevents the watchdog from rebooting your arduino for some Arduinos (when you are stuck in an infinite loop doing nothing).

I added that there just as a good practice

Do I understand right:

in the morning power (some lights) is switched on. The arduino is powered through a wallplug that gets power too if the lights are switched on?

If the arduino is powered an IR-signal shall be sent three times just to make sure the receivers of the IR-signal really received the signal.

during the day the arduino will still be under power but has absolutely nothing to do

In the evening power is swicthed off which means the arduino is unpowered to
until next morning.
In the next morning the same procedure happends?

is this correct?

best regards Stefan

The loop() repeats over and over forever. If you want to stop the loop() after a few iterations, you can send the µC into an infinite loop with a call to


as soon as some condition (i.e. a counter has reached a value) is met. This is, after all, the same as having a brief for loop in setup() and then an empty loop().

so basically what was offered in #3 but with less information...

Yes, Stephan that exactly what I plan.
I need to set the light fixtures in certain way. By default when powered on they just run all colours and modes and IR remote is the only option to change it(no DMX). Since the person who opens the gallery may be anybody I need to have this automated.

OK make a first attempt what you think how this can be coded.
Or asked questions. The questions should be specific.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Yes, I add nothing new there, but sometimes hearing the same thing stated again in a slightly different matter is what makes it click into place.

Indeed :innocent: