//#include <GyverTimers.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
int totalsecond = 0;
int second = 0;
int tap = 0;
int usbrst = 0;
int timeset;
int timersecond = 0;
int enablebutton;
int timerstate = 0;
int offstate = 0;
static byte count = 0;
void setup()
{
Serial.begin(9600);
//Timer1.setFrequency(1);
//Timer1.enableISR();
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
startMillis = millis();
pinMode(8, OUTPUT);digitalWrite(8, HIGH);//Charger IC Enable
//pinMode(7, OUTPUT);digitalWrite(7, LOW);//Tine Set 2^1 bit
//pinMode(6, OUTPUT);digitalWrite(6, LOW);//Time Set 2^0 bit
//pinMode(5, OUTPUT);digitalWrite(5, HIGH);//Reset Indicator
//pinMode(4, OUTPUT);digitalWrite(4, LOW);//USB Hub Reset
pinMode(10, INPUT_PULLUP);//Time Set Button
pinMode(9, INPUT_PULLUP);//Enable Timer Button
delay(500); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 0); // position to display
oled.println("Ready"); // text to display
oled.display(); // show on OLED
}
void loop()
{
/*enablebutton = digitalRead(9);
currentMillis = millis();
if(enablebutton == LOW){
delay(500);
//get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
timersecond++;
digitalWrite(8, !digitalRead(8)); //if so, change the state of the LED. Uses a neat trick to change the state
Serial.println(timersecond);
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}*/
timeset = digitalRead(10);
enablebutton = digitalRead(9);
if(timeset == LOW){
delay(500);
tap++;
if(tap < 4){
totalsecond = 60*tap;
}
else{
tap = 0;
totalsecond = 0;
}
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Mode: ");oled.println(tap);
oled.print("Timer: ");oled.print(totalsecond);oled.println(" s");
oled.display();
}
if(enablebutton == LOW){
delay(100);
while(totalsecond != 0){
currentMillis = millis();
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
//currentMillis = millis();
//timersecond = totalsecond;
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Timer Setup: ");oled.print(totalsecond);oled.println(" s");
oled.display();
totalsecond--;
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}
timerstate = 0;
offstate = 1;
tap = 0;
digitalWrite(8, LOW);
oled.clearDisplay();
oled.setCursor(0, 0);
oled.println("Times Up");
oled.display();
}
}
/*ISR(TIMER1_A) {
if(timerstate == 1){
digitalWrite(8, HIGH);
if(timersecond != 0){
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Timer Setup: ");oled.print(totalsecond);oled.println(" s");
oled.display();
Serial.println(timersecond);
timersecond--;
}
else {
timerstate = 0;
offstate = 1;
tap = 0;
digitalWrite(8, LOW);
oled.clearDisplay();
oled.setCursor(0, 0);
oled.println("Times Up");
oled.display();
Timer1.disableISR();
//Timer2.enableISR(CHANNEL_A);
}
}
}*/