#include "esp_timer.h"
#define LED_PIN 2
static void oneshot_timer_callback(void* arg);
esp_timer_handle_t oneshot_timer;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Ready..");
pinMode(LED_PIN, OUTPUT);
//create timer parameters..
const esp_timer_create_args_t oneshot_timer_args = {
.callback = &oneshot_timer_callback, //link the call back
.arg = nullptr, //not passing in anything
.name = "one-shot" //nice name
};
//create timer, not running yet..
ESP_ERROR_CHECK(esp_timer_create(&oneshot_timer_args, &oneshot_timer));
//start our one shot timer for 1 second..
ESP_ERROR_CHECK(esp_timer_start_once(oneshot_timer, 1000000));
}
void loop() {
// put your main code here, to run repeatedly:
delay(10);
}
//call back for timer..
static void oneshot_timer_callback(void* arg)
{
int64_t time_since_boot = esp_timer_get_time();
Serial.printf("One-shot timer called, time since boot: %lld us\n", time_since_boot);
digitalWrite(LED_PIN, !digitalRead(LED_PIN));//blink on board led..
//now let's restart the timer..
//check state of timer and stop it if needed..
if (esp_timer_is_active(oneshot_timer)) {
ESP_ERROR_CHECK(esp_timer_stop(oneshot_timer));
}
//restart our one shot timer for 1 second..
ESP_ERROR_CHECK(esp_timer_start_once(oneshot_timer, 1000000));
}