#include <Arduino.h>
// Handle for Timer 1 and Timer 2
TimerHandle_t timer1;
TimerHandle_t timer2;
// Counter to track how many times Timer 2 has triggered
volatile int timer2Counter = 0;
// Function prototypes
void timer1Callback(TimerHandle_t xTimer);
void timer2Callback(TimerHandle_t xTimer);
void setup() {
Serial.begin(115200);
while (!Serial) {
// Wait for Serial to be ready (in case of USB Serial)
}
delay(1000); // Give some time to the serial monitor to start
// Create Timer 1 (One-Shot Timer)
timer1 = xTimerCreate(
"Timer1", // Name of the timer
pdMS_TO_TICKS(10), // Timer period in ticks (1 second)
pdFALSE, // Auto-reload (pdFALSE means one-shot)
(void *)0, // Timer ID
timer1Callback // Callback function
);
// Create Timer 2 (Auto-Reload Timer)
timer2 = xTimerCreate(
"Timer2", // Name of the timer
pdMS_TO_TICKS(200), // Timer period in ticks (200 milliseconds)
pdTRUE, // Auto-reload
(void *)0, // Timer ID
timer2Callback // Callback function
);
// Start Timer 2
if (xTimerStart(timer2, 0) != pdPASS) {
Serial.println("Failed to start Timer 2");
}
}
void loop() {
// No code needed here, timers handle everything
}
void timer1Callback(TimerHandle_t xTimer) {
// Print the message from Timer 1
Serial.println("Timer 1: This prints after 5 times Timer 2 has printed.");
// Reset the Timer 2 counter
timer2Counter = 0;
}
void timer2Callback(TimerHandle_t xTimer) {
// Increment the counter each time Timer 2 triggers
timer2Counter++;
Serial.println("Timer 2: This prints every time.");
// Check if Timer 2 has triggered 5 times
if (timer2Counter >= 5) {
// Start Timer 1
if (xTimerStart(timer1, 0) != pdPASS) {
Serial.println("Failed to start Timer 1");
}
}
}