Explanation of project:~
I am a complete novice to Arduino, and am using the TinkerCAD website to try and devise a code to provide automatic control for 3-aspect model rail signals.
I have only included 3 signals in the code, in order to keep it simple, and so that I can grasp the principles before increasing the number of signals.
The 3 signals can be considered to be equally spaced around a circular track, in such a manner as the Train will pass signal 1, then pass signal 2, then pass signal 3, and then pass signal 1 again, etc, etc, etc.
Each signal has a Red, Yellow, and, Green aspect, and SHOULD only display 1 colour at any time.
If the block ahead of any signal is clear, AND the next block is also clear, the signal should show Green.
If the block ahead of any signal is clear, but the next block is occupied, then the signal should show Yellow.
If the block ahead of any signal is occupied, the signal should show Red.
As I am using the TinkerCAD website, I have created a project using 3 switches as INPUTS, to simulate the Train being detected in the block section(s) ahead of each signal.
When a switch is closed this tells the Arduino that a Train has passed the corresponding signal, and is occupying the block ahead, and the corresponding signal should show Red, and the signal behind (a.k.a. the previous signal) should be Yellow, and have no effect on the signal behind that (Green).
When there is no input from the switches (no Train on track), all signals should be Green.
This is to control Trains in one direction only.
Description of problem:~
My code does not work.
I get no error messages.
When I run the program each signal displays all 3 colours (Red, Yellow, and, Green) = all 9 LEDs illuminated at the same time, and the switches have no effect.
Unfortunately, as a complete novice, I am unable to think of a solution to this problem.
Plea:~
Could somebody please provide me with some guidance as to how I can solve this issue?
I include my (very rudimentary) code.
Thanks.
const int R1 = 2; // choose pin for signal 1 RED aspect
const int Y1 = 3; // choose pin for signal 1 YELLOW aspect
const int G1 = 4; // choose pin for signal 1 GREEN aspect
const int R2 = 5; // choose pin for signal 2 RED aspect
const int Y2 = 6; // choose pin for signal 2 YELLOW aspect
const int G2 = 7; // choose pin for signal 2 GREEN aspect
const int R3 = 8; // choose pin for signal 3 RED aspect
const int Y3 = 9; // choose pin for signal 3 YELLOW aspect
const int G3 = 10; // choose pin for signal 3 GREEN aspect
const int inPin1 = 14; // choose pin for pushbutton 1
int val1 = 0; // variable for reading the pin status
const int inPin2 = 15; // choose pin for pushbutton 2
int val2 = 1; // variable for reading the pin status
const int inPin3 = 16; // choose pin for pushbutton 3
int val3 = 2; // variable for reading the pin status
void setup()
{
pinMode(R1, OUTPUT); // set pin as output
pinMode(Y1, OUTPUT); // set pin as output
pinMode(G1, OUTPUT); // set pin as output
pinMode(R2, OUTPUT); // set pin as output
pinMode(Y2, OUTPUT); // set pin as output
pinMode(G2, OUTPUT); // set pin as output
pinMode(R3, OUTPUT); // set pin as output
pinMode(Y3, OUTPUT); // set pin as output
pinMode(G3, OUTPUT); // set pin as output
pinMode(14, INPUT); // set pin as input
pinMode(15, INPUT); // set pin as input
pinMode(16, INPUT); // set pin as input
// start with all signals Green
digitalWrite(R1, LOW); // turn R1 OFF
digitalWrite(Y1, LOW); // turn Y1 OFF
digitalWrite(G1, HIGH); // turn G1 ON
digitalWrite(R2, LOW); // turn R2 OFF
digitalWrite(Y2, LOW); // turn Y2 OFF
digitalWrite(G2, HIGH); // turn G2 ON
digitalWrite(R3, LOW); // turn R3 OFF
digitalWrite(Y3, LOW); // turn Y3 OFF
digitalWrite(G3, HIGH); // turn G3 ON
}
void loop() {
val1 = digitalRead(inPin1); // read input value 1
val2 = digitalRead(inPin2); // read input value 2
val3 = digitalRead(inPin3); // read input value 3
if (val1 == HIGH && val2 == HIGH);
{ digitalWrite(G1, HIGH);} // turns on G1
if (val1 == HIGH && val2 == LOW);
{ digitalWrite(Y1, HIGH);} // turns on Y1
if (val1 == LOW);
{ digitalWrite(R1, HIGH);} // turns on R1
// REPEAT FOR NEXT SIGNAL
if (val2 == HIGH && val3 == HIGH);
{ digitalWrite(G2, HIGH);} // turns on G2
if (val2 == HIGH && val3 == LOW);
{ digitalWrite(Y2, HIGH);} // turns on Y2
if (val2 == LOW);
{digitalWrite(R2, HIGH);} // turns on R2
// REPEAT FOR NEXT SIGNAL
if (val3 == HIGH && val1 == HIGH);
{ digitalWrite(G3, HIGH);} // turns on G3
if (val3 == HIGH && val1 == LOW);
{ digitalWrite(Y3, HIGH);} // turns on Y3
if (val3 == LOW);
{digitalWrite(R3, HIGH);} // turns on R3
}
// END
I am a complete novice to Arduino, and am using the TinkerCAD website to try and devise a code to provide automatic control for 3-aspect model rail signals.
I have only included 3 signals in the code, in order to keep it simple, and so that I can grasp the principles before increasing the number of signals.
The 3 signals can be considered to be equally spaced around a circular track, in such a manner as the Train will pass signal 1, then pass signal 2, then pass signal 3, and then pass signal 1 again, etc, etc, etc.
Each signal has a Red, Yellow, and, Green aspect, and SHOULD only display 1 colour at any time.
If the block ahead of any signal is clear, AND the next block is also clear, the signal should show Green.
If the block ahead of any signal is clear, but the next block is occupied, then the signal should show Yellow.
If the block ahead of any signal is occupied, the signal should show Red.
As I am using the TinkerCAD website, I have created a project using 3 switches as INPUTS, to simulate the Train being detected in the block section(s) ahead of each signal.
When a switch is closed this tells the Arduino that a Train has passed the corresponding signal, and is occupying the block ahead, and the corresponding signal should show Red, and the signal behind (a.k.a. the previous signal) should be Yellow, and have no effect on the signal behind that (Green).
When there is no input from the switches (no Train on track), all signals should be Green.
This is to control Trains in one direction only.
Description of problem:~
My code does not work.
I get no error messages.
When I run the program each signal displays all 3 colours (Red, Yellow, and, Green) = all 9 LEDs illuminated at the same time, and the switches have no effect.
Unfortunately, as a complete novice, I am unable to think of a solution to this problem.
Plea:~
Could somebody please provide me with some guidance as to how I can solve this issue?
I include my (very rudimentary) code.
Thanks.
const int R1 = 2; // choose pin for signal 1 RED aspect
const int Y1 = 3; // choose pin for signal 1 YELLOW aspect
const int G1 = 4; // choose pin for signal 1 GREEN aspect
const int R2 = 5; // choose pin for signal 2 RED aspect
const int Y2 = 6; // choose pin for signal 2 YELLOW aspect
const int G2 = 7; // choose pin for signal 2 GREEN aspect
const int R3 = 8; // choose pin for signal 3 RED aspect
const int Y3 = 9; // choose pin for signal 3 YELLOW aspect
const int G3 = 10; // choose pin for signal 3 GREEN aspect
const int inPin1 = 14; // choose pin for pushbutton 1
int val1 = 0; // variable for reading the pin status
const int inPin2 = 15; // choose pin for pushbutton 2
int val2 = 1; // variable for reading the pin status
const int inPin3 = 16; // choose pin for pushbutton 3
int val3 = 2; // variable for reading the pin status
void setup()
{
pinMode(R1, OUTPUT); // set pin as output
pinMode(Y1, OUTPUT); // set pin as output
pinMode(G1, OUTPUT); // set pin as output
pinMode(R2, OUTPUT); // set pin as output
pinMode(Y2, OUTPUT); // set pin as output
pinMode(G2, OUTPUT); // set pin as output
pinMode(R3, OUTPUT); // set pin as output
pinMode(Y3, OUTPUT); // set pin as output
pinMode(G3, OUTPUT); // set pin as output
pinMode(14, INPUT); // set pin as input
pinMode(15, INPUT); // set pin as input
pinMode(16, INPUT); // set pin as input
// start with all signals Green
digitalWrite(R1, LOW); // turn R1 OFF
digitalWrite(Y1, LOW); // turn Y1 OFF
digitalWrite(G1, HIGH); // turn G1 ON
digitalWrite(R2, LOW); // turn R2 OFF
digitalWrite(Y2, LOW); // turn Y2 OFF
digitalWrite(G2, HIGH); // turn G2 ON
digitalWrite(R3, LOW); // turn R3 OFF
digitalWrite(Y3, LOW); // turn Y3 OFF
digitalWrite(G3, HIGH); // turn G3 ON
}
void loop() {
val1 = digitalRead(inPin1); // read input value 1
val2 = digitalRead(inPin2); // read input value 2
val3 = digitalRead(inPin3); // read input value 3
if (val1 == HIGH && val2 == HIGH);
{ digitalWrite(G1, HIGH);} // turns on G1
if (val1 == HIGH && val2 == LOW);
{ digitalWrite(Y1, HIGH);} // turns on Y1
if (val1 == LOW);
{ digitalWrite(R1, HIGH);} // turns on R1
// REPEAT FOR NEXT SIGNAL
if (val2 == HIGH && val3 == HIGH);
{ digitalWrite(G2, HIGH);} // turns on G2
if (val2 == HIGH && val3 == LOW);
{ digitalWrite(Y2, HIGH);} // turns on Y2
if (val2 == LOW);
{digitalWrite(R2, HIGH);} // turns on R2
// REPEAT FOR NEXT SIGNAL
if (val3 == HIGH && val1 == HIGH);
{ digitalWrite(G3, HIGH);} // turns on G3
if (val3 == HIGH && val1 == LOW);
{ digitalWrite(Y3, HIGH);} // turns on Y3
if (val3 == LOW);
{digitalWrite(R3, HIGH);} // turns on R3
}
// END