Go Down

Topic: Only Execute Once (Read 2 times) previous topic - next topic

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.

retrolefty


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


pocketscience

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! :)


Cheers,
Is life really that serious...??!

Xenia2

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

pocketscience

Easy then - do that in setup()

G.
Is life really that serious...??!

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.

retrolefty


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


Xenia2

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

retrolefty


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

Nick Gammon


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


Better read up on doing loops in C.

For example:

Code: [Select]
for (byte i = 0; i < 6; i++)
  {
  digitalWrite (ledPin, HIGH);
  delay (200);
  digitalWrite (ledPin, LOW);
  delay (200);
  }
http://www.gammon.com.au/electronics

Xenia2

Oh i didn't think of that  ;)

Xenia2

#11
Aug 16, 2011, 03:02 am Last Edit: Aug 16, 2011, 03:07 am by Xenia2 Reason: 1
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.

pocketscience

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.
Is life really that serious...??!

Xenia2

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

wildbill


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?

Go Up