0

I am trying to build a set of four stars using an Arduino, potentiometer and a four channel relay to control how fast it speeds across the stars. My question is how can I delay the process, say for 20 or 30 seconds before it starts again?

3

Use Delay( ). Or this code

long previousMillis;

void setup(){
  previousMillis = millis();//in void setup
  bool lS=LOW;//in void setup
}

void loop(){
  long currentMillis= millis();//in void loop

  if(currentMillis-previousMillis>=20000){
     if (lS==LOW){
       digitalWrite(pinNo,HIGH);
       lS=HIGH;
     }
     else
     {
       digitalWrite(pinNo,LOW);
       lS=LOW;
     }
     previousMillis= millis();
  }
  delay(1);
}

While this does not serve your purpose, it shows you an how to switch on and off and LED after every 20 sec without keeping the Arduino idle for 20 sec.You could improve on it and make your own code.

1
  • Thanks FuaZe , for the edit.It looks a lot more easy to understand now , though the whole code is exactly the same to the Arduino. Apr 16 '15 at 10:12
1

If you want to just provide a delay use this function

delay(ms) -> where the argument is an integer value that represents the time in milliseconds.

For example, 1s = 1000ms

If you wanna stop everything and wait 1s, just use like that:

delay(1000);

Note that the atmega controller will stop everything it is doing and wait the amount of time. If you need to count or wait for something while other things are occurring, use timer interrupts. So the counting process is done in background and your main loop continues to do its jobs.

0

delay():

delay(20000); // wait for 20 seconds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.