/*****************************************************************
	access timer with 128x64 I2C Oled display, light & sound signal
                        by Steve Barth 2021
 *****************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define start_button 9    // connect start/stop button
#define Led_Green 10    // connect green led
#define Led_Red 11    //connect red led
#define buzzer 12   //connect speaker  
int timerMode = 0;    // data type for timer mode
long startTime;   // data type for starting time
bool Led_Red_state;   // data type for red led state


// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// (SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, OLED_RESET)
Adafruit_SSD1306 oled(128, 64, & Wire, 4);



/**************   SETUP   ***************/


void setup() {

  oled.setRotation(2);    // oled screen display rotation
  //Serial.begin(9600);   // serial monitor init
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3D);   // OLED display initialize, address setting
  oled.clearDisplay();    // clear screen
  oled.display();   // display enable
  delay(100);   // Pause 0.1 seconds for display
  pinMode(start_button, INPUT_PULLUP);  // setting pinmode with pullup resistor
  pinMode(Led_Green, OUTPUT);   // setting pinmode output for green led
  digitalWrite(Led_Green, LOW);   // green led off
  pinMode(Led_Red, OUTPUT);   // setting pinmode output for red led
  digitalWrite(Led_Red, LOW);   // red led off
  pinMode(buzzer, OUTPUT);    // setting pinmode output for speaker

}


/**************   LOOP   ***************/


void loop() {

  noTone(12);   // sound off
  Led_Red_state = 0;    // red led mode status 0

  if (timerMode == 0 ) {   // if red led "0" mode status
    oled.clearDisplay();    // clear screen
    oled.setTextSize(1);    // setting text size
    oled.setTextColor(WHITE); // text color (black background, white foreground)
    oled.setCursor(30, 8);  // cursor position
    oled.print("PLEASE NEXT,");   // text on the display
    oled.setCursor(35, 25);  // cursor position
    oled.print("STOP  NOW!");   // text on the display
    oled.display();   // display enable
  }


  if (digitalRead(start_button) == LOW) {    // if push button
    delay(150);    // delay for no bounce
    startTime = millis(); // start time settings
    timerMode++;   // I increase the mode status by 1
    //Serial.print("timerMode: ");    // serial monitor display text
    //Serial.println(timerMode);    // serial monitor display mode state
  }


  if (timerMode == 1) { // if red led "1" mode status
    oled.clearDisplay();    // clear screen
    oled.setTextSize(1);    // setting text size
    oled.setTextColor(WHITE); // text color (black background, white foreground)
    oled.setCursor(4, 26);  // cursor position
    oled.print("(Measurment: 5 sec.)");   // text on the display
    oled.setTextSize(2);    // setting text size
    oled.setTextColor(WHITE); // text color (black background, white foreground)
    oled.setCursor(15, 40);  // cursor position
    oled.print((millis() - startTime) / 1000.0);
    //Serial.println(millis()-startTime);
    oled.setCursor(70, 40);  // cursor position
    oled.print("sec.");   // text on the display
    oled.display();   // display enable


    if ((millis() - startTime) >= 5001) {  // time max.: 5 sec
      timerMode++;   // I increase the mode status by 1
    }
  }


  else if (50 <= (millis() - startTime) <= 4950 && timerMode >= 1) {  // if time between 0,05s-4,95s
    digitalWrite(Led_Red, HIGH);   // red led on
    Led_Red_state = 1;  // red led mode status 1
  }


  if (timerMode > 1 ) {    // if mode status bigger 1
    //Serial.print("timerMode: ");
    //Serial.println(timerMode);


    if (Led_Red_state == 0) {    // if red led  mode status 0
      oled.setTextSize(1);    // setting text size
      oled.setTextColor(WHITE); // text color (black background, white foreground)
      oled.setCursor(0, 6);  // cursor position
      oled.print("All right! PLEASE, GO");   // text on the display
      oled.display();   // display enable
      digitalWrite(Led_Green, HIGH);
      //Serial.println("All right! Please, GO!");
      tone(12, 900, 400);   // Plays 800 Hz tone for 0.4 seconds
      delay(100);   // 0,12 seconds delay
      tone(12, 1200, 400);  // Plays 1kHz tone for 0.4 seconds
      delay(75);   // 0,1 seconds delay
      tone(12, 1500, 400);  // Plays 1,2kHz tone for 0.4 seconds
      delay(50);   // 0,08 seconds delay
      noTone(buzzer);   // sound off
    }


    if (Led_Red_state == 1) {    // if red led  mode status 1
      oled.setTextSize(1);    // setting text size
      oled.setTextColor(WHITE); // text color (black background, white foreground)
      oled.setCursor(2, 6);  // cursor position
      oled.print("It's not okay, AGAIN!");   // text on the display
      oled.display();   // display enable
      //Serial.println("It's not okay, AGAIN!");
      tone(12, 330, 600); // Plays 330Hz tone for 0.6 seconds
      delay(600);   // 0,6 seconds delay
      noTone(buzzer);   // sound off

    }


    delay (1600);   // 1,6 seconds delay before clear screen
    oled.clearDisplay();    // clear screen
    digitalWrite(Led_Red, LOW);
    digitalWrite(Led_Green, LOW);
    //delay(100);
    timerMode = 0;    // set mode status 0
    //Serial.print("timerMode: ");
    //Serial.println(timerMode);
  }
}