I have this code for a model railroad project that is working for the most part, but, the last "while" instruction does not execute. I know for a fact the signal to the pins are there, but, it will not execute the "while" statement at all.
I do NOT need to use millis, as when the signal is supposed to be YELLOW, it stays Green... and it is supposed to run the "while" command line as long as the input is TRUE(HIGH). It does not execute al all.
The only signal that should matter, is the nextbloxkFullState, which should tell the controller to run the YELLOW routine. It doesn't even see it... I have checked the signals, they are proper, and I have output to another LED from this, and it does NOT respond to the input. I KNOW the signals are there, I am getting frustrated, and thought maybe someone could see the problem, that has GOT to be a simple one!!! I KNOW the inputs and outputs are working... I tested each one individually with a short program for each. It seems the last "while" statement is not seeing and executing the command.
the first 2 "while" statements work PERFECTLY!!! All I have to do is get the YELLOW output to work.
Thanks in advance for your help folks!!
Here is the code:
// constants won’t change. They’re used here to
// set pin numbers:
const int nextblockFull = 7; // Next block full (input)
const int blockDirection = 6; // LOW is inbound, HIGH is Outbound (input)
const int blockFull = 5; // block is full (Output)
const int todPin = 4; // the number of the Occupancy pin (input)
const int ledPin = 3; // the number of the LED pin (Output)
// variables will change:
int todState = 0;
int blockDirectionState = 0;
int nextblockFullState = 0;
int blockFullState = 0;
void setup() {
pinMode(blockFull, OUTPUT); // initialize "block full" signal pin to PREVIOUS block Signal. HIGH = Signal is YELLOW
// telling the previous block signal that it is clear to turn GREEN.
pinMode(blockDirection, INPUT); // initialize Block Traffic Direction input
// if signal is HIGH, then Traffic is set for INBOUND direction. Keeping it RED.
// if signal is LOW, then traffic is routed in the NORMAL direction ie. OUTBOUND
pinMode(nextblockFull, INPUT); // initialize Next block full signal INPUT from next block signal
// This INPUT is used to detect if the next Block Signal is RED.
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
// This is the signal head. It is a R/G bi directional LED.
// This OUTPUT will be using PWM, as to generate the R-Y-G block signal
pinMode(todPin, INPUT); // initialize the UV pin as an input:
// This INPUT tells the controller if the block is occupied, if this block is
// occupied, the signal is HIGH, keeping the "OCCUPIED BLOCK", in the RED
}
void loop(){
// read the Block direction state. HIGH = "Inbound", LOW = "Outbound" Traffic
// This will keep the block from going "bi-directional" and head in to inbound traffic
blockDirectionState = digitalRead(blockDirection); // check for signal. if it is HIGH, the blockDirection is Outbound
// If input is LOW, Block Traffic Direction is Inbound, and must stay RED until
// until traffic direction switches to Outbound
todState = digitalRead(todPin); // read todState
while (blockDirectionState == HIGH) { // check block direction. if traffic direction is inbound, callRedSignal() to make RED
digitalWrite(ledPin, HIGH); // make signal LED RED
digitalRead(blockDirectionState);
return;
}
digitalWrite(ledPin, LOW);
while (todState == HIGH){ // Detecting block full state, sets block signal to RED, sets blockFull HIGH
// Stays "RED" as long as train is detected
digitalWrite(ledPin, HIGH);
digitalWrite(blockFull, HIGH);
digitalRead(todState);
return;
}
if (digitalRead(todState == LOW)){ // when train is not detected, reset the Signal to GREEN
digitalWrite(blockFull, LOW); // return blockFull to LOW to tell the block is empty
}
nextblockFullState = (digitalRead, nextblockFull); // check nextblockFullState
todState = (digitalRead, todPin); // check todState for LOW
while (nextblockFullState == HIGH){ // if the nextblockFullState is "HIGH", then executeflash LED to make it YELLOW color
digitalWrite(ledPin, HIGH);
delay(5);
digitalWrite(ledPin, LOW);
delay(18);
return;
}
}