Millis instead of delay

Hi there,

I’m new to Arduino, I want to use millis instead of delay, but I found it too complicated to understand the logic. I tried to to follow some topics about it but nothing worked for my sketch. Any help or response will be appreciated!

Here is my sketch:

#include <Servo.h>

Servo servo1;

Servo servo2;

Servo servohead;

void setup() {

 // put your setup code here, to run once:

servo1.attach(5);

servo2.attach(6);

servohead.attach(7);
}

void loop() {

 // put your main code here, to run repeatedly:

servo1.write(90);

servo2.write(90);

delay(1000);

servohead.write(90);

delay(1000);

servohead.write(120);

delay(1000);

servohead.write(90);

delay(1000);

servohead.write(60);

delay(1000);

servohead.write(90);

delay(1000);

servo1.write(random(0,180));//move forward or backward 3 or 5 second

servo2.write(90);

delay(random(3000,5000));//

servo1.write(90);//stop 3 or 5 second

servo2.write(90);

delay(random(3000,6000));//

servo1.write(random(30,150));//turn 3 second

servo2.write(random(0,180));

delay(3000);//

}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

The demo Several Things at a Time illustrates the use of millis() to manage timing.

The concept is actually quite simple and is the same way that you use the clock on the wall in your kitchen. When something starts you save the value of millis() (make a note of the time on the clock) and at regular intervals you compare the latest value of millis() (the current time on your clock) with the value you saved. If the right amount of time has passed you know to do whatever (switch off the LED or take the chicken from the oven).

…R

Hi,

Thank you for you respond and suggestion! I'll take a look at the demonstration code and wish to learn something from it.