   digitalWrite(carGreenPin, carGreenState);
   digitalWrite(pedRedPin, pedRedState);
   digitalWrite(pedGreenPin, pedGreenState);
+  digitalWrite(pedWaitPin, pedWaitState);
+
+  delay(50);
 }
 

  Pedestrian light
 */

const int carRedPin = 13;
const int carYellowPin = 12;
const int carGreenPin = 8;
const int pedRedPin = 7;
const int pedGreenPin = 4;
const int buttonPin = 2;

int carRedState = 0;
int carYellowState = 0;
int carGreenState = 0;
int pedRedState = 0;
int pedGreenState = 0;

unsigned long time = 0;
int buttonState = 0;
boolean pedPushed = false;
boolean pedHasGreen = false;
unsigned long waitUntil = 0;
int switchStep = 0;

void setup() {
  pinMode(carRedPin, OUTPUT);

  pinMode(carGreenPin, OUTPUT);
  pinMode(pedRedPin, OUTPUT);
  pinMode(pedGreenPin, OUTPUT);
  pinMode(pedWaitPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  time = millis();
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && (lightState == 0 || lightState >= 5)) {
    pedPushed = true;
    pedWaitState = HIGH;
  }

  switch (lightState) {
  case 0:
    if (pedPushed == true) {
      pedPushed = false;
      if (waitUntil == 0 || waitUntil < time + 5000) {
        waitUntil = time + 5000;
      }
      lightState++;
    }
    break;
  case 1:
    if (time > waitUntil) {
      carYellowState = HIGH;
      carGreenState = LOW;
      waitUntil = time + 2000;
      lightState++;
    }
    break;
  case 2:
    if (time > waitUntil) {
      carRedState = HIGH;
      carYellowState = LOW;
      waitUntil = time + 2000;
      lightState++;
    }
    break;
  case 3:
    if (time > waitUntil) {
      pedRedState = LOW;
      pedGreenState = HIGH;
      pedWaitState = LOW;
      waitUntil = time + 10000;
      lightState++;
    }
    break;
  case 4:
    if (time > waitUntil) {
      pedRedState = HIGH;
      pedGreenState = LOW;
      waitUntil = time + 2000;
      lightState++;
    }
    break;
  case 5:
    if (time > waitUntil) {
      carYellowState = HIGH;
      waitUntil = time + 2000;
      lightState++;
    }
    break;
  case 6:
    if (time > waitUntil) {
      carRedState = LOW;
      carYellowState = LOW;
      carGreenState = HIGH;
      waitUntil = time + 20000;
      lightState = 0;
    }
    break;
  }

  digitalWrite(carRedPin, carRedState);
