#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

#define Fuss 7 // Pin connected to the Fuse relay.
#define GoButt 6 // Pin connected to the GO button.
#define ArmButt 5 // Pin connected to the ARM button.
#define BuzzPin 4 // Connected to the Speaker.
#define SetPot 0 // Analog Pin connected to the Pot.
#define EnableMotor 10
#define MotorPin1 8 // Pin connected to the Motor Driver Input 1.
#define MotorPin2 9 // Pin connected to the Motor Driver Input 2.
#define LimitSwitch1 12 // Pin connected to limit switch 1.
#define LimitSwitch2 11 // Pin connected to limit switch 2.

Adafruit_7segment display = Adafruit_7segment();

void setup() {
  Wire.begin();
  display.begin(0x70);
  display.setBrightness(15);
  pinMode(Fuss, OUTPUT);
  pinMode(EnableMotor, OUTPUT);
  pinMode(ArmButt, INPUT);
  pinMode(GoButt, INPUT);
  pinMode(MotorPin1, OUTPUT);
  pinMode(MotorPin2, OUTPUT);
  pinMode(LimitSwitch1, INPUT_PULLUP);
  pinMode(LimitSwitch2, INPUT_PULLUP);
  digitalWrite(Fuss, LOW);
  digitalWrite(EnableMotor, LOW);
  digitalWrite(ArmButt, HIGH);
  digitalWrite(GoButt, HIGH);

  // Play startup sound
  tone(BuzzPin, 700);
  delay(250);
  noTone(BuzzPin);
  tone(BuzzPin, 800);
  delay(250);
  noTone(BuzzPin);
  tone(BuzzPin, 600);
  delay(250);
  noTone(BuzzPin);
}

int DownCntr; // down counter (1/10 Secs.)
int Go = 0; // Stopped

void loop() {
  if (!digitalRead(GoButt) || !digitalRead(ArmButt)) {
    Go = 0; // ABORT!!!
    tone(BuzzPin, 440, 1500);
    delay(1500);
  }
  if (Go == 0) {
    WaitARM();
    WaitGO();
  }
  ShowTimer();
  if (DownCntr > 50) {
    if (DownCntr % 10 == 0) tone(BuzzPin, 1000, 50); // Tone every second.
  } else if (DownCntr % 2 == 0) tone(BuzzPin, 1000, 50); // Tone every 1/5 second.

  if (DownCntr == 0) {
    //------------------ ROCKET LAUNCH! --------------------
    tone(BuzzPin, 440); // Launch audible signal
    digitalWrite(Fuss, HIGH); // CLOSE the fuse circuit
    digitalWrite(EnableMotor, HIGH); // Motor turns clockwise

    // Rotate clockwise until limit switch 1 is pressed
    while (digitalRead(LimitSwitch1)) {
      digitalWrite(MotorPin1, HIGH);
      digitalWrite(MotorPin2, LOW);
    }

    digitalWrite(Fuss, LOW); // OPEN the fuse circuit.
    noTone(BuzzPin);
    digitalWrite(MotorPin1, LOW); // Motor stop
    digitalWrite(MotorPin2, LOW);

    delay(2000);

    digitalWrite(EnableMotor, HIGH); // Motor turns counterclockwise
    // Rotate counterclockwise until limit switch 2 is pressed
    while (digitalRead(LimitSwitch2)) {
      digitalWrite(MotorPin1, LOW);
      digitalWrite(MotorPin2, HIGH);
    }

    digitalWrite(EnableMotor, LOW); // OPEN the fuse circuit.
    //------------------------------------------------------
    Go = 0;
  }

  while (millis() % 100); // Wait until the next 1/10 of second.
  delay(50);
  DownCntr--;
}

void WaitGO() {
  ShowTimer();
  while (digitalRead(GoButt));
  Go = 1;
  delay(20);
  while (!digitalRead(GoButt)); // Debounce GO button.
}

void ReadTimer() {
  DownCntr = map(analogRead(SetPot), 0, 1023, 5, 60);
  DownCntr *= 10;
}

void ShowTimer() {
  display.print((float)DownCntr / 10,2);
  display.writeDisplay();
  

}

void WaitARM() {
  while (digitalRead(ArmButt) == 1) {
    ReadTimer();
    display.clear();
    display.writeDisplay(); // Turn Off Digits.
    delay(50);
    ReadTimer();
    ShowTimer(); // Show Digits.
    delay(150);
  }
  Go = 0;
  ShowTimer();
  tone(BuzzPin, 2000, 150);
  delay(200);
  tone(BuzzPin, 2000, 150);
  delay(200);
  tone(BuzzPin, 2000, 150);
  delay(20);
  while (!digitalRead(ArmButt)); // Debounce ARM button.
}