Esp32 long press button and millis problem

Hello, I have a problem with my sketch, it works fine with arduino boards but I am not able to make it work on an esp32. The esp32 keeps crashing, I suspect the problem is the millis() function and that I could replace it with esp_timer_get_time() but I was not able to do. Can you help me I am a novice.

Thank you

#include <Servo.h> 
Servo myservo;

static const int buttonPin = 2;                    
const int led = 6; 
const int limitSwitch = 8; 
const int relay = 4;
const int intervalButton = 50;                    
int buttonStatePrevious = HIGH;                   
int ledStatePrevious = LOW;

unsigned long minButtonLongPressDuration = 2000;   
unsigned long ledDuration = 10000;    
unsigned long relayDuration = 2000;              
unsigned long servoDuration = 2000;   
unsigned long limitSwitchMillis;              
unsigned long servoMillis;   
unsigned long relayMillis;   
unsigned long buttonLongPressMillis;               
unsigned long previousButtonMillis;               
unsigned long buttonPressDuration;          

bool buttonStateLongPress = false;                
bool ledStateLongPress = false;
bool relayState = false;
bool servoState = false;

unsigned long currentMillis;          

void setup() {
  Serial.begin(115200);               

  pinMode(buttonPin, INPUT_PULLUP);       
  pinMode(limitSwitch, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  pinMode(relay, OUTPUT);
  Serial.println("Press button");
  digitalWrite(relay, HIGH);

}

void readButtonState() {


  if(currentMillis - previousButtonMillis > intervalButton) {
    
    int buttonState = digitalRead(buttonPin);    
    int limit = digitalRead(limitSwitch);   

     
    if (limit == LOW ){
      digitalWrite(led, LOW);
      ledStateLongPress = false;
      limitSwitchMillis = currentMillis;
    } 
    if (limit == HIGH && !ledStateLongPress ){
      digitalWrite(led, HIGH);
    } 
    
    if (limit == HIGH &&  !ledStateLongPress && currentMillis - limitSwitchMillis >= ledDuration){    
      Serial.println("4 seconds");
      ledStateLongPress = true;
      digitalWrite(led, LOW);
    }

    if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
      buttonLongPressMillis = currentMillis;
      buttonStatePrevious = HIGH;
      Serial.println("Button pressed");
      digitalWrite(relay, LOW);
    }

    buttonPressDuration = currentMillis - buttonLongPressMillis;

    if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
      buttonStateLongPress = true;
      relayState = false;   
      Serial.println("Button long pressed");
      myservo.attach(7);
      myservo.write(0);  // set servo to on
      relayMillis = currentMillis;

  }
      if (!relayState && currentMillis - relayMillis >= relayDuration) {
      Serial.println("Relay Off");
      digitalWrite(relay, HIGH);
      myservo.detach();
      relayState = true;
      }
      
    if (buttonState == LOW && buttonStatePrevious == HIGH) {
      buttonStatePrevious = LOW;
      buttonStateLongPress = false;
      Serial.println("Button released");
      digitalWrite(relay, HIGH);
      myservo.attach(7);
      myservo.write(150);  // set servo off
      servoMillis = currentMillis;
      servoState = false;
      }
      
      if (!servoState && currentMillis - servoMillis >= servoDuration) {
      Serial.println("Servo OFF");
      myservo.detach();
      servoState = true;
      
      
      
      if (buttonPressDuration < minButtonLongPressDuration) {
        Serial.println("Button pressed shortly");
      
      }
    }
           
    previousButtonMillis = currentMillis;

 }
 }

void loop() {

  currentMillis = millis();    
  readButtonState();           
  
}

what's the crash log saying?

It repaets this message

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
E (128) psram: PSRAM ID read error: 0xffffffff
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4008119d  PS      : 0x00060330  A0      : 0x800d0deb  A1      : 0x3ffb1f60  
A2      : 0x00000004  A3      : 0x00000002  A4      : 0x0800001c  A5      : 0x00000003  
A6      : 0x00000003  A7      : 0x00000000  A8      : 0x3f400384  A9      : 0x00000000  
A10     : 0xffffffff  A11     : 0x00000048  A12     : 0x08000000  A13     : 0x4a000000  
A14     : 0xffffffff  A15     : 0x00000000  SAR     : 0x0000001a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0xffffffff  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x4008119d:0x3ffb1f60 0x400d0de8:0x3ffb1f80 0x400d1f92:0x3ffb1fb0 0x40089c91:0x3ffb1fd0

Rebooting...
ets Jun  8 2016 00:22:57


The issue you are getting, is it a secret or will you post the issue? Have you placed the exception the esp is producing into the ESP Exception Decoder that you installed into the Arduino IDE? Could you post the Exception results?

Write more about this relay and it's connection to the ESP. Aw heck show a schematic and a few images of the wired, powered project not properly working.

That's an issue. The Exception decoder results?

Oi! BOINK on me head it does!

Use the ESP32 servo library.

I have tried the ESP32 servo library but it didn't matter (ESP32Servo.h)

With a push of a button the circuit turn the servo a certain amount and activate the relay, a few seconds later it cuts the relay and servo. There is also a switch (called limit) that turn on or off an LED

100% the Servo library does not work with the ESP32. Continued insistence on the use of the Servo library with the ESP32 will result in the issue NOT being solved.

Post your wiring diagram and images of your project.

Post images of your project.

I would gladly post images but the servo and switches are installed on my electric bike, and I can't remove it. The purpose of this program is to switch on or off a circuit breaker with the servo and before opening the circuit breaker it turn a relay on to pass the current through a 5W resistor to prevent current surge (it acts as a antispark device). When the bike is powered off (push button depressed) the battery is completely cut off from the controller as a safety measure. As I said the circuit worked perfectly with and arduino uno, and pro mini)

they are 5V devices and pins can possibly provide more current. They might be better protected against current surge or other stuff... it's just not the same board

Is the crash happening when you use the system connected to the bike only (ie if you don't drive anything, will it crash ?)

The current surge (antispark) board is completely isolated from the esp32 the relay is activated with an optocoupler, It worked perfectly on the bike with the arduino. The purpose of this system is to automate several safety measures. When the switch is turned on the relay lets the current pass through a 5W resistor for a few seconds then the servo turn the circuit breaker which allow the current to pass through the circuit breaker and the relay turn off which bypasses the antispark board (5W resistor). The antispark board is turned off during normal operation of the bike

still would be good to know if you see the crash when nothing is connected

The exception decoder

PC: 0x400811a1: __pinMode at C:\Users\*****\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32\esp32-hal-gpio.c line 115
EXCVADDR: 0xffffffff

Decoding stack results
0x400811a1: __pinMode at C:\Users\*****\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32\esp32-hal-gpio.c line 115
0x400d0e68: setup() at C:\Users\*****\Desktop\antispark_led_nodelay/antispark_led_nodelay.ino line 41
0x400d2916: loopTask(void*) at C:\Users\****\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32\main.cpp line 18
0x40089d79: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

As long as you insist on using the Servo library with an ESP32 you will NOT resolve this issue. Understand?

Post the code where you are using the ESP32Servo library.

Post pics of your wiring and project. Post a schematic.

that likely means that you have a pointer in the code that is not correct.

I am working on it

Here is a crude schematic of the project don't pay attention to the pins used on the esp32 I will choose them afterwards

You CANNOT power a servo from the ESP32's 5V output.

A simple plastic geared servo draws about 800mA stall current, the ESP32's regulator cannot supply that current.

The GPIO pins used are highly relevant with and ESP32.

The ESP32's GPIO cannot supply LED power without using a current limiting resistor for long.

Sir I know that, there is an external 5v power supply with the ground connected to the esp32, I also know about the LED resistor and pull up/pull down resistors, as I said this is a crude schematic not detailed, for the esp32 pins I will use these ones

the image is captured from a youtube video from the channel (The Hook Up)