ESP-32 if logic is broken or i am broken

Gibbedy

Well-known member
Not a teensy but i need help on this.
I have one of those dev board aliexpress 38pin jobs. The controller is labled ESP-32. How is it possible that I get this terminal output (when the PE input is pulsed on and off:
Code:
deltaTime = 0
This should not be happening!
deltaTime = 0
This should not be happening!

edit: To save you some time, let me say, just look at where the print statements are made and then the if statement block that they are in, and the printout. The complete program is there so once you are hooked in you can go through it all and share in my frustration or put me on the correct path.
With this program (complete) compiled using arduino ide:
Code:
#define PE_INPUT 13

unsigned long peDebounceStartTime = 0; //millis when debounce timer was started
bool lastPeState = false;
bool currentPeState = false;
bool debounceStarted = false;
unsigned long deltaTime = 0;
unsigned long currentTime = 0;

void setup() {
  pinMode(PE_INPUT,INPUT_PULLUP);
  Serial.begin(115200);
}

bool readPeWithDebounce()
/*
 * Returns the debounced state of the Photo eye input.
 */
{
  bool peState = digitalRead(PE_INPUT);
  currentTime = millis();
 
  if (peState != lastPeState)  //We should debounce this
  {
    if (debounceStarted == false) // must be first loop since peState changed
    {
      debounceStarted = true;    
      peDebounceStartTime = currentTime;
    }
    else //Debounce has already started
    {
      deltaTime = currentTime - peDebounceStartTime;
      if(deltaTime > 1000)
      {
          if(deltaTime < 1000)
          {
            Serial.print("deltaTime is less than 1000 and has a value of:");
            Serial.println(deltaTime);
          }
          Serial.print("deltaTime = ");
          Serial.println(deltaTime);
          Serial.println("This should not be happening!");
        return peState;    
      }
    }  
  }
  else
  {
    debounceStarted = false;
    return lastPeState;
  }
}
void loop() {
  lastPeState = currentPeState;
  currentPeState = readPeWithDebounce();
}

To be clear, what i think i'm seeing is Serial.print(deltaTime) showing deltatime to be 0 from inside an if statement block where the condition requires deltatime to be greater than 1000.

In addition to that an if condition that will print in the case that deltaTime is less than 1000 is not being printed despite print showing that it is 0.

What's more, when performing the test I can clearly tell that the terminal is printing immediately when my photo eye input is made/not made. That is there is no 1000ms delay.

I'm thinking i have a wrong board selected or some compiler weirdness that i have created. Has anyone got any ideas. I've been at it for a few days.
Thanks.
Gavin
 
Last edited:
if(deltaTime < 1000) { Serial.print("deltaTime is less than 1000 and has a value of:"); Serial.println(deltaTime); }
this can't execute - needs to move out of current if(>1000) to an else. As written the print of '0' should not be there given the if(>1000) ???

Do a Ctrl+T and confirm the code indents right

There is an exit path from readPeWithDebounce() without a return value - check verbose output for warnings/errors.
 
Hello defragster.
All print statements are in there to make sense of what illogical situation is going on. The if condition is met by having a value of deltaTime > 1000, and yet it prints the deltaTime value as 0 inside the if's statement block... yet it doesn't print the nested if condition that checks if it is less than 1000.

It doesn't give me any compiler warnings with verbose compiler setting ticked. I fixed up my exit path without a return in any case.

The esp32 is running the code I've pasted. There is something strange going on.. I will work on stripping things down more until i can get to the cause.

edit: I think there is still an exit path without a return.. ASAP i will reprogram esp-32 and see if that was it...
 
Last edited:
defragster, Thankyou so very much.
Failing to return a value from a function was the cause of this situation. I should have come here sooner.
Thanks.
Gavin.
 
defragster, Thankyou so very much.
Failing to return a value from a function was the cause of this situation. I should have come here sooner.
Odd - usually the compiler notes those no return value exits? They should be an ERROR as the code it makes seems to shift the stack or something evil causing odd results or crashes.
Ctrl+T can be a helpful reformat for a misplaced ';' or braces - though I didn't see any - just that the indenting seemed odd and didn't explain how an impossible state was printed ⁉️

I know the bad no value return can be fatal. Was online with @shawn some time back and editing his ethernet code and made one bad return and chased CRASHING for about an hour before reading the code then seeing the warning :(
 
Missing any return from all execution paths is usually a warning. To make this specific warning an error, use "-Werror=return-type" in your compiler options.

May I suggest also adding "-Wall" to your build options? (Teensyduino includes that by default.) My other suggestion is to fix all warnings generated by a build. "Undefined behaviour" problems are too prevelant and are insidious.
 
I think this is an issue with the esp32 arduino build environment. It's caught me in the past.
Which given how painfully slow it is to build it would be nice if it at least caught such obvious errors.
 
A summary of this:
What happened:
Illogical runtime logic was occurring. An if statement block was being executed at runtime, where debugging print statements showed the conditions inside that statement block would mean the if condition should have evaluated to false.

What caused this:
I was calling a function (that contained the previously mentioned illogical if condition) that had a return type bool, which i wasn't using. The function had multiple chances to finish without returning a bool (or anything). This i have since learned can cause "undefined behavour" in c++.

Why no compile error:
Arduino IDE "File"-> "Preferences" - > "compiler wanrings" was set to "None"
I don't know when this was changed, but enabling that now gives me
Code:
error: control reaches end of non-void function
which would have saved me two days of torture. Note* i was still getting compiler errors with none, just not warnings.

How I haven't come accross this before:
I have never had this before. I write allot of code, most bad, very large confusing classes,functions, and i don't remember even seeing"control reaches end of non void function".
For 6 months i've been working on a python application and at some point I changed the way i return from function calls. Learning java in uni I was taught to always have one return statement, so every function was structured that way. I'm not sure why, but i stuck to that rule, until chatgpt convinced me that certain functions i had made (in python) would be easier to understand with multiple returns and I agreed. A few days ago i start a simple home automation type project and this happnes. Note* ChatGPT didn't help me solve this. Maybe it's not written about enough, but it's +1 to defragster/human for this.

Thank you all.
Gavin.
 
I think this is an issue with the esp32 arduino build environment. It's caught me in the past.
Which given how painfully slow it is to build it would be nice if it at least caught such obvious errors.
I tested and even with compiler warnings set to "none", a teensy4.1 gives you the warning at compile time where with esp32 dev module it doesn't so it's treated differently like you suggest.
 
Back
Top