#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "U8glib.h"
#define OLED_RESET -1
const int buttonPin1 = 2;
int buttonState1 = 0;
const int buttonPin2 = 3;
int buttonState2 = 0;
int hours = 9;
int minutes = 30;
int seconds = 0;
char *number1[12]={"6","5","4","3","2","1","12","11","10","9","8","7"};
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;
float radius = min(SCREEN_HEIGHT, SCREEN_WIDTH)/2-1;
const int X_CENTER = SCREEN_WIDTH / 2;
const int Y_CENTER = SCREEN_HEIGHT / 2;
int x1, y1, x2, y2;
double angle;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void draw(void) {
//u8g.drawCircle(X_CENTER, Y_CENTER, radius);
u8g.drawCircle(X_CENTER, Y_CENTER, 1);
//draw minute's ticks (60 lines)
for(int j=1; j<=60; j++){
angle = j*6;
angle = angle * 0.0174533;
x1 = X_CENTER + (sin(angle) * radius);
y1 = Y_CENTER + (cos(angle) * radius);
x2 = X_CENTER + (sin(angle) * (radius));
y2 = Y_CENTER + (cos(angle) * (radius));
u8g.drawLine(x1,y1,x2,y2);
}
//draw hour's ticks (12 lines)
for(int j=0; j<12; j++){
angle = j*30;
angle = angle * 0.0174533;
x1 = X_CENTER + (sin(angle) * radius);
y1 = Y_CENTER + (cos(angle) * radius);
x2 = X_CENTER + (sin(angle) * (radius - 4));
y2 = Y_CENTER + (cos(angle) * (radius - 4));
u8g.drawLine(x1,y1,x2,y2);
//draw hour digits(12 lines)
x2 = X_CENTER + (sin(angle) * (radius - 8));
y2 = Y_CENTER + (cos(angle) * (radius - 8));
u8g.setFont(u8g_font_chikita);
char buffer[3];
sprintf(buffer, "%s", number1[j]);
u8g.drawStr(x2 - 2, y2 + 3, buffer);
angle = seconds*6;
angle = angle * 0.0174533;
x2 = X_CENTER + (sin(angle) * (radius - 1));
y2 = Y_CENTER - (cos(angle) * (radius - 1));
u8g.drawLine(X_CENTER,Y_CENTER,x2,y2);
angle = minutes*6;
angle = angle * 0.0174533;
x2 = X_CENTER + (sin(angle) * (radius - 10));
y2 = Y_CENTER - (cos(angle) * (radius - 10));
u8g.drawLine(X_CENTER,Y_CENTER,x2,y2);
angle = hours*30 + ((minutes / 12) * 6);
angle = angle * 0.0174533;
x2 = X_CENTER + (sin(angle) * (radius / 2));
y2 = Y_CENTER - (cos(angle) * (radius / 2));
u8g.drawLine(X_CENTER,Y_CENTER,x2,y2);
}
}
void setup() {
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display(); // Display startup message
delay(2000);
display.clearDisplay(); // Clear the buffer
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// rebuild the picture after some delay
delay(500);
if (buttonState1 == LOW) {
seconds +=1;
if(seconds==60){ seconds=seconds-60; minutes +=1;}
if(minutes==60){ minutes=0; hours +=1;}
if(hours==24){hours=1;}
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
//delay(800);
} else if (buttonState1 == HIGH){
display.setCursor(0, 0);
display.print("medicine1 at ");
display.println("7am");
display.setCursor(0,25);
display.print("medicine2 at ");
display.print("1pm");
display.setCursor(0,50);
display.print("medicine3 at ");
display.println("9pm");
display.display(); // Show the text
delay(500); }
else if (buttonState2 == LOW)
{ // Check if the new button is pressed
seconds +=1;
if(seconds==60){ seconds=seconds-60; minutes +=1;}
if(minutes==60){ minutes=0; hours +=1;}
if(hours==24){hours=1;}
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}else if (buttonState2 == HIGH){
display.setCursor(0, 0);
display.print("date1");
display.display(); // Show the text
delay(500);
}
}