Lesson 3 - Understanding long Data Type
Learn when to use the long data type for larger whole numbers such as time values from millis() and big counters in ESP32 projects.
Progress indicator
Lesson 3 of 4
Learning Objectives
- Understand that long stores whole numbers and can hold larger values than int.
- Learn why long is commonly used with millis() timing in Arduino/ESP32 code.
- Read a beginner timer example that checks elapsed time.
- Explain the difference between current time, start time, and interval values.
- Avoid common beginner mistakes when working with larger time numbers.
Concept Explanation
What is long in Arduino?
In Arduino and ESP32 sketches, long is a whole-number data type like int, but it can store much larger values. This is helpful when your program needs to count for a long time or track large time values.
What kind of values can long store?
A long stores whole numbers (positive or negative) and is useful when the value may become bigger than a normal int can safely hold in your lesson examples. Think of values that keep growing: elapsed milliseconds, total button presses, or long-running counters.
Why use long with millis()?
The millis() function returns a time value that keeps increasing while the board runs. Because this value can become large, beginners are usually taught to store it in a long (or laterunsigned long) variable.
Beginner Tip
In many Arduino examples you will later see unsigned long with millis(). For this lesson, we focus on long first so you can clearly understand the timing idea without too many new concepts at once.
Real example: start time
long startTime = millis(); stores the moment a timer starts.
Real example: interval
long intervalMs = 2000; stores a reusable 2-second interval.
int vs long (Beginner Comparison)
Both int and long store whole numbers. The main difference is that long is used when you expect bigger values.
| Type | Good for | Example |
|---|---|---|
| int | Small/medium whole numbers used often in simple logic | int ledPin = 2; |
| long | Larger counters and elapsed time values (like millis timing) | long startTime = millis(); |
Code Example
This example uses long variables to check when 2 seconds have passed.
long startTime = 0;
long intervalMs = 2000;
void setup() {
Serial.begin(115200);
startTime = millis();
}
void loop() {
long currentTime = millis();
if (currentTime - startTime >= intervalMs) {
Serial.println("2 seconds passed");
startTime = currentTime;
}
}Code Explanation (Step-by-Step)
- long startTime stores the timer start moment (the last time we started counting).
- long intervalMs stores the waiting interval (2000 ms = 2 seconds).
- In setup(), Serial.begin(115200) starts the Serial Monitor connection.
- In setup(), startTime = millis() saves the first timestamp so the timer has a starting point.
- Inside loop(), currentTime = millis() reads the current running time of the board.
- currentTime - startTime calculates the elapsed time since the last reset point.
- If elapsed time is greater than or equal to intervalMs, the code prints a message.
- After printing, startTime = currentTime resets the timer so the next 2-second wait can begin.
Common Beginner Mistakes
Mistake 1: Resetting startTime too early
If you set startTime = millis() every loop before the check, elapsed time never grows enough to reach the interval.
Mistake 2: Confusing seconds and milliseconds
2000 means 2000 ms, which is 2 seconds. Beginners often expect it to mean 2000 seconds.
Practice Task
- Change
intervalMsfrom2000to5000and explain what changes in the print timing. - Rename
startTimetolastPrintTimeand explain why the new name is clearer. - Add another long variable like
long totalPrints = 0;and describe how you would increase it each time the message prints.
Try it now
Open the simulator workspace to practice changing timing values and observe how variables update while stepping through code.