/*
You can use a small arduino
To set the time on the switch and press the black button to scroll the menu.
Set the Datetime values to your liking
If you see the Set all menu, press the green button to set.
*/
#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
static int second,minute,hour,day = 1,month = 1;
static long year = 2010;
static int ss,mm,hh,dd = 1,mn = 1;
static long yy = 2010;
uint32_t previous,prev;
//for menu
int buttonP;
//for debounce delay
long debounceDel = 100;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setTime(int y, int n, int d, int h, int m, int s){
year = y;
month = n;
day = d;
hour = h;
minute = m;
second = s;
previous = micros();
}
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
//buttons
pinMode(12, INPUT_PULLUP); //for menu display (black)
pinMode(11, INPUT_PULLUP); //for setting time (green)
pinMode(10, INPUT_PULLUP); //for ++ (blue)
pinMode(9, INPUT_PULLUP); //for -- (red)
display.clearDisplay();
}
void refreshDT(){
if(micros() - previous >= 1000000){
++second;
previous = micros();
}
if(second > 59){
second = 0;
++minute;
}
if(minute > 59){
minute = 0;
++hour;
}
if(hour > 23){
hour = 0;
++day;
}
}
void loop() {
refreshDT();
if(buttonP == 0){
displayDT();
}
else if(buttonP == 1){
displaySY();
}
else if(buttonP == 2){
displaySN();
}
else if(buttonP == 3){
displaySD();
}
else if(buttonP == 4){
displaySH();
}
else if(buttonP == 5){
displaySM();
}
else if(buttonP == 6){
displayST();
}
if(!digitalRead(12)){
delay(debounceDel);
++buttonP;
if(buttonP >= 7){
buttonP = 0;
}
}
}
void displayDT(){
if(micros() - prev >= 500000){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(14, 6);
char tbuffer[40];
char dbuffer[40];
sprintf(tbuffer, "%02d:%02d:%02d", hour,minute,second);
display.println(tbuffer);
display.display();
display.setCursor(14, 46);
sprintf(dbuffer, "%02d-%02d-%02d", day,month,year%100);
display.println(dbuffer);
display.display();
prev = micros();
}
}
void displaySY(){
delay(debounceDel);
display.clearDisplay();
display.setCursor(0, 6);
display.println("Set Year");
display.setCursor(0,30);
display.println(yy);
display.display();
if(!digitalRead(10)){
++yy;
}
if(!digitalRead(9)){
--yy;
}
}
void displaySN(){
delay(debounceDel);
display.clearDisplay();
display.setCursor(0, 6);
display.println("Set Month");
display.setCursor(0,30);
display.println(mn);
display.display();
if(!digitalRead(10)){
++mn;
}
if(!digitalRead(9)){
--mn;
}
if(mn >= 13){
mn = 1;
}
if(mn <= 0){
mn = 12;
}
}
void displaySD(){
delay(debounceDel);
display.clearDisplay();
display.setCursor(0, 6);
display.println("Set DOM");
display.setCursor(0,30);
display.println(dd);
display.display();
if(!digitalRead(10)){
++dd;
}
if(!digitalRead(9)){
--dd;
}
if(dd >= 32){
dd = 1;
}
if(dd <= 0){
dd = 31;
}
}
void displaySH(){
delay(debounceDel);
display.clearDisplay();
display.setCursor(0, 6);
display.println("Set Hour");
display.setCursor(0,30);
display.println(hh);
display.display();
if(!digitalRead(10)){
++hh;
}
if(!digitalRead(9)){
--hh;
}
if(hh >= 24){
hh = 1;
}
if(hh <= -1){
hh = 23;
}
}
void displaySM(){
delay(debounceDel);
display.clearDisplay();
display.setCursor(0, 6);
display.println("Set Minute");
display.setCursor(0,30);
display.println(mm);
display.display();
if(!digitalRead(10)){
++mm;
}
if(!digitalRead(9)){
--mm;
}
if(mm >= 60){
mm = 1;
}
if(mm <= -1){
mm = 59;
}
}
void displayST(){
display.clearDisplay();
display.setCursor(0, 6);
display.println("Set All");
display.display();
if(!digitalRead(11)){
setTime(yy,mn,dd,hh,mm,ss);
}
}