CIPAD 22-Leçon 21 part 2. La fonction Arduino millis()

3 min read 5 hours ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the use of the millis() function in Arduino programming, as covered in the video by Daniel Talbot. Understanding the millis() function is crucial for managing timing in your projects effectively, allowing for non-blocking code execution. This tutorial is especially relevant for beginners looking to enhance their Arduino programming skills.

Step 1: Understanding the millis() Function

The millis() function returns the number of milliseconds since the Arduino board began running the current program. This is useful for timing events without using delay, which can halt the execution of code.

  • Key Points:
    • millis() returns a long integer value.
    • It resets to zero after approximately 50 days of continuous operation.
    • Use millis() for timing tasks and creating delays without blocking code execution.

Step 2: Implementing millis() in a Simple Project

To see millis() in action, you can create a simple LED blinking project. Follow these steps:

  1. Setup the Hardware:

    • Connect an LED to a digital pin (e.g., pin 13) on the Arduino.
    • Connect a resistor (220 ohms) in series with the LED to prevent excess current.
  2. Write the Code:

    • Open the Arduino IDE and start a new sketch. Use the following code structure:
    const int ledPin = 13; // LED connected to digital pin 13
    unsigned long previousMillis = 0; // Store last time LED was updated
    const long interval = 1000; // Interval at which to blink (milliseconds)
    
    void setup() {
        pinMode(ledPin, OUTPUT); // Set the LED pin as output
    }
    
    void loop() {
        unsigned long currentMillis = millis(); // Get current time
    
        // Check if it's time to change the LED state
        if (currentMillis - previousMillis >= interval) {
            previousMillis = currentMillis; // Save the last time LED was updated
            
            // If the LED is on, turn it off, and vice versa
            int ledState = digitalRead(ledPin); // Read the current state
            digitalWrite(ledPin, !ledState); // Change the state
        }
    }
    
  • Explanation of the Code:
    • The previousMillis variable stores the last time the LED was toggled.
    • The interval variable defines how long the LED stays in each state (1 second in this case).
    • The loop checks the elapsed time since previousMillis and toggles the LED state accordingly.

Step 3: Testing and Observing Results

  1. Upload the Code:

    • Connect your Arduino to your computer and upload the sketch.
  2. Observe the LED:

    • The LED should blink on and off every second. If it doesn’t, check your connections and code for errors.

Conclusion

This tutorial introduced you to the millis() function and demonstrated its application in a simple LED blinking project. By understanding how to use millis(), you can create responsive Arduino projects that manage timing without blocking.

For your next steps, consider experimenting with different intervals or integrating more components into your project to enhance your learning experience.