Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Understanding and Implementing Timing Functions in Arduino Projects

Timing functions are crucial in many Arduino projects, allowing precise control over the timing of events and actions. These functions are essential for tasks such as blinking LEDs, controlling motors, and managing communication protocols. In the Arduino environment, timing functions such as delay(), millis(), and micros() are commonly used. This article will explore these functions, their importance, and how to effectively use them in your Arduino projects.

Project: In this example project, we will create a simple LED blinking system that demonstrates the use of timing functions. The objective is to blink an LED at different intervals using both the delay() function and the millis() function. This project will help you understand the differences between these functions and how to use them in various scenarios.

Components List:

  • Arduino Uno (1)
  • Breadboard (1)
  • LED (1)
  • 220-ohm resistor (1)
  • Jumper wires (several)

Examples:

  1. Using the delay() Function:
// Define the pin for the LED
const int ledPin = 13;

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Turn the LED on
  digitalWrite(ledPin, HIGH);
  // Wait for 1000 milliseconds (1 second)
  delay(1000);
  // Turn the LED off
  digitalWrite(ledPin, LOW);
  // Wait for 1000 milliseconds (1 second)
  delay(1000);
}

Explanation:

  • pinMode(ledPin, OUTPUT); sets the LED pin as an output.
  • digitalWrite(ledPin, HIGH); turns the LED on.
  • delay(1000); pauses the program for 1000 milliseconds.
  • digitalWrite(ledPin, LOW); turns the LED off.
  • delay(1000); pauses the program for another 1000 milliseconds, creating a blinking effect.
  1. Using the millis() Function:
// Define the pin for the LED
const int ledPin = 13;
// Variable to store the last time the LED was updated
unsigned long previousMillis = 0;
// Interval at which to blink (1000 milliseconds)
const long interval = 1000;

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Get the current time
  unsigned long currentMillis = millis();

  // Check if the interval has passed
  if (currentMillis - previousMillis >= interval) {
    // Save the last time the LED was updated
    previousMillis = currentMillis;

    // If the LED is off, turn it on, and vice-versa
    if (digitalRead(ledPin) == LOW) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
}

Explanation:

  • unsigned long previousMillis = 0; initializes a variable to store the last time the LED was updated.
  • const long interval = 1000; sets the interval at which to blink the LED.
  • unsigned long currentMillis = millis(); gets the current time in milliseconds.
  • if (currentMillis - previousMillis >= interval) checks if the interval has passed.
  • previousMillis = currentMillis; updates the last time the LED was updated.
  • if (digitalRead(ledPin) == LOW) checks the current state of the LED and toggles it.

Common Challenges:

  • Blocking Code: The delay() function blocks the execution of the program, which can be problematic in more complex projects. Using millis() allows for non-blocking code, enabling the execution of other tasks simultaneously.
  • Precision Timing: For high-precision timing, the micros() function can be used, which returns the number of microseconds since the Arduino started running the current program.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.