/*
Lucas Dahl
60 minute countdown for 4 digit 7 segment led display
Used for the suitcase bomb design, also uses code for the 7 digit code also on github
The output pins for the other arduinos will go into A1, safe, death, A5, and A3
*/
#include <TimerOne.h>


int sensorPin = A1;
int musicer = A0;
int sensorValue = 0;  // variable to store the value coming from the sensor
int resistorValue = 0;
int starter =0 ;
int safe = A2;
int death = A4;

// pins for the 4 digit 7 segment led, these can change depending on which is orders so look at the data sheet for the led.
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;

// n is the number that is displayed, the countdown function is on the bottom but if you wanted a different countdown then 60 min then change this value.
long n = 5959;
int x = 100;
//Used to change the speed of the coundown, if you add more programs to this code the countdown will be slower.
int timing_adjust = 5;

//A larger adjust for speed, if this is changed to 0 then it will count down 10 times faster. use this as a punishment for doing something wrong
int count = 9;

int minute = 0;
int stopper = 0;

//flip is the flipswitches part of the suitcase bomb, pass is the 7 digit passkey, and plug is the audio plug part.
int flip = 0;
int pass = 0;
int plug = 0;

// variable to change the count
int change_count = 9;

//the two pins that connect to the wire that needs to be cut, one stops the time and the other increases the speed.
int readDeath = 0;
int readSafe = 0;

int nostop = 0;
int toners = 0;
int ending = 0;

void setup()
{
  Serial.begin(9600);

  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);
  pinMode(musicer, OUTPUT);
    
  Timer1.initialize(100000);
  Timer1.attachInterrupt( add );
}
/***************************************/ 
void loop()
{
  //The timer will only start when the starter changes, this is so that when the bomb opens up the photoresistor will detect the light and start the bomb 
  if (starter ==0)
  {
  timing_adjustay(1000);
  sensorValue = analogRead(sensorPin); 
  if (sensorValue > 40)
  {
  starter = 1;
  }
  }
  flip = analogRead(A5);
  readDeath = analogRead(death);
  pass = analogRead(A3);
  readSafe = analogRead(safe);
  Serial.println(readSafe);
  
  if (readDeath < 800)
  {
   change_count = 1; 
   nostop = 1;
  }
 
  if (flip > 900 && pass > 800)
  {
    if (readDeath < 800)
    {
       change_count = 1;
       if (readSafe <900 && n == 0001)
       {
        n = 1;
        stopper = 1;
        if (toners == 0){
        winner();
        toners = 1;
       }
        
       }
    }
    if(nostop == 0)
    {
      if (readSafe < 900 )
      {  
        stopper = 1;
        if (toners == 0){
        winner();
        toners = 1;
        }
      }
    } 
  }
  
  if(starter == 1 && n == 0 && toners == 0)
  {
   loser();
   toners = 1;
  }
  if(n == 3000)
  {
    tone(musicer, 1000);
    timing_adjustay(1000);
    noTone(musicer);
  }
   if(n == 1500)
  {
    tone(musicer, 1000);
    timing_adjustay(1000);
    noTone(musicer);
  }
   if(n == 5 && ending == 0)
  {
    tone(musicer, 1000);
    timing_adjustay(10);
    noTone(musicer);
    ending = 1;
  }
   if(n == 4 && ending == 1)
  {
    tone(musicer, 1000);
    timing_adjustay(10);
    noTone(musicer);
    ending = 2;
  }
   if(n == 3 && ending == 2)
  {
    tone(musicer, 1000);
    timing_adjustay(10);
    noTone(musicer);
    ending = 3;
  }
   if(n == 2 && ending == 3)
  {
    tone(musicer, 1000);
    timing_adjustay(10);
    noTone(musicer);
    ending = 0;
  }

  clearLEDs();//clear the 7-segment display screen
  pickDigit(0);//Light up 7-segment display d1
  pickNumber((n/1000));// get the value of thousand
  timing_adjustay(timing_adjust);//timing_adjustay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(1);//Light up 7-segment display d2
  pickNumber((n%1000)/100);// get the value of hundred
  timing_adjustay(timing_adjust);//timing_adjustay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(2);//Light up 7-segment display d3
  pickNumber(n%100/10);//get the value of ten
  timing_adjustay(timing_adjust);//timing_adjustay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(3);//Light up 7-segment display d4
  pickNumber(n%10);//Get the value of single digit
  timing_adjustay(timing_adjust);//timing_adjustay 5ms
}
/**************************************/ 
void pickDigit(int x) //light up a 7-segment display
{
  //The 7-segment LED display is a common-cathode one. So also use digitalWrite to  set d1 as high and the LED will go out
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);

  switch(x)
  {
    case 0: 
    digitalWrite(d1, LOW);//Light d1 up 
    break;
    case 1: 
    digitalWrite(d2, LOW); //Light d2 up
    digitalWrite(p, HIGH);
    break;
    case 2: 
    digitalWrite(d3, LOW); //Light d3 up 
    break;
    default: 
    digitalWrite(d4, LOW); //Light d4 up 
    break;
  }
}
//The function is to control the 7-segment LED display to display numbers. Here x is the number to be displayed. It is an integer from 0 to 9 
void pickNumber(int x)
{
  switch(x)
  {
    default: 
    zero(); 
    break;
    case 1: 
    one(); 
    break;
    case 2: 
    two(); 
    break;
    case 3: 
    three(); 
    break;
    case 4: 
    four(); 
    break;
    case 5: 
    five(); 
    break;
    case 6: 
    six(); 
    break;
    case 7: 
    seven(); 
    break;
    case 8: 
    eight(); 
    break;
    case 9: 
    nine(); 
    break;
  }
} 
void clearLEDs() //clear the 7-segment display screen
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void zero() //the 7-segment led display 0
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void one() //the 7-segment led display 1
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void two() //the 7-segment led display 2
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void three() //the 7-segment led display 3
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void four() //the 7-segment led display 4
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void five() //the 7-segment led display 5
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void six() //the 7-segment led display 6
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void seven() //the 7-segment led display 7
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void eight() //the 7-segment led display 8
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void nine() //the 7-segment led display 9
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
/*******************************************/
void add()
{
  // Toggle LED
  if(starter == 1 && stopper == 0){
    count --;
  if(count == 0)
  {
    minute++;
    count = change_count;
    n--;
    if(minute == 60)
    {
      n = n-40;
      minute = 0;
    }
    if(n == 0)
    {
      n = 0000;
    }
  }
  }
}
void winner()
{
   tone(musicer, 523);
  timing_adjustay (100);
  noTone(musicer);
  timing_adjustay(25);
  tone(musicer, 659);
  timing_adjustay (125);
  noTone(musicer);
  timing_adjustay(25);
  tone(musicer, 680);
  timing_adjustay (150);
  noTone(musicer);
  timing_adjustay(25);
  tone(musicer, 659);
  timing_adjustay (125);
  noTone(musicer);
  timing_adjustay(50);
  tone(musicer, 1000);
  timing_adjustay (175);
  tone(musicer, 1200);
  timing_adjustay (250);
  noTone(musicer);

}
void loser()
{
  tone(musicer, 100);
  timing_adjustay (100);
  noTone(musicer);
  timing_adjustay(50);
  tone(musicer, 200);
  timing_adjustay (50);
  noTone(musicer);
  timing_adjustay(250);
   tone(musicer, 100);
  timing_adjustay (100);
  noTone(musicer);
  timing_adjustay(50);
  tone(musicer, 200);
  timing_adjustay (50);
  noTone(musicer);
  timing_adjustay(450);
    tone(musicer, 100);
  timing_adjustay (100);
  noTone(musicer);
  timing_adjustay(50);
  tone(musicer, 200);
  timing_adjustay (50);
  noTone(musicer);
  timing_adjustay(700);
  tone(musicer, 100);
  timing_adjustay (100);
  noTone(musicer);
  timing_adjustay(50);
  tone(musicer, 200);
  timing_adjustay (50);
  noTone(musicer);
  timing_adjustay(1000);
  tone(musicer, 100);
  timing_adjustay (100);
  noTone(musicer);
  timing_adjustay(50);
  tone(musicer, 200);
  timing_adjustay (50);
  noTone(musicer);
  timing_adjustay(1200);
  tone(musicer, 1000);
  timing_adjustay(1000);
  noTone(musicer);
}
