//Libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//Init libraries objects
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;
//Constants
char daysOfTheWeek[7][12] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int btSet = 12;
const int btUp = 11;
const int btDown = 10;
const int buzzer = 9;
//Variables
int DD,MM,YY,H,M,S, set_state, up_state, down_state;
int btnCount = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis;
String sDD;
String sMM;
String sYY;
String sH;
String sM;
String sS;
boolean backlightON = true;
boolean setupScreen = false;
// logo with start of arduino
//static const unsigned char PROGMEM logo_bmp[] =
//{ 0b00000000, 0b11000000,
// 0b00000001, 0b11000000,
// 0b00000001, 0b11000000,
// 0b00000011, 0b11100000,
// 0b11110011, 0b11100000,
// 0b11111110, 0b11111000,
// 0b01111110, 0b11111111,
// 0b00110011, 0b10011111,
// 0b00011111, 0b11111100,
// 0b00001101, 0b01110000,
// 0b00011011, 0b10100000,
// 0b00111111, 0b11100000,
// 0b00111111, 0b11110000,
// 0b01111100, 0b11110000,
// 0b01110000, 0b01110000,
// 0b00000000, 0b00110000 };
void setup() {
pinMode(btSet, INPUT_PULLUP);
pinMode(btUp, INPUT_PULLUP);
pinMode(btDown, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
Serial.begin(57600);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
//if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
// Serial.println(F("SSD1306 allocation failed"));
//}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
// display.display();
// delay(1000);
// display.clearDisplay();
}
//void text() {
// display.clearDisplay(); // Clear display buffer
// display.setTextColor(SSD1306_WHITE);
// display.setTextSize(1);
// display.setCursor(0,0);
// display.print("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
// display.setCursor(0,16);
// display.print("AHOJ");
//}
void loop() {
//currentMillis = millis();
//readBtns();
getTimeDate();
// if (!setupScreen){
//Show();
// rtc.adjust(DateTime(2024, 01, 11, 14, 45, 0));
// }
// else{
// Setup();
// }
if (set_state==LOW || up_state==LOW || down_state==LOW) {
digitalWrite(buzzer, HIGH);
}
else {
digitalWrite(buzzer, LOW);
}
}
//Read buttons
void readBtns(){
set_state = digitalRead(btSet);
up_state = digitalRead(btUp);
down_state = digitalRead(btDown);
if (set_state==LOW){
if(btnCount<5){
btnCount++;
setupScreen = true;
if(btnCount==1){
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print("------SET------");
display.setCursor(0,15);
display.print("-TIME and DATE-");
delay(1000);
display.clearDisplay();
}
}
else{
display.clearDisplay();
rtc.adjust(DateTime(YY, MM, DD, H, M, 0));
display.setTextColor(SSD1306_WHITE);
display.setTextSize(3);
display.setCursor(0,14);
display.print("Saving....");
delay(1000);
display.clearDisplay();
setupScreen = false;
btnCount=0;
}
delay(500);
}
}
//Read time and date from rtc ic
void getTimeDate(){
//if (!setupScreen){
DateTime now = rtc.now();
// DD = now.day();
// MM = now.month();
// YY = now.year();
// H = now.hour();
// M = now.minute();
// S = now.second();
//}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(500);
//Make some fixes...
// if (DD<10){ sDD = '0' + String(DD); } else { sDD = DD; }
// if (MM<10){ sMM = '0' + String(MM); } else { sMM = MM; }
// sYY=YY;
// if (H<10){ sH = '0' + String(H); } else { sH = H; }
// if (M<10){ sM = '0' + String(M); } else { sM = M; }
// if (S<10){ sS = '0' + String(S); } else { sS = S; }
}
void Show(){
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0,16);
display.print("Cas - ");
// display.print(H);
// display.print(":");
// display.print(M);
// display.print(":");
// display.print(S);
// display.setCursor(0,25);
// display.print("Datum - ");
// display.print(DD);
// display.print("/");
// display.print(MM);
// display.print("/");
// display.print(YY);
display.setCursor(0,25);
display.print("Ahoj");
display.print(" ");
display.print("ako");
display.print(" ");
display.print("sa");
display.print(" ");
display.print("mas");
display.display();
}