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.