ESP32: ESP32_xSoftwareTimer.ino

File ESP32_xSoftwareTimer.ino, 1.4 KB (added by krit, 3 years ago)
Line 
1#include <Arduino.h>
2#include <freertos/timers.h>
3#define LED1  12
4#define LED2  17
5TimerHandle_t blinkTimer;
6TimerHandle_t serialTimer;
7void vBlink1Task(void * pvParameters){
8 
9  while(true){
10    digitalWrite(LED1, !digitalRead(LED1));
11    vTaskDelay(1000 / portTICK_PERIOD_MS);
12  }
13}
14void vBlink2Task(void *pvParameters){
15  while(true){
16    digitalWrite(LED2, !digitalRead(LED2));
17    vTaskDelay(100 / portTICK_PERIOD_MS);
18  }
19}
20void vBlinkTimerCallback( TimerHandle_t xTimer ){
21  uint32_t id = ( uint32_t )pvTimerGetTimerID(xTimer);
22  digitalWrite(2, LOW);
23  vTaskDelay(200 / portTICK_PERIOD_MS);
24  digitalWrite(2, HIGH);
25}
26void vSerialTimerCallack(TimerHandle_t xTimer){
27  Serial.println("Serial tick!");
28}
29
30void setup(){
31  Serial.begin(115200);
32  pinMode(2, OUTPUT);
33  pinMode(LED1, OUTPUT);
34  pinMode(LED2, OUTPUT);
35  xTaskCreate(vBlink1Task, "vBlink1Task", 1024, NULL, tskIDLE_PRIORITY, NULL);
36  xTaskCreate(vBlink2Task, "vBlink2Task", 1024, NULL, tskIDLE_PRIORITY+1, NULL);
37
38
39  blinkTimer = xTimerCreate("blinkTimer", 9000, pdTRUE, (void*)0, vBlinkTimerCallback);
40  serialTimer = xTimerCreate("serialTimer", 3000, pdTRUE, (void*)1, vSerialTimerCallack);
41
42  if (xTimerStart(blinkTimer, 100) != pdPASS ){
43    Serial.println("Start blink timer failed!");
44  }
45  if (xTimerStart(serialTimer, 100) != pdPASS ){
46    Serial.println("Start serial timer failed!");
47  }
48 
49}
50
51void loop(){ }