Passing a magnet by the reed switch increases the counter. Trying the same code on pin 4 (GPIO4) doesnt produce any result.
Code: Select all
static void IRAM_ATTR reedswitch_ISR(void* arg);
int reedswitchpin = 2;
uint32_t numberToggles;
volatile bool toggled = false;
unsigned long reed_time = 0;
unsigned long last_reed_time = 0;
static void setupinterrupts() {
pinMode(reedswitchpin, INPUT_PULLUP);
// Install the GPIO ISR service
gpio_install_isr_service(0); // You can use a different number (0, 1, 2) if needed
esp_err_t err = gpio_isr_handler_add((gpio_num_t)reedswitchpin, &reedswitch_ISR, (void*) NULL);
if (err != ESP_OK) {
Serial.printf("gpio_isr_handler_add failed (%x)", err);
}
gpio_set_intr_type((gpio_num_t)reedswitchpin, GPIO_INTR_NEGEDGE);
}
static void IRAM_ATTR reedswitch_ISR(void* arg) {
reed_time = millis();
if (reed_time - last_reed_time > 250) {
numberToggles++;
toggled = true;
last_reed_time = reed_time;
}
}
void setup() {
Serial.begin(115200);
delay(100);
setupinterrupts();
delay(100);
Serial.println("waiting...");
}
void loop() {
if (toggled) {
Serial.printf("reed has been toggled %u times\n", numberToggles);
Serial.println(" ");
}
toggled = false;
}