Pages: [1] 2   Go Down
Author Topic: Only Execute Once  (Read 1849 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 332
Posts: 16568
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Sydney, Australia
Offline Offline
Full Member
***
Karma: 3
Posts: 230
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley


Cheers,
Logged

Is life really that serious...??!

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, Australia
Offline Offline
Full Member
***
Karma: 3
Posts: 230
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Easy then - do that in setup()

G.
Logged

Is life really that serious...??!

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 332
Posts: 16568
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 332
Posts: 16568
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Better read up on doing loops in C.

For example:

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

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh i didn't think of that  smiley-wink
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 15, 2011, 08:07:00 pm by Xenia2 » Logged

Sydney, Australia
Offline Offline
Full Member
***
Karma: 3
Posts: 230
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Is life really that serious...??!

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1] 2   Go Up
Jump to: