Hello all, I currently have a signal set up on my train layout, It can do it what it needs to do,
as in stay green, go red when it is interrupted via IR sensor, switch to yellow then green.
My question is, How do i re-work the code so that it can get always go back to red when ever the signal is cut, instead of having to wait for the delay?
For example, train comes in, signal is cut, signal goes to red, stays red then goes to yellow. If i try to cut the signal at any point, it stays yellow for a fixed amount of time before going to red again. How can i fix it so that when ever it is interrupted it will go to red no matter what and restart the timer to yellow then green?
Thanks for any help
int green = 2; // green
int yellow = 3; // yellow
int red = 4; //red
int isObstaclePin = 9; // input pin from IR sensor
int isObstacle = HIGH; // HIGH MEANS NO OBSTACLE
void setup() {
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(isObstaclePin, INPUT);
Serial.begin(9600);
}
void loop() {
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == HIGH)
{
digitalWrite(green, HIGH);
delay(10);
}
else
{
Serial.println("clear");
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
delay(10000);
digitalWrite(red, LOW);
delay(10);
digitalWrite(yellow, HIGH);
delay(10000);
digitalWrite(yellow, LOW);
delay(10);
}
}