/*Midland wool lights tower building C. */

//tower building c
//random lights

#define towerfirstfloor 2
#define towersecondfloor 3
#define towerthirdfloor 4
#define towerfourthfloor 5
#define towerfifthfloor 6

// 7 empty
//first on and last off
#define officewhite 8  //white

// constant lights
#define officeyellow  9 //yellow
#define towerclock 10

//security
#define officesecurity 11


//toggle day and night
#define Switch A5



//********************************************
void setup() {


  //random lights
  //building A

  //building C
  pinMode(towerfirstfloor, OUTPUT);
  pinMode(towersecondfloor, OUTPUT);
  pinMode(towerthirdfloor, OUTPUT);
  pinMode(towerfourthfloor, OUTPUT);
  pinMode(towerfifthfloor, OUTPUT);
  pinMode(towerclock, OUTPUT);
  digitalWrite(towerclock, HIGH);
  pinMode(officewhite, OUTPUT);
  pinMode(officeyellow, OUTPUT);
  digitalWrite(officeyellow, HIGH);
  pinMode(Switch, INPUT);
  randomSeed(analogRead(A7));

  //security
  pinMode(officesecurity, OUTPUT);
  Serial.begin(115200);
}

//Routine
void loop() {
  static int prev_val = LOW;
  int val = digitalRead(Switch);
  // only react to changes in the switch position
  if (val != prev_val) {
    if (val == HIGH) {
      turnonLights();
    } else {
      turnoffLights();
    }
    prev_val = val;
  }
  delay(50);
}

//**************************************************
void turnonLights()
{
  digitalWrite(officewhite, HIGH);
  delay(3000);
  digitalWrite(officesecurity, LOW);
  delay(random(500,1000));
  int lights_lit = 0;
  while (lights_lit < 5) {
    int Lights;
    do { // find a random light that is not yet lit
      Lights = random(2, 7);
    } while (digitalRead(Lights) == HIGH);
    int dlay = random(500, 1000);
    digitalWrite(Lights, HIGH);
    lights_lit += 1;
    delay(dlay);
  }
}

//**************************************************
void turnoffLights() {
  digitalWrite(officesecurity, HIGH);
  delay(random(700,2000));
  int lights_lit = 5;
  while (lights_lit > 0) {
    int Lights;
    do { // find a random light that is still lit
      Lights = random(2, 7);
    } while (digitalRead(Lights) == LOW);
    int dlay = random(700, 2000);
    digitalWrite(Lights, LOW);
    lights_lit -= 1;
    delay(dlay);
  }
  delay(3000);
  digitalWrite(officewhite, LOW);
}