If statement makes ESP8266 reboot

Hi

When I run if statement the MCU reset the second I press the button

I have tried

unsigned long startTime = millis();

and

int startTime = millis();

Same result

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Soft WDT reset

Exception (4):
epc1=0x40201a46 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffe40 end: 3fffffd0 offset: 0160
3fffffa0:  3fffdad0 00000000 3ffeeedc 3ffeef08  
3fffffb0:  3fffdad0 00000000 3ffeeedc 4020450c  
3fffffc0:  feefeffe feefeffe 3fffdab0 40100d95  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Code:

int switchTare       = D1;    // select the input pin for the switch
int switchTareValue  = 0;

Void loop()

  if (digitalRead(switchTare) == HIGH) {
    int startTime = millis();
      while (digitalRead(switchTare) == HIGH) {
      if (millis() - startTime > 7000) {              
         //tare();                                  // call function tare
        Serial.println("Calibrate millis");
        break;
      }
    }
  }

Any idea why mcu crash?

you are blocking the code with your while loop
add a yield(); to give the esp a chance to perform it's background tasks.

If you don't know how - make a full compileable example we can put in the IDE and make it run on our devices.

1 Like

Thanks!!!

This code compiles for me

//card node mcu 1.0

const int switchTare = D2;

void setup() {
  pinMode(switchTare, INPUT);
}

void loop() {
  if (digitalRead(switchTare) == HIGH) {
    int startTime = millis();
    while (digitalRead(switchTare) == HIGH) {
      if (millis() - startTime > 7000) {              // tid knappen må holdes nede før komando kjøres
        //functionTare();                               // call function tare
        Serial.println("tare mills");
        break;
      }
    }
  }
}

add a yield()


const int switchTare = D2;

void setup() {
  pinMode(switchTare, INPUT);
}

void loop() {
  if (digitalRead(switchTare) == HIGH) {
    int startTime = millis();
    while (digitalRead(switchTare) == HIGH) {
      if (millis() - startTime > 7000) {              // tid knappen må holdes nede før komando kjøres
        //functionTare();                               // call function tare
        Serial.println("tare mills");
        break;
      }
      yield();
    }
  }
}

doesn't throw an exception any more.
Doesn't print neither because you missed a Serial.begin();

1 Like

Thanks, I did not have a qlue about the yield() function.

But you did, and it worked!!!
:slight_smile:

SOLUTION as a function
(update 2)

I made this like a function because I have 4 switches (read relays).
tare
calibrate
alarm
reset

All 4 is functions, all switches except alarm is NO

Example

int switchTare       = D1;    // select the input pin for the switch
int switchTareValue  = 0;     // variable to store the value coming from the sensor

void setup(){
pinMode(switchTare, INPUT);
}

void loop() {

// check if switchTare is pressed
  if (digitalRead(switchTare) == HIGH) {
       buttonWait(switchTare, buttonPressTime, functionTare, HIGH);    // wait for HIGH state
  }


//Function

void buttonWait(int switchPin, int waitTime, void (*function)(), bool state) {  // true for HIGH, false for LOW
  pinMode(switchPin, INPUT);
  if (digitalRead(switchPin) == state) {
    int startTime = millis();
    while (digitalRead(switchPin) == state) {
      if (millis() - startTime > waitTime) {
        (*function)();  // call the function pointed to by the function pointer
        Serial.println("tare mills");
        break;
      }
      yield(); // allow other tasks to be executed
    }
  }
}

it's up to you but
a) I suspect your sketch will not compile - {} are not correct, missing variables, ...
b) your code is still blocking. Do a refactoring of your code and get rid off your while

may be you should google for "Finite State Machine" and apply that to your program.

1 Like

Hi @noiasca thanks for helping me.

The code is part of a bigger project so there can be missing parts in my function sample.
I have built a bee monitor, 150 kg scale (with auto calibration), temp, humidity sent over LoRa to a receiver (ttgo lora oled) that again sends data to cayenne and to a web server.

My sketch it compiles and when I press button for 5 sec the function is running and then goes back to the void loop.

I'm not an expert, a bit google, a bit old basic and fortran 30-40 years ago and a skilled son. And I have time, started on this code 1 year ago :wink:

I'm sure the code can be optimized, but that is a level I'm not capable off.

I'm happy as long as it runs, what it does per today!

If you have specifics in code I appreciate your effort to help me further.

M

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.