Last Updated: 24/09/2021
Lesson 7 - delay() and millis()
Lessons >> Basics >> Lesson 7 - delay() and millis()
Lesson Contents
Controlling timing of programs
Using the functions:
delay()
millis()
random()
Understanding how changing delay() effects the timing of other parts of the program
Understanding how millis() allows timing of a section of code to change without effecting other parts of the program.
Generating a random number using the random() function
Using an unsigned long for storing time.
Running multiple timers at once.
Adjusting the start time of a timer
Example 1: delayMillisv1.ino
Click to Download code:delayMillisv1.ino
Timing 3 items with delay()
/* 23/07/21
* delayMillisv1
* Basic delay script
*/
int q;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("delayMillisv1...");
//this will print a blank line
Serial.println(" ");
}
void loop(){
Serial.println("1st Item");
//Delay stops all Arduino Activity for 2000 milliseconds (2 seconds)
delay(2000);
Serial.println("2nd Item");
//Delay stops all Arduino Activity for 2000 milliseconds (2 second)
delay(2000);
Serial.println("3rd Item");
//Delay stops all Arduino Activity for 2000 milliseconds (2 second)
delay(2000);
}
Example 2: delayMillisv2.ino
Click to Download code:delayMillisv2.ino
Errors caused by code running and alteration in other delay()'s .
Using the function random() to generate a random number.
/* 23/07/21
* delayMillisv2
* Problems of keeping exact time when work is introduced.
*/
int q;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("delayMillisv2...");
//this will print a blank line
Serial.println(" ");
}
void loop(){
Serial.println("1st Item");
for(q=0;q<50;q++){
Serial.println(q);
}
//Delay stops all Arduino Activity for 2000 milliseconds (2 seconds)
delay(2000);
Serial.println("2nd Item");
//delay is now a random number between 500 and 2500
delay(random(500,2500));
//Delay stops all Arduino Activity for 2000 milliseconds (2 second)
//delay(2000);
Serial.println("3rd Item");
//Delay stops all Arduino Activity for 2000 milliseconds (2 second)
delay(2000);
}
Example 3: delayMillisv3.ino
Click to Download code:delayMillisv3.ino
introducing millis() to control three timings.
Altering the start time of a timer by giving the unsigned long an initial value.
/* 23/07/21
* delayMillisv3
* Basics of millis().
*/
int q;
unsigned long timer1 = 0;
unsigned long timer2 = 2000;
unsigned long timer3 = 4000;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("delayMillisv3...");
//this will print a blank line
Serial.println(" ");
}
void loop(){
if(millis() > timer1){
//take the current time and add 6 seconds (6000);
timer1 = millis() + 6000;
Serial.println("1st Item");
}
if(millis() > timer2){
//take the current time and add 6 seconds (6000);
timer2 = millis() + 6000;
Serial.println("2nd Item");
}
if(millis() > timer3){
//take the current time and add 6 seconds (6000);
timer3 = millis() + 6000;
Serial.println("3rd Item");
}
}
Example 4: delayMillisv4.ino
Click to Download code:delayMillisv4.ino
Three timers running at different timings without effecting each other.
Using random() to generate a random number
/* 23/07/21
* delayMillisv4
* Using millis() to control 3 timers with different timings.
*/
int q;
unsigned long timer1 = 0;
unsigned long timer2 = 2000;
unsigned long timer3 = 4000;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("delayMillisv4...");
//this will print a blank line
Serial.println(" ");
}
void loop(){
if(millis() > timer1){
//take the current time and add 6 seconds (6000);
timer1 = millis() + 6000;
Serial.println("1st Item");
for(q=0;q<100;q++){
Serial.println(q);
}
}
//if the value of millis is above the timer value
if(millis() > timer2){
//take the current time and add 3.5 seconds (3500) to reset the timer
timer2 = millis() + 3500;
Serial.println("2nd Item");
}
if(millis() > timer3){
//take the current time and add 2 to 8 seconds (2000 - 8000);
timer3 = millis() + random(2000,8000);;
Serial.println("3rd Item");
}
}
Additional Resource Links
There is a new example sketch on the tone() function page integrating a tone() with a flashing LED.
Don't forget to use the Reference in you Arduino IDE and look at millis() for more help on this lesson.
Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Lesson 7 - delay() and millis() as a reference.