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();
}
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.
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.
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
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
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