#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
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address of the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Global variables
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
unsigned long timeNow = 0;
unsigned long timeLast = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(2000);
display.clearDisplay();
print_line("Welcome to Medibox!", 10, 20, 2);
}
void loop() {
update_time();
print_time_now();
}
void print_line(String text, int column, int row, int textsize) {
display.setTextSize(textsize);
display.setTextColor(SSD1306_WHITE);
display.setCursor(column, row);
display.println(text);
display.display();
}
void print_time_now() {
display.clearDisplay();
print_line(String(days), 0, 0, 2);
print_line(":", 20, 0, 2);
print_line(String(hours), 30, 0, 2);
print_line(":", 50, 0, 2);
print_line(String(minutes), 60, 0, 2);
print_line(":", 80, 0, 2);
print_line(String(seconds), 90, 0, 2);
}
void update_time() {
timeNow = millis() / 1000; //seconds passed from bootup
seconds = timeNow - timeLast;
if (seconds >= 60) {
minutes += 1; //minutes = minutes +1
timeLast += 60;
}
if (minutes == 60) {
hours += 1;
minutes = 0;
}
if (hours == 24) {
days += 1;
hours = 0;
}
}