/*
  Test box

  3008
  Test led's and keys

  Created 08 September 2022
  By Tomasz Majdan
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

//Button test box
const int buttonPinStart = 11;   //configure pin as an input and enable the internal pull-up resistor
const int buttonPinNext = 12;    //configure pin as an input and enable the internal pull-up resistor
const int buttonPinStartLed = 7; //configure pin as an input and enable the internal pull-up resistor
const int buttonPinNextLed = 8;  //configure pin as an input and enable the internal pull-up resistor
int buttonStart = 0;             // variable for reading the pushbutton status
int buttonNext = 0;              // variable for reading the pushbutton status

//Button PCB
const int swTick = 10;   //SW Tick
const int swCross = 9; //SW Cross
int buttonTick = 0;     //Variable for reading the pushbutton status
int buttonCross = 0;    //Variable for reading the pushbutton status

//LEDS
const int ledCross = 2;   // LED connected to digital pin 29
const int ledTick = 3;    // LED connected to digital pin 28
const int ledRed = 4;     // LED connected to digital pin 27
const int ledYell = 5;    // LED connected to digital pin 26
const int ledGreen = 6;   // LED connected to digital pin 25
const int delay1 = 1000;   // Time delay 1s

//buzzer
const int buzzerKey = A0;       //Buzzer
const int delayBuzzer = 100;    //Time delay 0.1s

void setup() {
  digitalWrite(buttonPinStartLed, HIGH);    // set the LED off
  digitalWrite(buttonPinNextLed, HIGH);     // set the LED off

  //Buzzer
  pinMode(buzzerKey, OUTPUT);

  //Button test box
  pinMode(buttonPinStart, INPUT_PULLUP);
  pinMode(buttonPinNext, INPUT_PULLUP);
  pinMode(buttonPinStartLed, OUTPUT);
  pinMode(buttonPinNextLed, OUTPUT);

  //Button PCB
  pinMode(swTick, INPUT_PULLUP);
  pinMode(swCross, INPUT_PULLUP);

  //LCD
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  lcd.setCursor(1, 0);
  lcd.print("TT Electronics");
  lcd.setCursor(3, 1);
  lcd.print("3008 Test");
  delay(1500);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("TT Electronics");

  digitalWrite(ledGreen, HIGH);
  digitalWrite(ledYell, HIGH);
  digitalWrite(ledRed, HIGH);
  digitalWrite(ledTick, HIGH);
  digitalWrite(ledCross, HIGH);


  //LED
  pinMode(ledCross, OUTPUT);
  pinMode(ledTick, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledYell, OUTPUT);
  pinMode(ledGreen, OUTPUT);

}
void loop() {
  startLoop();
}
void startLoop() {
  lcd.setCursor(1, 0);
  lcd.print("TT Electronics");
  lcd.setCursor(2, 1);
  lcd.print("Press START");
  digitalWrite(buttonPinStartLed, LOW);    // set the LED on

  buttonStart = digitalRead(buttonPinStart); // check if the pushbutton is pressed. If it is, the buttonState is HIGH
  Serial.println(buttonStart);               // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonStart == LOW) {
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("TT Electronics");
    delay(150);
    keyTest();
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("TT Electronics");
    lcd.setCursor(1, 1);
    lcd.print("Test complete");
    buzzer();
    delay(100);
    buzzer();
    delay(100);
    buzzer();
    delay(2000);
    Serial.println("Complete");
    lcd.clear();
  }
  else {
    startLoop();
  }
}
void buzzer() {
  digitalWrite(buzzerKey, HIGH);    // set the buzzer on
  delay(delayBuzzer);
  digitalWrite(buzzerKey, LOW);     // set the buzzer off
}
void keyTest() {
  keyTick();
  keyCross();
  leds();
  allComplete();
}
void keyTick() {
  digitalWrite(buttonPinStartLed, HIGH);    // set the LED off
  buttonTick = digitalRead(swTick);  // check if the pushbutton is pressed. If it is, the buttonState is LOW
  lcd.setCursor(0, 0);
  lcd.print("   Press key    ");
  lcd.setCursor(4, 1);
  lcd.print("SW1 Tick ");
  if (buttonTick == LOW) {
    lcd.clear();
    lcd.setCursor(4, 1);
    lcd.print("SW1 Tick ");
    lcd.setCursor(4, 1);
    lcd.print("Key pressed");
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("Press key  ");
    lcd.setCursor(3, 1);
    lcd.print("SW2 Cross ");
    buzzer();
    delay(500);
  }
  else {
    keyTick();
  }
}
void keyCross() {
  digitalWrite(buttonPinStartLed, HIGH);    // set the LED off
  buttonCross = digitalRead(swCross); // check if the pushbutton is pressed. If it is, the buttonState is HIGH
  if (buttonCross == LOW) {
    lcd.clear();
    lcd.setCursor(7, 1);
    lcd.print("SW2 Cross ");
    lcd.setCursor(4, 1);
    lcd.print("Key pressed");
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print("LED Test");
    lcd.setCursor(3, 1);
    lcd.print("Press NEXT ");
    digitalWrite(buttonPinNextLed, LOW);    // set the LED on
    buzzer();
    delay(500);
  }
  else {
    keyCross();
  }
}
void leds() {
  buttonNext = digitalRead(buttonPinNext);   // read the state of the pushbutton value
  if (buttonNext == LOW) {
    digitalWrite(buttonPinNextLed, HIGH);    // set the LED on
    lcd.setCursor(4, 0);
    lcd.print("LED Test");
    lcd.setCursor(0, 1);
    lcd.print("  In progress      ");
    delay(500);
    digitalWrite(ledGreen, LOW);
    delay(delay1);
    digitalWrite(ledGreen, HIGH);
    digitalWrite(ledYell, LOW);
    delay(delay1);
    digitalWrite(ledYell, HIGH);
    digitalWrite(ledRed, LOW);
    delay(delay1);
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledTick, LOW);
    delay(delay1);
    digitalWrite(ledTick, HIGH);
    digitalWrite(ledCross, LOW);
    delay(delay1);
    digitalWrite(ledCross, HIGH);
    delay(500);    
    digitalWrite(ledGreen, LOW);
    digitalWrite(ledYell, LOW);
    digitalWrite(ledRed, LOW);
    digitalWrite(ledTick, LOW);
    digitalWrite(ledCross, LOW);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("All LED's Lit?");
    lcd.setCursor(3, 1);
    lcd.print("Press NEXT");
    digitalWrite(buttonPinNextLed, LOW);    // set the LED on
    delay(500);
  }
  else {
    leds();
  }
}
void allComplete() {
  buttonNext = digitalRead(buttonPinNext);   // read the state of the pushbutton value
  if (buttonNext == LOW) {
    digitalWrite(ledGreen, HIGH);
    digitalWrite(ledYell, HIGH);
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledTick, HIGH);
    digitalWrite(ledCross, HIGH);
    digitalWrite(buttonPinNextLed, HIGH);    // set the LED off
    delay(500);                              // wait for a half second
  }
  else {
    allComplete();
  }
}