UCLA BMES Build Team

Winter Break Problem Set

Problem 1 - Traffic Light

Task:

Circuit:

Circuit

Code:

int red = 13;
int yellow = 7;
int green = 2;
int buttonPin = 8;
int previousState = LOW;                 // LOW = red, HIGH = green

void setup()
{
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(red, HIGH);
}

void loop()
{
  if (digitalRead(buttonPin) == HIGH)
  {
    if (previousState == LOW)            // if light is currently red
    {
      digitalWrite(red, LOW);
      digitalWrite(green, HIGH);
      previousState = HIGH;              // light is now green
    }
    else                                 // if light is currently green
    {
      digitalWrite(green, LOW);
      digitalWrite(yellow, HIGH);
      delay(5000);
      digitalWrite(yellow, LOW);
      digitalWrite(red, HIGH);
      previousState = LOW;               // light is now red
    }
    while (digitalRead(buttonPin) == HIGH)
    {
      // do nothing while button is still pressed so that code only runs once
    }
  }
}