Problem with traffic light control programme

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);
  }
  }
}

Either send everyone a copy of the book, or post a schematic showing how YOU have things wired to the Arduino.

You do have external resistors wired with the switches, right?

The sketch in the book is written for a non-American style of traffic lights.
But since you wrote programme instead of program, you are probably used to the way the traffic lights work in that sketch. I wasn't.

The sketch uses delay() too much, so it may be just unresponsive rather than faulty.

Edit: Nope, after a second read-through it's more likely bad wiring.
Edit2: I miss the old warnings when someone else has posted before a reply is posted.

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

I don't think so. As PaulS hinted, it's much more likely to be your wiring. You have floating inputs for sure when you disconnect the input pins and from the symptoms it sounds like you do when they're connected too.

Use input_pullup on your inputs and pull the input to ground to indicate a press of the switch.

As a test prior to changing anything, tie pins 7 and 13 to ground and I expect the constant changing will go away.

Many thanks for the prompt replies. I had assumed that if nothing was connected to the input pins, the lights would not change (as there is no "else" statement in the sketch). This is the circuit diagram I am working from (attached).

The original diagram in my (old) book didn't have the connection to ground on the part of the circuit with the capacitors and resistors - this was added in the errata to the book. Adding this seems to make the lights stick as they are, but not change on a button press.....

Your circuit image doesn't display for me.

Input_pullup is a common way to deal with the floating input issue - it doesn't require any extra hardware, but it does mean that you have to adjust your code so that LOW on that input means that the switch was pressed.

Apologies for the image problem - I'm at work and can't really fix it at the moment, but I'll try later.

As a newcomer to this, I had no idea about the floating input phenomenon but I'll check that out. Sounds logical. Thanks once again for your help, this has been hugely frustrating and my son and I are keen to solve it and move on.

I was curious as to why your image didn't display and pulled it out of the page source to discover that it does display if used directly. I note pulldown resistors and a cap, the latter presumably for debounce reasons. It looks plausible, but the symptoms suggest that it's not working.

Sadly, my electronics skills are not sufficient to tell whether there's an issue with it as defined. I suspect though that somehow, whatever the R & C are connected to is not actually ground on the Arduino. A common reason for this is a breadboard with a break in it - some separate the power and ground lines halfway across the board. This will be reflected in the red & blue lines that represent the power & ground suggested use. It's obvious when you know about it and invisible when you don't. Check your connectivity with a meter.

If that's not the issue, I would drop the resistor & cap, use input_pullup and have the switch connect the input to ground when pressed. Reverse the logic in the code to regard LOW as a button press.

Here's the image that zebrafoot attempted to embed in reply #5:

I think some websites are set up to prevent hotlinking images and that's why it doesn't show up. The solution is to attach and them embed following the instructions at (Out of Date) Guide: How to Insert Uploaded Images in a Post - Website and Forum - Arduino Forum