Timer Interrupt (Sampling)

// ESP32_Timer_Interrupt_1.ino

#define LED1 LED_BUILTIN

hw_timer_t *timer = NULL;

void onTimer(){
digitalWrite(LED1, !digitalRead(LED1));
}

void setup() {
pinMode(LED1, OUTPUT);

//count up (true) or down (false). 80 = prescale
//timerN = 0…3
//timerBegin(timerN, prescale, up/down);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 5000000, true);
timerAlarmEnable(timer);
}

void loop() {
}

//############################ // ESP32_Timer_Interrupt_1.ino #############

 

// ESP32_Timer_Interrupt_2_TwoTimer.ino

#define LED1 LED_BUILTIN
#define LED2 2

hw_timer_t *timer = NULL;
hw_timer_t *timer2 = NULL;

void onTimer(){
digitalWrite(LED1, !digitalRead(LED1));
}

void onTimer2(){
digitalWrite(LED2, !digitalRead(LED2));
}

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

//count up (true) or down (false). 80 = prescale
//timerN = 0…3
//timerBegin(timerN, prescale, up/down);
timer = timerBegin(0, 80, true);
timer2 = timerBegin(1, 80, true);

timerAttachInterrupt(timer, &onTimer, true);
timerAttachInterrupt(timer2, &onTimer2, true);

timerAlarmWrite(timer, 5000000, true);
timerAlarmWrite(timer2, 500000, true);

timerAlarmEnable(timer);
timerAlarmEnable(timer2);
}

void loop() {
}

 

//##################### ESP32_Timer_Interrupt_2_TwoTimer.ino ##############