Hi, im new to arduino uno and programming it, ive been asked by a friend to help him make 3 way traffic lights and program the arduino. We have the traffic lights built. We are just having some difficulites with the code. Could any one correct it or fix it for me please?
There will be 2 sets of traffic lights on a main road, there will be a secondary road joining the main road. When there is car detected at secondary road (Position B on diagram below) the lights on main road should turn RED. . As soon as there is no car underneath the sensor the light on secondary read should switch to red and main road to green. It should stay that way until there is car detected at secondary road.
The sequance should be Green-yellown then red. When car goes the light should change back straight to red skipping yellow.
const int red = 11; // 11,12,13 main road
const int yellow = 12;
const int green = 13;
int swt = 2; // switch instead of sensor for now
const int red1 = 8; // 8,9,10 secondary road
const int yellow1 = 9;
const int green1 = 10;
// sets the pins 8-13 as outputs // sets pin 2 as input
void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(swt, INPUT);
}
// creates the loop that your Arduino will repeat over & over //
void loop(){
changeLights();
}
// Function that will change lights in Irish traffic lights system
void changeLights()
{
while(swt = HIGH)
{
digitalWrite(green, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
delay(2000);
digitalWrite(green1, LOW);
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
// turn off green, then turn yellow on for 5 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(5000);
// turn off yellow, then turn red on
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
//turn off red1, then turn on green1
digitalWrite(green1, HIGH);
digitalWrite(red1, LOW);
}
while(swt = LOW);
{
//turns red off, turns green on
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
// turn off green1, then turn yellow1 on for 5 seconds
digitalWrite(green1, LOW);
digitalWrite(yellow1, HIGH);
delay(5000);
// turn off yellow1, then turn red1 on
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
}
}