Hi everyone,
I build a simple traffic light for my son's wooden train toys, and he asked me if i wanted to make a railroad crossing with lights.
So i started to build the sign and its electronics.
I also made the sketch for the traffic light and the crossing.
Here is where it went wrong.
If i only use the traffic light everything is fine, as soon i put this second "if" statement, it just won't work.
don't worry about the wiring, cause it works stand alone.
If i put the second "if" (crossing) at first, then it will run but the traffic light won't.
So that makes me think i did something wrong in my code.
is there anyone who could help me ?
int red = 13;
int orange = 11;
int green = 9;
int button = 7;
int buttoncrossing = 2;
int ledcrossing1 = 5;
int ledcrossing2 = 4;
int val = 0;
int val2 = 0;
void setup() {
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(button, INPUT);
pinMode(ledcrossing1, OUTPUT);
pinMode(ledcrossing2, OUTPUT);
pinMode(buttoncrossing, INPUT);
}
void loop() {
val = digitalRead(button);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(orange, LOW);
if(val == HIGH){
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
delay(5000);
digitalWrite(green, LOW);
digitalWrite(orange, HIGH);
delay(2000);
}
val2 = digitalRead(buttoncrossing);
digitalWrite(ledcrossing1, LOW);
digitalWrite(ledcrossing2, LOW);
if(val2 == HIGH){
digitalWrite(ledcrossing1, HIGH);
delay(500);
digitalWrite(ledcrossing1, LOW);
delay(500);
digitalWrite(ledcrossing2, HIGH);
delay(500);
digitalWrite(ledcrossing2, LOW);
delay(500);
digitalWrite(ledcrossing1, HIGH);
delay(500);
digitalWrite(ledcrossing1, LOW);
delay(500);
digitalWrite(ledcrossing2, HIGH);
delay(500);
digitalWrite(ledcrossing2, LOW);
delay(500);
}
}