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

How to Implement Temporização in Arduino Projects

Temporização, or timing, is a crucial aspect of many Arduino projects. It allows you to control the timing of events, such as turning an LED on and off, reading sensor data at regular intervals, or managing communication protocols. In the Arduino environment, timing can be implemented using functions like delay(), millis(), and micros(). This article will explore how to use these functions to implement effective timing in your Arduino projects.

Examples:

  1. Using delay() for Simple Timing:

    The delay() function pauses the program for a specified number of milliseconds. This is useful for simple tasks where precise timing is not critical.

    void setup() {
     pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as an output
    }
    
    void loop() {
     digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
     delay(1000); // Wait for 1000 milliseconds (1 second)
     digitalWrite(LED_BUILTIN, LOW); // Turn the LED off
     delay(1000); // Wait for another second
    }
  2. Using millis() for Non-blocking Timing:

    Unlike delay(), the millis() function allows you to perform other tasks while waiting for a specific time to pass. This is known as non-blocking timing.

    unsigned long previousMillis = 0; // Store the last time the LED was updated
    const long interval = 1000; // Interval at which to blink (milliseconds)
    
    void setup() {
     pinMode(LED_BUILTIN, OUTPUT);
    }
    
    void loop() {
     unsigned long currentMillis = millis();
    
     if (currentMillis - previousMillis >= interval) {
       previousMillis = currentMillis; // Save the last time the LED was toggled
    
       // If the LED is off, turn it on, and vice versa
       if (digitalRead(LED_BUILTIN) == LOW) {
         digitalWrite(LED_BUILTIN, HIGH);
       } else {
         digitalWrite(LED_BUILTIN, LOW);
       }
     }
    }
  3. Using micros() for Microsecond Precision:

    For tasks requiring microsecond precision, use the micros() function. This function returns the number of microseconds since the Arduino board began running the current program.

    unsigned long previousMicros = 0;
    const long intervalMicros = 1000000; // 1 second in microseconds
    
    void setup() {
     pinMode(LED_BUILTIN, OUTPUT);
    }
    
    void loop() {
     unsigned long currentMicros = micros();
    
     if (currentMicros - previousMicros >= intervalMicros) {
       previousMicros = currentMicros;
    
       if (digitalRead(LED_BUILTIN) == LOW) {
         digitalWrite(LED_BUILTIN, HIGH);
       } else {
         digitalWrite(LED_BUILTIN, LOW);
       }
     }
    }

These examples demonstrate how to implement timing in Arduino projects using different functions based on your precision needs. Understanding and using these functions effectively can greatly enhance the functionality and efficiency of your Arduino applications.

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.