// https://github.com/espressif/arduino-esp32/issues/2894
/*@cnc4less
Author
cnc4less commented 29 minutes ago •
tried the esp task still not what it should if you have more than one interrupt, the example below
will show 3 ways to present the interrupts , one from the isr handle,, task and iar task notify, , if in the task or loop still it will just read it one time ONLY till i trigger the 2nd interrupt then rest the esp32 how to fix this problem.
*/
const byte interruptPin1 = 25;
const byte interruptPin2 = 33;
SemaphoreHandle_t syncSemaphore1;
SemaphoreHandle_t syncSemaphore2;
int nTasks=2;
void IRAM_ATTR handleInterrupt1() {
xSemaphoreGiveFromISR(syncSemaphore1, NULL);
// can't use Serial in interrupt
// Serial.println("Motion detected isr 1");
}
void IRAM_ATTR handleInterrupt2() {
xSemaphoreGiveFromISR(syncSemaphore2, NULL);
//Serial.println("Motion detected isr 2");
}
void producerTask( void * parameter )
{
/* Block for 1 second */
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
while( true) {
if(xSemaphoreTake(syncSemaphore1, xDelay)){
Serial.printf("Motion detected 11, %d pending\n",uxSemaphoreGetCount(syncSemaphore1));
}
else {
Serial.printf("no synch1 in last second\n");
}
}
vTaskDelete( NULL );
}
void consumerTask( void * parameter)
{
/* Block for 1 second */
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
while(true){
if(xSemaphoreTake(syncSemaphore2, xDelay)){
Serial.printf("Motion detected 22, %d pending\n",uxSemaphoreGetCount(syncSemaphore2));
}
else {
Serial.println(" no motion in last Second ");
}
}
vTaskDelete( NULL );
}
void setup() {
Serial.begin(115200);
syncSemaphore1 = xSemaphoreCreateCounting(1024,0); // Max 1024 before give will return fail, init at 0
syncSemaphore2 = xSemaphoreCreateCounting(1024,0);
pinMode(interruptPin1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin1), handleInterrupt1, RISING);
pinMode(interruptPin2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin2), handleInterrupt2, RISING);
xTaskCreate(
producerTask, // Task function.
"Producer", // String with name of task.
10000, // Stack size in words.
NULL, // Parameter passed as input of the task
1, // Priority of the task.
NULL); // Task handle.
xTaskCreate(
consumerTask, // Task function.
"Consumer", // String with name of task.
10000, // Stack size in words.
NULL, // Parameter passed as input of the task
1, // Priority of the task.
NULL); // Task handle.
Serial.println("Tasks launched and semaphore passed...");
}
void loop() {
delay(100);
unsigned long count =uxSemaphoreGetCount(syncSemaphore1);
if(count>0) {
Serial.printf("Pending Motion 1 =%d\n",count);
}
count =uxSemaphoreGetCount(syncSemaphore2);
if(count>0) {
Serial.printf("Pending Motion 2 =%d\n",count);
}
}Button on 33 debounce
Button on 25 wo debounce