Search Code

Lesson 14/31-1: Arduino Millis (no delay) and map functions | SunFounder Robojax

Lesson 14/31-1: Arduino Millis (no delay) and map functions | SunFounder Robojax

This guide introduces the Arduino millis() function, a fundamental tool for time-based projects that require non-blocking code execution. Unlike delay(), which halts the entire program, millis() tracks elapsed time since the board started, allowing your Arduino to multitask effectively. You can use this technique to build:

  • Non-blocking LED blinkers that run while other code executes.
  • Timed events triggered by sensors or buttons, such as a light that turns off 5 seconds after a button press.
  • Simple data logging systems that record sensor values at regular intervals.
  • User interface systems that respond to inputs without freezing.
Sunfounder car assembled

Understanding the millis() Function

The millis() function is a built-in Arduino command that returns the number of milliseconds since the Arduino board began running the current program. It starts at zero on power-up and increments every millisecond. You can call this function at any point in your code to get the current "uptime" value. For example, after 1 second, it returns 1000; after 5 seconds, it returns 5000. The key advantage is that it doesn't pause your code, so you can check the time while other tasks continue to run.

Why Avoid delay()?

The delay() function is a simple way to pause a program, but it is a blocking function. When you call delay(5000), the Arduino stops executing all other code for 5 seconds, freezing any connected components. This is problematic for projects that need to respond to inputs or update outputs continuously. Using millis() allows you to check if a certain amount of time has passed without stopping the rest of your program, making it ideal for multitasking.

Hardware Required

  • Arduino board (Uno, Mega, etc.)
  • USB cable for programming and power

Wiring Guide

This example requires no external wiring. The Arduino only needs to be connected to your computer via USB to upload the code and view the serial monitor output.

Code Explanation

This first example is a simple demonstration of how millis() works. The code initializes the serial monitor and prints the value of millis() in an infinite loop.

void setup() {
  Serial.begin(9600);//initialize serial monitor
  Serial.println("Introduction to millis");
  delay(2000);
}

void loop() {
  Serial.println(millis());
}

The setup() function starts serial communication at 9600 baud and prints a welcome message. The delay(2000) here is only for demonstration to show that millis() starts counting from the moment the program runs. The loop() function continuously prints the current value of millis(). When you open the Serial Monitor, you will see the time in milliseconds rapidly increasing. This illustrates that millis() is a counter that never stops, providing a continuous time reference for your program.

This is the fundamental building block. In the video, the instructor demonstrates how to use this value to trigger actions after a set period, like checking if 5000 milliseconds (5 seconds) have passed before executing a specific code block. This is done by storing the current millis() value in a variable and then comparing it in the main loop.

Driving an LED with a percentage value using the map() function is covered in Lesson 14/31-4: Arduino coding map() example 1.

Live Project Demonstration

In the video, the instructor uploads this code and opens the Serial Monitor. You can see the output starts after the initial 2-second delay, showing a value near 1999. The numbers then increase rapidly, demonstrating the continuous count of milliseconds. This live view confirms that the function is working and provides a clear visual understanding of how time is tracked.

Related projects from this video

Chapters

  • [00:00] Introduction to millis()
  • [01:19] What is millis() and why use it
  • [03:06] Practical demonstration of millis()
  • [05:46] Using millis() to trigger a timed event
  • [08:29] Example with a push button
  • [12:14] Advanced push button timing example
  • [17:20] Introduction to the map() function
  • [19:47] Code example for map() with an LED
  • [22:41] Creating a custom map function

Images

Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
916-Lesson 14/31-1: Arduino coding millis() example 1
Language: C++
/*
Lesson 14/31: Arduino coding millis() example 1
Get this code and watch video Download and resource page https://robojax.com/RJT597
YouTube video https://www.youtube.com/watch?v=lqgzQohNiIM
 
  
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
/*
 * Example-1 of using millis() with Arduino
 * Written by Ahmad Shamshiri on July 27, 2019
 * in Ajax, Ontario, Canada
 * Watch video instructio for millis() :https://youtu.be/u2HsiTS8niQ
 */


void setup() {
  Serial.begin(9600);//initialize serial monitor
  Serial.println("Introduction to millis");
  delay(2000);
  


}

void loop() {

  Serial.println(millis());

}// loop end

Resources & references

Files📁

Arduino Libraries (zip)