// include libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//define oled display parameters
#define screen_width 128 // OLED DISPLAY WIDTH IN PIXELS
#define screen_height 64 // OLED DISPLAY HEIGHT IN PIXELS
#define oled_reset -1 // REST PIN
#define screen_address 0x3C // SEE DATA SHEET FOR ADDRESS
// declare objects
Adafruit_SSD1306 display(screen_width , screen_height ,&Wire , oled_reset);
//declare 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(;;);
}
// turn on OLED DISPLAY
display.display();
delay(2000);
// clear OLED DISPLAY
display.clearDisplay();
print_line("Welcome to Medibox!" , 10 ,20, 2);
display.clearDisplay();
}
void loop() {
// put your main code here, to run repeatedly:
update_time();
print_time_now();
}
void print_line(String text,int column , int row, int text_size ){
//display a custom message
display.setTextSize (text_size);
display.setTextColor(SSD1306_WHITE);
display.setCursor(column,row) ; // COLUMN , ROW
display.println(text);
display.display();
}
void print_time_now(void){
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; // timeLast = timeLast + 1
}
if (minutes==60){
hours +=1; // hours = hours +1
minutes =0 ;
}
if ( hours == 24){
days +=1; // days = days +1
hours = 0;
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306