Explanation of project:~
I am a complete novice to Arduino, and am using the TinkerCAD website to try and devise a code to control 3-aspect Model Rail Signals.
Each signal has a Red, Yellow, and, Green aspect.
When a signal shows Red, this should turn the signal behind (a.k.a. the previous signal) to Yellow, and have no effect on the signal behind that.
I have only included 3 signals in the code, in order 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.
This is to control Trains in one direction only.
As I am using the TinkerCAD website, I have created a project using 3 pushbuttons as INPUTS, to simulate the Train being detected in the block section(s) ahead of each signal.
Description of problem:~
When I operate pushbutton 1 (to simulate that the Train has now passed signal-1 and is being detected in the block section ahead of that signal, signal-1 correctly displays a Red aspect, and signal-2 correctly displays a Yellow aspect, and signal-3 correctly remains unchanged at Green.
BUT>>>> the Green aspect in Signal 1 goes dim instead of OFF, and the Green aspect in signal-2 remains Green (full brightness).
A similar thing happens when I operate pushbutton 2, and also pushbutton 3; i.e. the signal corresponding to those pushbuttons correctly displays a Red aspect, and the one behind correctly displays a Yellow aspect, BUT>>>> the signal showing Red also has the Green dim, and the signal showing Yellow has the Green full brightness.
I am assuming from the way I have attempted to write the code that the Green aspects are getting conflicting instructions; i.e. the code appertaining to whichever pushbutton is operated to set the corresponding signal to Red is correctly telling the corresponding Green to go OFF, but another part of the code is telling the same Green to stay ON.
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 have (hopefully) attached a screenshot of what happens when I operate the pushbutton, and I also include my (very rudimentary) code.
Thanks.
[code]
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 = 0; // variable for reading the pin status
const int inPin3 = 16; // choose pin for pushbutton 3
int val3 = 0; // 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
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
if (val1 == LOW) { // check if input 1 is LOW (button 1 pressed)
digitalWrite(R1, HIGH); // turn R1 ON
digitalWrite(Y1, LOW); // turn Y1 OFF
digitalWrite(G1, LOW); // turn G1 OFF
digitalWrite(Y2, HIGH); // turn Y2 ON
digitalWrite(G2, LOW); // turn G2 OFF
} else {
digitalWrite(R1, LOW); // turn R1 OFF
digitalWrite(G1, HIGH); // turn G1 ON
digitalWrite(Y2, LOW); // turn Y2 OFF
digitalWrite(G2, HIGH); // turn G2 ON
}
val2 = digitalRead(inPin2); // read input value 2
if (val2 == LOW) { // check if input 2 is LOW (button 1 pressed)
digitalWrite(R2, HIGH); // turn R2 ON
digitalWrite(Y2, LOW); // turn Y2 OFF
digitalWrite(G2, LOW); // turn G2 OFF
digitalWrite(Y3, HIGH); // turn Y3 ON
digitalWrite(G3, LOW); // turn G3 OFF
} else {
digitalWrite(R2, LOW); // turn R2 OFF
digitalWrite(G2, HIGH); // turn G2 ON
digitalWrite(Y3, LOW); // turn Y3 OFF
digitalWrite(G3, HIGH); // turn G3 ON
}
val3 = digitalRead(inPin3); // read input value 3
if (val3 == LOW) { // check if input 3 is LOW (button 1 pressed)
digitalWrite(R3, HIGH); // turn R3 ON
digitalWrite(Y3, LOW); // turn Y3 OFF
digitalWrite(G3, LOW); // turn G3 OFF
digitalWrite(Y1, HIGH); // turn Y1 ON
digitalWrite(G1, LOW); // turn G1 OFF
} else {
digitalWrite(R3, LOW); // turn R3 OFF
digitalWrite(G3, HIGH); // turn G3 ON
digitalWrite(Y1, LOW); // turn Y1 OFF
digitalWrite(G1, HIGH); // turn G1 ON
}
}
[/code]