Traffic Light Challenge


It's been a long week. You've learned a lot about controlling LEDs, reading buttons and analog sensors, and making decisions with your Arduino. Let's try to do something interesting with all that knowledge - make a day/night traffic light thingy with a walk button.

Here's a video of the kind of thing I'm looking for:



The Circuit



This design has all the parts needed for the sketches you've done this week. The pins may differ, but you can probably still show off your other work if you edit the sketches a little bit.


The Code


Try to program a traffic light controller for the circuit above that works like this:

Try to make it so that the light cycling speed can change at almost any time.

Here's some code to get you started. You can use or ignore my suggested structure in the loop function:

/* Traffic Lights
 * Solution by: 
 *
 * This is a simple traffic light simulator. 
 *
 * During the day, when the walk button (the bottom one) is pressed, the light turns
 * green so you can walk. Then it cycles back to red.
 *
 * At night, when there is little traffic, the button does nothing and the light
 * blinks red to act as a simple stop signal. 
 *
 * The potentiometer controls the speed at which the light cycles.
 *
 */
 
 // Symbolic names for our pins.
 const int pot_sense = A0;
 const int light_sense = A1;
 const int walkButton = 2;
 const int button2 = 3;
 const int red = 13;
 const int yellow = 12;
 const int green = 10;

// variable for light speed
int light_spd = 500;

void setup()
{
  // Configure inputs
  pinMode(pot_sense, INPUT);
  pinMode(light_sense, INPUT);
  pinMode(walkButton, INPUT);
  pinMode(button2, INPUT);
  
  // Configure outputs
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  
  // Setting up Serial communication, just in case.
  Serial.begin(9600);
}


void loop()
{
  //Get the speed the light will cycle at
  
  //if it is night
  {
    //the traffic light blinks red
  }
  //otherwise it must be day
  {
    //So the traffic light will show solid red by default
    
    //if the "walk" button is pressed
    {
      //Show green for a bit
      
      //Show yellow for a bit
      
      //Show red for a bit
    }
  }
}




Challenge idea courtesy of Dr. David Gerhard.