Ok, so here's my sob story....
I have a 5 year old son who happens to be a train fanatic (as all young boys should be..)
I have built him a surround for his O-scale track with three signal lights and a crossing flasher. I am trying to program a Uno to control all of these functions using three IR sensors to detect the train wheels.
I am new to code (but learning). I am not one to ask for help with anything, but I am deploying for ten months in a few days (Army) and really don't want to let the little fellow down. I can either get the flasher to work, or I can get the signal to work. I can not get the loop to change gears (to put it in my terms) between the two.
For simplicity, I have wired all my signal lights together in an array that will do the job with only three functions (red, green and yellow). The crossing signal has two separate LED's that need to flash alternately.
Can I plead for some assistance in showing me the error of my ways?
I have tried some examples using "case" examples, but alas, no joy..
Here is my "code" (note the self loathing tone). I think I have reached the point of saturation and this may be complete gibberish, but here it is....
If this isn't the proper forum or method to ask for help, MODS please delete, and accept my apologies.
const int CRS1 = 10; // Crossing light Left
const int CRS2 = 9; // Crossing light Right
const int rrCrossinIn = 2; // Crossing Signal sensor
const int rrCrossing = HIGH; // HIGH MEANS NO TRAIN
const int RS1Pin = 3; //Rail Signal Sensor 1
const int RS1 = HIGH; // HIGH MEANS NO TRAIN
const int RS2Pin = 4; //Rail Signal Sensor 2
const int RS2 = HIGH; // HIGH MEANS NO TRAIN
const int RED = 11; // Signal Output 1
const int YELLOW = 12; // Signal Output 2
const int GREEN = 13; // Signal Output 3
int crossingswitch = 2; // variable for reading the switch status
int signalswitch1 = 3;
int signalswitch2 = 4;
void setup() {
pinMode(CRS1, OUTPUT);
pinMode(CRS2, OUTPUT);
pinMode(crossingswitch, INPUT);
pinMode(RS1Pin, INPUT);
pinMode(RS2Pin, INPUT);
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
signalswitch1 = digitalRead(RS1Pin);
signalswitch2 = digitalRead(RS2Pin);
crossingswitch = digitalRead(rrCrossinIn);
}
void loop() {
green();
yellow();
red();
crs();
}
void green() {
if (signalswitch1 == LOW) {
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
if (signalswitch1 == HIGH) {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
}
void yellow() {
signalswitch1 = digitalRead(RS1Pin);
if (signalswitch2 == LOW) {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
delay(500);
}
if (signalswitch2 == HIGH) {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
}
void red() {
signalswitch1 = digitalRead(RS1Pin);
if (signalswitch1 == LOW) {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
if (signalswitch1 == HIGH) {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
}
void crs()
{
if (crossingswitch == LOW) //if the switch on:
{
digitalWrite(CRS1, HIGH); //red1 LED on
digitalWrite(CRS2, LOW); //red2 LED off
delay(400); //wait 400ms
digitalWrite(CRS1, LOW); //red1 LED off
digitalWrite(CRS2, HIGH); //red2 LED on
delay(400);
}
if (crossingswitch == HIGH) //if the switch off
{
digitalWrite(CRS1, LOW); //red1 LED on
digitalWrite(CRS2, LOW); //red2 LED off
}
}