how can i delay a specific line instead of the whole loop?
thank you
Delay 1 line instead of whole loop
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
huh?
any time you use a delay you're just putting the arduino into a loop counting cycles and nothing more. everything stops.
if you want the loop to continue running and to have something happen periodically, the easiest thing to do is to use the millis function to get the elapsed time since the sketch started.
give us a code snippet and an idea of what you want to have happen.
any time you use a delay you're just putting the arduino into a loop counting cycles and nothing more. everything stops.
if you want the loop to continue running and to have something happen periodically, the easiest thing to do is to use the millis function to get the elapsed time since the sketch started.
give us a code snippet and an idea of what you want to have happen.
-
- Posts: 9
- Joined: Wed Sep 10, 2008 6:25 pm
Code: Select all
void loop() // run over and over again
{
digitalWrite (Red1, HIGH); // Red1 ON
digitalWrite (Yellow1, LOW); // Yellow1 OFF
digitalWrite (Green1, LOW); // Green1 OFF
digitalWrite (Red2, LOW); // Red2 OFF
digitalWrite (Green2, HIGH); // Green2 ON
do
{
digitalWrite (Blue4, HIGH);
digitalWrite (Blue3, LOW);
digitalWrite (Blue2, LOW);
delay(200);
digitalWrite (Blue4, LOW);
digitalWrite (Blue3, HIGH);
digitalWrite (Blue2, LOW);
delay(200);
digitalWrite (Blue4, LOW);
digitalWrite (Blue3, LOW);
digitalWrite (Blue2, HIGH);
delay(200);
}while (Green2==HIGH);
delay (5000)
What i want to happen is when Green2 is HIGH, i want blue4,3,2 to switch lighting continuously while Green2 is on for 5 seconds
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
what's going to happen there is that your loop is going to repeat until Green2 goes low which is never going to happen because you don't change it in the loop. then after a period equal to forever, the code will execute a 5 second wait.
notice that each iteration will take about 600ms, so the easiest thing to do would be to use a for loop and have it execute 8 times which would give you 4.8 seconds instead of 5 if that's copacetic.
or you could change the 200ms delays to 333 ms and run the for loop 15 times. or use 167ms delays and repeat 30 times.
or 211 ms delays and 18 iterations.
those are the easy solutions.
notice that each iteration will take about 600ms, so the easiest thing to do would be to use a for loop and have it execute 8 times which would give you 4.8 seconds instead of 5 if that's copacetic.
or you could change the 200ms delays to 333 ms and run the for loop 15 times. or use 167ms delays and repeat 30 times.
or 211 ms delays and 18 iterations.
those are the easy solutions.
Please be positive and constructive with your questions and comments.