#include <WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
#include <Arduino.h>
// 'myGun', 128x64px
const unsigned char myGun [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1f, 0xf0, 0x07, 0xc0, 0xf8, 0x0f, 0x01, 0xf0, 0x1f, 0xe0, 0x0f, 0x80, 0xff, 0x80, 0x3e, 0x00,
0x1f, 0xfc, 0x1f, 0xf0, 0xfc, 0x0f, 0x87, 0xfc, 0x3f, 0xf0, 0x3f, 0xe0, 0xff, 0xe0, 0xff, 0x80,
0x1f, 0xfe, 0x3f, 0xf8, 0xfe, 0x1f, 0x8f, 0xfe, 0x7f, 0xf8, 0x7f, 0xf0, 0xff, 0xf1, 0xff, 0xc0,
0x1f, 0xfe, 0x7e, 0xfc, 0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, 0x06, 0x01, 0xe0, 0x3f, 0xc0, 0x0f, 0x81, 0xf8, 0xe0, 0x3e, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x1c, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0xe0, 0x0f, 0x00, 0x3e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x0b, 0x00, 0x63, 0x00, 0x00,
0x00, 0x00, 0x18, 0xe3, 0x86, 0xcf, 0x18, 0x78, 0x6c, 0x00, 0x60, 0x03, 0x00, 0x63, 0x00, 0x00,
0x00, 0x00, 0x1c, 0xc7, 0xc7, 0xdf, 0x18, 0xfc, 0x7e, 0x00, 0x60, 0x03, 0x00, 0x63, 0x00, 0x00,
0x00, 0x00, 0x0c, 0xcc, 0x67, 0x18, 0x19, 0x8e, 0x67, 0x00, 0x60, 0x03, 0x00, 0x63, 0x00, 0x00,
0x00, 0x00, 0x0c, 0x8f, 0xe6, 0x1e, 0x19, 0x86, 0x63, 0x00, 0x60, 0x03, 0x00, 0x63, 0x00, 0x00,
0x00, 0x00, 0x0d, 0x8f, 0xe6, 0x0f, 0x19, 0x86, 0x63, 0x00, 0x60, 0x03, 0x00, 0x63, 0x00, 0x00,
0x00, 0x00, 0x07, 0x8c, 0x06, 0x03, 0x19, 0x8e, 0x63, 0x00, 0x60, 0x83, 0x04, 0x63, 0x00, 0x00,
0x00, 0x00, 0x07, 0x07, 0xc6, 0x1f, 0x18, 0xfc, 0x63, 0x00, 0x61, 0xc3, 0x0e, 0x3e, 0x00, 0x00,
0x00, 0x00, 0x07, 0x03, 0xc6, 0x1e, 0x18, 0x70, 0x63, 0x00, 0x60, 0x83, 0x04, 0x1c, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#define EEPROM_ADDRESS_1 0
#define EEPROM_ADDRESS_2 (EEPROM_ADDRESS_1 + sizeof(int))
#define DATA_SIZE sizeof(int)
int valueToStore1 = 10;
int valueToStore2 = 5;
int flag=0;
long start =millis();
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SW_PIN 5
#define PLUS_PIN 16
#define MINUS_PIN 17
#define BUZZER_PIN 18
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000);
int timeleft=600;
int resttimeleft=0;
int screen = 0;
bool switchPressed = false;
bool lastSwitchState = HIGH;
bool currentSwitchState;
int workValue = 25;
int restValue = 5;
char saveValue = 'N'; // 'N' initially
enum Mode {
WORK,
REST,
SAVE
};
Mode currentMode = WORK;
void setup()
{
pinMode(SW_PIN, INPUT);
pinMode(PLUS_PIN, INPUT);
pinMode(MINUS_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
timeClient.begin();
int storedValue1, storedValue2;
EEPROM.begin(DATA_SIZE * 2); // Multiply by 2 because we're storing two values
EEPROM.get(EEPROM_ADDRESS_1, storedValue1);
EEPROM.get(EEPROM_ADDRESS_2, storedValue2);
// Check if the stored values are valid
if (storedValue1 != valueToStore1 || storedValue2 != valueToStore2) {
// If not, store the new values
Serial.println("Storing new values in EEPROM...");
EEPROM.put(EEPROM_ADDRESS_1, valueToStore1);
EEPROM.put(EEPROM_ADDRESS_2, valueToStore2);
EEPROM.commit();
} else {
Serial.println("Values already stored in EEPROM.");
}
EEPROM.end();
Logo();
if (digitalRead(SW_PIN) == HIGH)
{
while (saveValue != 'Y') {
if (digitalRead(SW_PIN) == HIGH) {
switch (currentMode) {
case WORK:
currentMode = REST;
break;
case REST:
currentMode = SAVE;
break;
case SAVE:
if (saveValue == 'Y') {
// Exit the loop
return;
}
currentMode = WORK;
break;
}
}
if (currentMode != SAVE) {
if (digitalRead(PLUS_PIN) == HIGH) {
incrementValue();
}
if (digitalRead(MINUS_PIN) == HIGH) {
decrementValue();
}
} else {
if (digitalRead(PLUS_PIN) == HIGH || digitalRead(MINUS_PIN) == HIGH) {
toggleSaveValue();
}
}
displayValues();
}
}
}
void loop() {
timeClient.update();
currentSwitchState = digitalRead(SW_PIN);
if (currentSwitchState != lastSwitchState) {
delay(50); // Debounce delay
if (currentSwitchState == LOW) {
switchPressed = true;
}
}
lastSwitchState = currentSwitchState;
if (switchPressed) {
switchPressed = false;
screen = (screen + 1) % 3; // Cycling through three screens
}
if (timeleft<590 && flag==0)
{
buzzrest() ;
}
if (timeleft<590 && flag==1)
{
timerrest() ;
}
switch (screen) {
case 0: // Indian Standard Time
display.clearDisplay();
display.setRotation(0);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Time");
display.setCursor(80, 0);
display.print("(1/3)");
display.setCursor(0, 20);
display.setTextSize(3);
display.println(timeClient.getFormattedTime());
display.display();
break;
case 1: // Timer
display.clearDisplay();
display.setRotation(0);
display.setCursor(0, 0);
display.setTextSize(1);
display.print("Timer");
display.setCursor(80, 0);
display.print("(2/3)");
display.setCursor(0, (SCREEN_HEIGHT - 16) / 2); // Center text vertically
display.setTextSize(3);
timeleft=(valueToStore1*60) - ((millis()-start)/1000);
display.println(timeleft);
display.display();
break;
case 2: // SETUP
display.clearDisplay();
display.setRotation(0);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Settings");
display.setCursor(80, 0);
display.print("(3/3)");
display.setCursor(0, (SCREEN_HEIGHT - 16) / 2); // Center text vertically
display.setTextSize(3);
display.println("SETUP");
display.display();
break;
}
}
void Logo(void)
{
for(int i=-1;i<=128;i=i+10)
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(30,0);
display.drawBitmap(0, 0, myGun, 128, 64, WHITE); // display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
display.fillRect(-1,59,i,6,SSD1306_WHITE);
display.display();
}
}
void buzzrest(void)
{
digitalWrite(BUZZER_PIN, HIGH);
delay(1000); // Wait for 1 second
// Turn off the buzzer
digitalWrite(BUZZER_PIN, LOW);
delay(1000); // Wait for 1 second
digitalWrite(BUZZER_PIN, HIGH);
delay(1000); // Wait for 1 second
// Turn off the buzzer
digitalWrite(BUZZER_PIN, LOW);
delay(1000); // Wait for 1 second
flag=1;
}
void timerrest(void)
{
display.clearDisplay();
display.setRotation(0);
display.setCursor(15, (SCREEN_HEIGHT - 16) / 2); // Center text vertically
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.println("RELAX!");
display.display();
delay(valueToStore2*60000);
}
void incrementValue() {
switch (currentMode) {
case WORK:
workValue++;
break;
case REST:
restValue++;
break;
default:
break;
}
}
void decrementValue() {
switch (currentMode) {
case WORK:
workValue--;
break;
case REST:
restValue--;
break;
default:
break;
}
}
void toggleSaveValue() {
saveValue = (saveValue == 'Y') ? 'N' : 'Y';
}
void displayValues() {
display.clearDisplay();
display.setTextSize(1);
// Display mode values as a list
display.setCursor(0, 0);
display.println("----- SETUP -----");
display.setCursor(0, 20);
// Display Work Time
display.setTextColor(SSD1306_WHITE);
display.print("1. Work Time: ");
if (currentMode == WORK) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Highlighted text
} else {
display.setTextColor(SSD1306_WHITE); // Default text color
}
display.println(workValue);
// Display Rest Time
display.setTextColor(SSD1306_WHITE);
display.print("2. Rest Time: ");
if (currentMode == REST) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Highlighted text
} else {
display.setTextColor(SSD1306_WHITE); // Default text color
}
display.println(restValue);
// Display Save Option
display.setTextColor(SSD1306_WHITE);
display.print("3. Save(Y/N) :");
if (currentMode == SAVE) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Highlighted text
} else {
display.setTextColor(SSD1306_WHITE); // Default text color
}
display.println(saveValue);
display.display();
display.setTextColor(SSD1306_WHITE);
}