Hello Sara & Rui,
Am working on a project and I need your help.
Am using your code for button and led
Remember Last GPIO State After RESET
But i need this button to act like interupt. It seems i cant get it work. Can you help me?
Best regards,
Robert
Hi.
I’m sorry for taking so long to get back to you.
We’re currently out of the office with limited access to the internet.
What code do you have so far? You can search for PIR motion sensor examples, and apply it to your pushbutton.
Can you share your code and explain what’s not working?
You may also consider using a pushbutton library. For example, this one is very easy to use: https://github.com/pololu/pushbutton-arduino
Regards,
Sara
Hi Sara,
Im using your example for for button to save the last state of the LED:
void IRAM_ATTR ButtonBheating() {
reading = digitalRead(ButtonHeat1);
if (reading != LastButtonHeat1State) {
lastDebounceTime = millis();
}
if ((millis() – lastDebounceTime) > debounceDelay) {
if (reading != ButtonHeat1State) {
ButtonHeat1State = reading;
if (ButtonHeat1State == HIGH) {
LedHeat1State = !LedHeat1State;
}
}
}
LastButtonHeat1State = reading;
if (digitalRead(LedHeat1) != LedHeat1State) {
Serial.println(“State changed Button Heat1”);
// change the LED state
digitalWrite(LedHeat1, LedHeat1State);
// save the LED state in flash memory
preferences1.putBool(“state”, LedHeat1State);
Serial.printf(“State saved Button Heat1: %d \n”, LedHeat1State);
}
}
The code works great, On button change it’s turns on or off the LED and remembers the last state.
But my code is a bit more longer and complicated and is connected to firebase , sending data and files to firebase. The problem is, when it sending files to firebase, the code “delays” for a few seconds, and then the button is not responding. I do not use delays in my code!
So I been trying to use Interupt on the specific button, but i can’t get it working, I get ESP32 to reset.
But when I try to comment out line;
preferences1.putBool(“state”, LedHeat1State);
it kind of work, but last state in not saved in next boot.
Can you help me.
Best regards
Hi.
What tasks does the ESP32 do on the interrupt callback function?
You can’t add complex tasks to the callback function or that will crash the ESP32.
Instead, you should try using flag variables that are changed on the callback function. Then, you should check the value of those variables in the loop() and proceed accordingly.
Can you show me your interrupt callback function?
Regards,
Sara