• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Button interrupt

Q&A Forum › Category: ESP32 › Button interrupt
0 Vote Up Vote Down
Robert Barbirič asked 3 years ago

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

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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 
 
 
 

0 Vote Up Vote Down
Robert Barbirič answered 3 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.