Delay 1 line instead of whole loop

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
rudster
 
Posts: 9
Joined: Wed Sep 10, 2008 6:25 pm

Delay 1 line instead of whole loop

Post by rudster »

how can i delay a specific line instead of the whole loop?
thank you

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Post by mtbf0 »

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.

rudster
 
Posts: 9
Joined: Wed Sep 10, 2008 6:25 pm

Post by rudster »

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

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Post by mtbf0 »

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.

rudster
 
Posts: 9
Joined: Wed Sep 10, 2008 6:25 pm

Post by rudster »

thank you so much, got to finish my project :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”