#define buttonPin 22
#define ledPin 2
SemaphoreHandle_t xSemaLED;
TickType_t btnDeounce = 0;
void IRAM_ATTR ISR() {
btnDeounce = xTaskGetTickCountFromISR();
xSemaphoreGiveFromISR(xSemaLED, NULL);
}
void setup() {
Serial.begin(115200);
xSemaLED = xSemaphoreCreateBinary();
xTaskCreate(flashLED, "Flash LED", 1024 * 4, NULL, 1, NULL);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(buttonPin, ISR, FALLING);
}
void flashLED(void *pvParam) {
pinMode(ledPin, OUTPUT);
while (1) {
xSemaphoreTake( xSemaLED, portMAX_DELAY);
if ((xTaskGetTickCount() - btnDeounce) < 100) {
if (buttonPin == HIGH) { // Nếu mà button bị nhấn
digitalWrite(ledPin,HIGH); // Đèn led sáng
} else { // ngược lại
digitalWrite(ledPin,LOW);
}
vTaskDelay(1000);
}
}
}
void loop() {}