ESP32 WROOM 32 crashes when exiting function

zapp123
Posts: 3
Joined: Mon Dec 11, 2023 2:44 pm

ESP32 WROOM 32 crashes when exiting function

Postby zapp123 » Sat Dec 23, 2023 6:29 pm

Hi there.

I have an ESP32 WROOM 32 devkit 1 and I am trying to use the Capacitive Touch sensors built into the ESP

I have some code which waits for touch to be detected, and once it is detected, it should return that the pad has been touched, instead it crashes and reboots.

Does anyone know why and what might be causing this behavior?

Code below

Code: Select all

#define Threshold 40

int touchPad;
bool touchDetected = false;
int flag = 0;
int i = 0;
                  
void setup() {
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("Booting");
  
  touchAttachInterrupt(T0, gotTouch0, Threshold);
  touchAttachInterrupt(T3, gotTouch3, Threshold);
  touchAttachInterrupt(T4, gotTouch4, Threshold);
  touchAttachInterrupt(T5, gotTouch5, Threshold);
  touchAttachInterrupt(T6, gotTouch6, Threshold);
  touchAttachInterrupt(T7, gotTouch7, Threshold);
  touchAttachInterrupt(T8, gotTouch8, Threshold);
  touchAttachInterrupt(T9, gotTouch9, Threshold);
}

void loop(){
  if(touchDetected){ //Check continuously if any touch is detected
    switch(touchPad){
      case 0  : Serial.println("Case 0 Selected"); delay(1000); break;
      case 3  : Serial.println("Case 3 Selected"); break;
      case 4  : Serial.println("Case 4 Selected"); break;
      case 5  : Serial.println("Case 5 Selected"); break;
      case 6  : Serial.println("Case 6 Selected"); break;
      case 7  : Serial.println("Case 7 Selected"); break;
      case 8  : Serial.println("Case 8 Selected"); break;
      case 9  : Serial.println("Case 9 Selected"); break;
      default : break;
      
      delay(1000);
    }
  }
}

void gotTouch0(){
 Serial.println("Got 0");
 touchDetected = true;
 Serial.println("Changed TouchDetected to True");
 touchPad = 0;
 Serial.println("Set TouchPad to True");
 delay(5000);
 Serial.println("Going Back");
 }

void gotTouch3(){
 Serial.println("Got 3");
 touchDetected = true;
 touchPad = 3;
 delay(500);
}

void gotTouch4(){
 Serial.println("Got 4");
 touchDetected = true;
 touchPad = 4;
 delay(500);
}

void gotTouch5(){
 Serial.println("Got 5");
 touchDetected = true;
 touchPad = 5;
 delay(500);
}

void gotTouch6(){
 Serial.println("Got 6");
 touchDetected = true;
 touchPad = 6;
 delay(500);
}

void gotTouch7(){
 Serial.println("Got 7");
 touchDetected = true;
 touchPad = 7;
 delay(500);
}

void gotTouch8(){
 Serial.println("Got 8");
 touchDetected = true;
 touchPad = 8;
 delay(500);
}

void gotTouch9(){
 Serial.println("Got 9");
 touchDetected = true;
 touchPad = 9;
 delay(500);
}

User avatar
Basalt
Posts: 28
Joined: Wed Aug 16, 2023 7:59 pm

Re: ESP32 WROOM 32 crashes when exiting function

Postby Basalt » Sun Dec 24, 2023 12:52 pm

I never used the Touch functions myself, but I do notice some issues with your code:
- using delay() in an interrupt function is generally a bad idea
- you don't reset touchDetected to false again

Have a look at some examples:
https://espressif-docs.readthedocs-host ... touch.html

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 WROOM 32 crashes when exiting function

Postby lbernstone » Sun Dec 24, 2023 5:31 pm

The delay in your loop is not in the right place. It should be at the very end of the loop in order to debounce the switches and allow other tasks cpu time.
If it is crashing with no error, then you probably have a power problem.

zapp123
Posts: 3
Joined: Mon Dec 11, 2023 2:44 pm

Re: ESP32 WROOM 32 crashes when exiting function

Postby zapp123 » Mon Dec 25, 2023 12:40 pm

Thank you guys! Issue was resolved by moving the delays. The flag change was necessary to reset and wait for another interrupt. it works fine now :)

Who is online

Users browsing this forum: No registered users and 39 guests