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);
}