• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Code executation problem

Q&A Forum › Category: ESP32 › Code executation problem
0 Vote Up Vote Down
Bart asked 2 years ago

I (finally) discovered the reason one of my if statements refused to work.  I wanted to disclose it here in the hope it helps someone else as I spent literally hours tracking this down.
System:  Using VS Code / Platformio on linux (Arch) to develop a program to be run on an ESP32 device.
I want to control the outputs of three pins based on the inputs of three other pins.  I have a function that monitors the status of the three pins monitor pins named checkStatus() that returns a logical true/false and makes it’s decision based on the status of the three sensors.  This function works as desired.
In the main loop is where the problem showed up.  Actual code follows:

void loop()
{
    staticbool firstTime;

    dutyCycleLeft = BULBOFF;
    dutyCycleRight = BULBOFF;
    firstTime = true;

    while (true)
    {

    if (!checkStatus()) { break; }

    digitalWrite(BELL_PIN, LOW);
 
    // Turn on the left bulb to full brightness.
    while (dutyCycleLeft > BULBON)
    {
        ledcWrite(leftChannel, dutyCycleLeft);
        dutyCycleLeft–;
        delay(BULBDELAY);
    }
    delay(250);

    if (!checkStatus()) { break; }

    // Start to dim first bulb.
    while (dutyCycleLeft < BULBDIM)
    {
        ledcWrite(leftChannel, dutyCycleLeft);
        dutyCycleLeft++;
        delay(BULBDELAY);
    }
 
    // Finish dimming first bulb while starting to turn second bulb on.
    while (dutyCycleLeft < BULBOFF)
    {
        ledcWrite(leftChannel, dutyCycleLeft);
        dutyCycleLeft++;
        ledcWrite(rightChannel, dutyCycleRight);
        dutyCycleRight–;
        delay(BULBDELAY);
    }

    if (!checkStatus()) { break; }

THE REST OF THE CODE IS NOT NECESSARY TO THIS DISCUSSION

It continues with while statements with a check on the sensors after each one.
 
The problem:  I had written the checkStatus() line like this:
    if (!checkStatus()) break;
Which is legal C code.
I finally found that
    if (!checkStatus()) { break; }
works.
I still don’t know why it was only the first if statement that wouldn’t work while all the rest didn’t, but the fix is really easy to implement, so I use it.
As I said, I didn’t want someone else to go through the same process as I did. 

Question Tags: non functioning if statement
1 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Thank you so much for discussing this subject in such detail.
It may definetely be useful for other.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.