Offline
Newbie
Karma: 0
Posts: 40
|
 |
« on: August 10, 2011, 05:29:26 pm » |
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)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #1 on: August 10, 2011, 05:35:28 pm » |
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
Full Member
Karma: 3
Posts: 230
Arduino rocks
|
 |
« Reply #2 on: August 10, 2011, 05:44:45 pm » |
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,
|
|
|
|
|
Logged
|
Is life really that serious...??!
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #3 on: August 10, 2011, 06:27:05 pm » |
Nope, it is not dependent on anything. I just wanted the LEDs to flash before the main loop kick in.
|
|
|
|
|
Logged
|
|
|
|
|
Sydney, Australia
Offline
Full Member
Karma: 3
Posts: 230
Arduino rocks
|
 |
« Reply #4 on: August 10, 2011, 06:54:28 pm » |
Easy then - do that in setup()
G.
|
|
|
|
|
Logged
|
Is life really that serious...??!
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #5 on: August 11, 2011, 03:40:21 pm » |
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)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #6 on: August 11, 2011, 03:51:29 pm » |
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
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #7 on: August 13, 2011, 08:52:23 pm » |
So how the LED flash if it is not inside a loop function?
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #8 on: August 13, 2011, 09:13:31 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: August 13, 2011, 11:24:13 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #10 on: August 14, 2011, 08:55:34 am » |
Oh i didn't think of that 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #11 on: August 15, 2011, 08:02:20 pm » |
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
Full Member
Karma: 3
Posts: 230
Arduino rocks
|
 |
« Reply #12 on: August 16, 2011, 12:04:25 am » |
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
Newbie
Karma: 0
Posts: 40
|
 |
« Reply #13 on: August 16, 2011, 06:43:41 am » |
^ Oh I am not expecting you the write the code  I was just looking for examples or how to get started.
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Online
Edison Member
Karma: 24
Posts: 2347
|
 |
« Reply #14 on: August 16, 2011, 06:49:51 am » |
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
|
|
|
|
|
|