const int motoristRedPin = 2;
const int motoristGreenPin = 3;
const int motoristBluePin = 4;
const int pedestrianRed1Pin = 5;
const int pedestrianGreenPin = 6;
const int pedestrianBlinkPin = 7;
const int pedestrianRed2Pin = 8;
const int buttonPin = 9;

void setup() {
  pinMode(motoristRedPin, OUTPUT);
  pinMode(motoristGreenPin, OUTPUT);
  pinMode(motoristBluePin, OUTPUT);
  pinMode(pedestrianRed1Pin, OUTPUT);
  pinMode(pedestrianGreenPin, OUTPUT);
  pinMode(pedestrianBlinkPin, OUTPUT);
  pinMode(pedestrianRed2Pin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  digitalWrite(motoristRedPin, HIGH);
  digitalWrite(pedestrianRed1Pin, HIGH);
  digitalWrite(pedestrianRed2Pin, HIGH);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    pedestrianCrossing();
  } else {
    normalTrafficFlow();
  }
}

void pedestrianCrossing() {
  digitalWrite(motoristRedPin, HIGH);
  digitalWrite(motoristGreenPin, HIGH);
  digitalWrite(motoristBluePin, HIGH);
  delay(1000);

  digitalWrite(motoristGreenPin, LOW);
  delay(500);
  digitalWrite(motoristRedPin, HIGH);
  delay(500);

  digitalWrite(pedestrianRed1Pin, LOW);
  digitalWrite(pedestrianGreenPin, HIGH);
  delay(20000);

  for (int i = 0; i < 5; i++) {
    digitalWrite(pedestrianGreenPin, LOW);
    delay(500);
    digitalWrite(pedestrianGreenPin, HIGH);
    delay(500);
  }

  digitalWrite(pedestrianGreenPin, LOW);
  digitalWrite(pedestrianBlinkPin, HIGH);
  delay(5000);

  digitalWrite(motoristRedPin, HIGH);
  digitalWrite(motoristGreenPin, HIGH);
  digitalWrite(motoristBluePin, LOW);
  delay(5000);

  digitalWrite(pedestrianBlinkPin, LOW);
  digitalWrite(pedestrianRed1Pin, HIGH);
}

void normalTrafficFlow() {
  digitalWrite(motoristRedPin, HIGH);
  digitalWrite(motoristGreenPin, HIGH);
  digitalWrite(motoristBluePin, LOW);
}