Hi,
I'm new to the forum and I hope someone can help with what is probably (to most users here) a simple problem.
My son and I both have no experience of programming or electronics, but have been trying to follow the Boxall Arduino Workshop book. We have been stuck for some time on the traffic light problem.
A quick explanation - the lights should control the traffic from West to East and vice versa with switches (representing sensors). Without input from the switches, the lights should constantly be on red from one direction, green from the other; initially, the traffic flow is from West to East. I'm aware that the sketch as in the book incorrectly assigns one of the input pins, but even with that sorted the lights cycle through the programme, changing to allow the traffic to flow from West, then from East, but don't pause (i.e. the inputs are redundant and changing is constant, with a 10s delay between changes).
Disconnecting the input pins has from the circuit has no effect, so clearly the issue is with the way I have put together the sketch (although as far as I can see it's the same as the book). As I mentioned above, we haven't managed to progress past this project and I'd be extremely grateful if anyone can spot the flaw in my code (outlined below).
Many thanks in advance,
Pete
// Project 5 - controlling traffic
#define westButton 7 //note sketch says 3
#define eastButton 13
#define westRed 2
#define westYellow 1
#define westGreen 0
#define eastRed 12
#define eastYellow 11
#define eastGreen 10
#define yellowBlinkTime 500 //0.5 seconds for ellow light blink
boolean trafficWest = true;
int flowTime = 10000; // amount of time to let traffic flow
int changeDelay = 2000;//time between delays
void setup()
{
// setup digital I/O pins
pinMode (westButton, INPUT);
pinMode (eastButton, INPUT);
pinMode (westRed, OUTPUT);
pinMode (westYellow, OUTPUT);
pinMode (westGreen, OUTPUT);
pinMode (eastRed, OUTPUT);
pinMode (eastYellow, OUTPUT);
pinMode (eastGreen, OUTPUT);
//The initial states for the lights west side green first
digitalWrite(westRed, LOW);
digitalWrite(westYellow, LOW);
digitalWrite(westGreen, HIGH);
digitalWrite(eastRed, HIGH);
digitalWrite(eastYellow, LOW);
digitalWrite(eastGreen, LOW);
}
void loop()
{
if ( digitalRead(westButton) == HIGH )// a west to east traffic flow
{
if ( trafficWest != true ) // only continue if traffic flowing in the east direction
{
trafficWest = true; //change traffic flow flag to west>east
delay(flowTime); //give time for traffic to flow
digitalWrite(eastGreen, LOW); // change east-facing lights from green to yellow to red
digitalWrite(eastYellow, HIGH);
delay(changeDelay);
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, HIGH);
for ( int a = 0 ; a < 5; a++)// blink yellow light
{
digitalWrite(westYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(westYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(westYellow, LOW);
digitalWrite(westRed, LOW); // change west-facing lights from red to green
digitalWrite(westGreen, HIGH);
}
}
if ( digitalRead(eastButton) == HIGH) // request east>west traffic flow
{
if ( trafficWest == true ) // only continue if traffic flow is in the opposite (west) direction
{
trafficWest = false; // change traffic flow flag to east>west
delay(flowTime); // give time for traffic to flow
digitalWrite(westGreen, LOW); // change west lights from green to yellow to red
digitalWrite(westYellow, HIGH);
delay(changeDelay);
digitalWrite(westYellow, LOW);
digitalWrite(westRed, HIGH);
delay(changeDelay);
for ( int a = 0 ; a < 5; a++ ) // blink yellow light
{
digitalWrite(eastYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(eastYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, LOW); //change east-facing lights from red to green
digitalWrite(eastGreen, HIGH);
}
}
}

