Problem fixed. Wondering why it occurred

Using VS code and Platformio to create program for ESP32, but like the attitude of the members of this forum. It's about the if command in C language.

It is my understanding that the if statement will process only the first following command unless curly braces are used to group multiple commands.

example:

if (condition) do something;
If more than one command is to be processed, they must be inside brackets:

[code] if (condition){
do this;
do that;
}

I had a situation where, inside a while loop, I wrote if (occupied()) break; as the first line in this while loop. occupied is a function that checks sensors and returns true of false. It is called several times inside this loop and it refused to be recognized, but only the first time it was used in that loop. I received no errors during compile. I changed only that first occurrence to if (occupied()){break}; and it worked.

It's not the end of the world, but I'm curious about why it happened. I has been over 20 years since I have written any code and wonder if the change from C to C++ has anything to do with this.

After reading "That" thread in the community section, I'm tempted to say I want an answer right away, but I'll be nice. :grinning:

Without seeing the actual code, to would be next to impossible to determine why a function would "refuse to be recognized." Do you happen to have that code to post?

No, I don't. However, I could recreate it. I mean it was a simple fix once I figured out what was wrong. My question / concern is: Am I correct that I can write an if statement that has only one command without using the braces? As I said, it's been many years since I written any C code. I looked through all the C books I still have and can't find any confirmation that my memory is right. Then again, the line without braces worked farther down the loop and the compiler didn't complain.

Let me post the section of corrected code and understand the difference is only the braces around the if statements right after the while true statement. Ahhh, it's not that big, I'll post the whole sketch, it's not that big. I might even get some pointers about what I actually should have done. They would be welcome.

#include <Arduino.h>
#include <stdio.h>
#include <stdbool.h>

// Define pins for Track 1 sensors.
#define TRACK1_EAST_PIN 27
#define TRACK1_CENTER_PIN 26
#define TRACK1_WEST_PIN 25

// Define pins for Track 2 sensors.
#define TRACK2_EAST_PIN 33
#define TRACK2_CENTER_PIN 32
#define TRACK2_WEST_PIN 35

// Define pins for Signal Bulbs and Bell Trigger.
#define FIRST_BULB_PIN 13
#define SECOND_BULB_PIN 12
#define BELL_PIN 14

// Define pins for Gates control.
// These pins will be toggeled, one high and one low to provide
// power modules to control gate motors.
#define GATES1_PIN  18
#define GATES2_PIN  19
// delay for gates to raise before turning off bulbs and bell.
#define GATES_DELAY 200

// Define Values for Signal Bulb brightness.
// NOTICE!  Full OFF is 256 - Full ON is 0.
#define BULBON 0
#define BULBDIM 180
#define BULBOFF 256
#define BULBDELAY 1

// ////////////////////////////////////////////////////////////////////////////
// Function declarations.                                                    //
// ////////////////////////////////////////////////////////////////////////////
bool occupied();

// variables used to control dimming of leds
const int freq = 5000;
const int firstChannel = 0;
const int secondChannel = 1;
const int resolution = 8;
int dutyCycleFirst = BULBOFF;
int dutyCycleSecond = BULBOFF;


// ////////////////////////////////////////////////////////////////////////////
// Start of setup function.                                                  //
// ////////////////////////////////////////////////////////////////////////////
void setup()
{
  //Serial.begin(115200);
  Serial.begin(9600);

  // Set the mode of the pins to be used.
  pinMode(TRACK1_EAST_PIN, INPUT);   // This pin connects to the sensor of the East Approach Block. Track 1
  pinMode(TRACK1_CENTER_PIN, INPUT); // This pin connects to the sensor of the Crossing Block.      Track 1
  pinMode(TRACK1_WEST_PIN, INPUT);   // This pin connects to the sensor of the West Approach Block. Track 1
  pinMode(TRACK2_EAST_PIN, INPUT);   // This pin connects to the sensor of the East Approach Block. Track 2
  pinMode(TRACK2_CENTER_PIN, INPUT); // This pin connects to the sensor of the Crossing Block.      Track 2
  pinMode(TRACK2_WEST_PIN, INPUT);   // This pin connects to the sensor of the West Approach Block. Track 2
  pinMode(FIRST_BULB_PIN, OUTPUT);   // This pin provides power to the left bulb of the crossing lights.
  pinMode(SECOND_BULB_PIN, OUTPUT);  // This pin provides power to the right bulb of the crossing lights.
  pinMode(BELL_PIN, OUTPUT);         // This pin provides power to the bell module.
  pinMode(GATES1_PIN, OUTPUT);       // This pin provides power to the gates module.
  pinMode(GATES2_PIN, OUTPUT);       // This pin provides power to the gates module.


  // Setup FIRST_BULB_PIN and SECOND_BULB_PIN to enable dimming of the LEDS.
  ledcSetup(firstChannel, freq, resolution);
  ledcAttachPin(FIRST_BULB_PIN, firstChannel);
  ledcSetup(secondChannel, freq, resolution);
  ledcAttachPin(SECOND_BULB_PIN, secondChannel);

  // Start with both bulbs and bell turned off.
  ledcWrite(firstChannel, BULBOFF);
  ledcWrite(secondChannel, BULBOFF);
  digitalWrite(BELL_PIN, HIGH);
  delay(500);
}

// ///////////////////////////////////////////////////////////////////// //
// Start of main loop. This will run continiously!                       //
// All it does is turn on the bell and flash the lights if occupied() //
//   returns true or just loop otherwise.                                //
// ///////////////////////////////////////////////////////////////////// //
void loop()
{
  static bool firstTime;

  dutyCycleFirst = BULBOFF;
  dutyCycleSecond = BULBOFF;
  firstTime = true;

  while (occupied())  //  !!!!!  This WAS while true !!!!!!!!!
  {
    /* *******************  REMEMBER  ********************************** */
    /* THE HIGHER THE DUTYCYCLE, THE DIMMER THE LIGHT!!!!                */
    /* THE LOWER THE DUTYCYCLE, THE BRIGHTER THE LIGHT!!!!               */
    /* ***************************************************************** */
// if (!occupied) break;  !!!!!!!!!!!!!  This was the line that was being ignored !!!!!!!!!!!!
    digitalWrite(BELL_PIN, LOW);
    digitalWrite(GATES1_PIN, HIGH);
    digitalWrite(GATES2_PIN, LOW);

    // Turn on the first bulb to full brightness.
    while (dutyCycleFirst > BULBON)
    {
      ledcWrite(firstChannel, dutyCycleFirst);
      dutyCycleFirst--;
      delay(BULBDELAY);
    }
    delay(250);

    // Start to dim first bulb.
    while (dutyCycleFirst < BULBDIM)
    {
      ledcWrite(firstChannel, dutyCycleFirst);
      dutyCycleFirst++;
      delay(BULBDELAY);
    }
// if (!occupied) break;  !!!!!!!!!!!!!  This was the line that was being executed properly!!!!!!!!!!!!
 
    // Finish dimming first bulb while starting to turn second bulb on.
    while (dutyCycleFirst < BULBOFF)
    {
      ledcWrite(firstChannel, dutyCycleFirst);
      dutyCycleFirst++;
      ledcWrite(secondChannel, dutyCycleSecond);
      dutyCycleSecond--;
      delay(BULBDELAY);
    }
// if (!occupied) break;  !!!!!!!!!!!!!  This was the line that was being executed properly!!!!!!!!!!!!

    // Finish turning second bulb on.
    while (dutyCycleSecond > BULBON)
    {
      ledcWrite(secondChannel, dutyCycleSecond);
      dutyCycleSecond--;
      delay(BULBDELAY);
    }
    delay(250);

    // If occupied() turn off second bulb and start first bulb.
    // Otherwise, we'll be exiting the loop and both bulbs will be turned off.
    if (occupied())
    { 
      // Start to dim the second bulb.
      while (dutyCycleSecond < BULBDIM)
      {
        ledcWrite(secondChannel, dutyCycleSecond);
        dutyCycleSecond++;
        delay(BULBDELAY);
      }
      // Finish dimming second bulb while starting to turn first bulb on
      while (dutyCycleSecond < BULBOFF)
      {
        ledcWrite(secondChannel, dutyCycleSecond);
        dutyCycleSecond++;
        ledcWrite(firstChannel, dutyCycleFirst);
        dutyCycleFirst--;
        delay(BULBDELAY);
      }
    }

    firstTime = false;
  }
  /* ************************************** */
  /* This is the end of the activate loop.  */
  /* ************************************** */
  // Raise the gates
  digitalWrite(GATES1_PIN, LOW);
  digitalWrite(GATES2_PIN, HIGH);

  delay(GATES_DELAY);

  // Ensure first bulb is off
  // Ensure second bulb is off
  while (dutyCycleSecond < BULBOFF)
  {
    dutyCycleSecond++;
    ledcWrite(secondChannel, dutyCycleSecond);
    delay(BULBDELAY);
  }

  // Turn off the bell
  digitalWrite(BELL_PIN, HIGH);

}


// ////////////////////////////////////////////////////////////////////////// //
// Function to test occupancy of the six inputs.  Track 1 and Track 2         //
// ////////////////////////////////////////////////////////////////////////// //
bool occupied()
{
  static bool Track1_Triggered;
  static bool Track2_Triggered;
  bool Track1_result;
  bool Track2_result;

  if ((!digitalRead(TRACK1_EAST_PIN)) && (!digitalRead(TRACK1_WEST_PIN)) && (!digitalRead(TRACK1_CENTER_PIN)))
  { 
    Track1_Triggered = false;
    Track1_result = false;
  }
  else if (digitalRead(TRACK1_CENTER_PIN))
  {
    Track1_Triggered = true;
    Track1_result = true;
  }
  else
  {
    Track1_result = (Track1_Triggered ? false : true);
  }

  if ((!digitalRead(TRACK2_EAST_PIN)) && (!digitalRead(TRACK2_WEST_PIN)) && (!digitalRead(TRACK2_CENTER_PIN)))
  { 
    Track2_Triggered = false;
    Track2_result = false;
  }
  else if (digitalRead(TRACK2_CENTER_PIN))
  {
    Track2_Triggered = true;
    Track2_result = true;
  }
  else
  {
    Track2_result = (Track2_Triggered ? false : true);
  }

return ((!Track1_result && !Track2_result) ? false : true);
}

Yes, an if statement will run the next command when the condition is true. That command can be a single line, or it can be a block of code denoted using braces. I prefer to always use braces because I think it looks cleaner and if one were to add a second command, there is no need to remember to add the braces later.

Well, that didn't work out as planned!

to post code I use open square brace and the word code followed by close square brace, then the code followed by open square brace, a forward slash, the word code followed by close square brace, right?

In the editor there is a code button.

Thank you for that. For once I was right!!!!

Well, it didn't work. Perhaps optimization of the code by the compiler? In any case. I was in the habit of doing as you suggest. Don't really know why I did it the way I did.

Anyway, As the title notes, I have it fixed in the code and I'm feeling pretty good that a few of my old knowledge is still there.

Grrr! :face_exhaling: Use the button! Got it!

Now, where's the good job, thanks, karma, whatever button?

The heart under any reply.