Only Execute Once

Hi!,

Is there a way I can make a code inside a loop to execute only once? Likes the code only execute once when Arduino turn on or when is reset.

Xenia2:
Hi!,

Is there a way I can make a code inside a loop to execute only once? Likes the code only execute once when Arduino turn on or when is reset.

Well the easiest way is to place the statements you want to execute just once into the startup loop, that's why its there. However there are ways to structure your main loop to have a section just execute the first time through the loop, but that is kind of kludgy if you ask me.

Lefty

If the piece of code is dependant on something else happening first then you can simply set a flag when that thing happens and check for it..

if ( checkForSomethingHappening() && itHasntHappenedBefore )
{
// Do some funky one-time magic, then flip the flag so we never come in here again
doSomeFunkyOnetimeMagic();
itHasntHappenedBefore = false
}

Of course you could (should!) embed that same logic into checkForSomethingHappening() function! :slight_smile:

Cheers,

Nope, it is not dependent on anything. I just wanted the LEDs to flash before the main loop kick in.

Easy then - do that in setup()

G.

Just want to clarify something.

These same LEDs are also inside my main loop; turn on/off based on the input. I have six seconds before one of the LED turn on solid, within that six second I want the LEDs to flash.

Xenia2:
Just want to clarify something.

These same LEDs are also inside my main loop; turn on/off based on the input. I have six seconds before one of the LED turn on solid, within that six second I want the LEDs to flash.

Not a problem, the coding you do in the setup() function determins how long you remain in the setup() function and then enter the loop() function.

Lefty

So how the LED flash if it is not inside a loop function?

Xenia2:
So how the LED flash if it is not inside a loop function?

You can control the LED both in the setup and in the loop function.

Lefty

Xenia2:
So how the LED flash if it is not inside a loop function?

Better read up on doing loops in C.

For example:

for (byte i = 0; i < 6; i++)
  {
  digitalWrite (ledPin, HIGH);
  delay (200);
  digitalWrite (ledPin, LOW);
  delay (200);
  }

Oh i didn't think of that :wink:

My replacement laptop just came in, so I got a chance to test the code.

I have to find another way to get the LEDs to flash in the main loop only once at startup. The flashing LEDs inside the setup() did not work the way I wanted. I would like the LEDs to flash every one second for six seconds.

I showed you how to execute something once in the main loop earlier. Refer to that... and then actually have a go at writing some code. If you have specific problems then I'm sure we can assist, but we're not going to write your code for you - well I'm not anyway...

What you are asking is trivial in the extreme - you need to a) keep track of how many times you've flashed the LED, and b) keep track of the 1 second spacing...

G.

^ Oh I am not expecting you the write the code :slight_smile: I was just looking for examples or how to get started.

Xenia2:
My replacement laptop just came in, so I got a chance to test the code.

I have to find another way to get the LEDs to flash in the main loop only once at startup. The flashing LEDs inside the setup() did not work the way I wanted. I would like the LEDs to flash every one second for six seconds.

What was the code you used to do this? What did it do? What did you want to be different?

This is just enhancement to my current code, which monitor the water level, if water fall below the sensor it will turn on the pump.

Right now every six seconds it read the sensor value, if LOW turn on the pump (yellow LED on), after six seconds if HIGH turn off the pump (green LED on). So at startup it will wait six seconds before anything happens, after six second one of the LED turn on (either pump on or pump off state). I wanted to take that only intial six seconds and have the all LEDs to flash before one of the LED remain solid on.

It was suggested that I can get the LED to flash inside setup(), but I dont see how to get its flash without inside a main loop().

A large number of people on the forum could very easily make helpful suggestions, but seeing your code is crucial to those suggestions making sense.

Also, why do you want a six second delay when the program starts? Is it a desired feature, or a side effect of how loop is structured?

Because after pump turn on, I wanted its to remain on for 6 more seconds...just a buffer to water level.

Xenia2:
I was just looking for examples or how to get started.

Other than the one I wrote? In what way did that not work?