Created
April 8, 2022 11:15
-
-
Save pankaj-gecko/432a2fe488fca51342bde68ce54f62e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <TimeLib.h> | |
#include <DS1307RTC.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 32 // OLED display height, in pixels | |
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); | |
const char *monthName[12] = { | |
"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
}; | |
tmElements_t tm; | |
void setup() { | |
bool parse=false; | |
bool config=false; | |
// get the date and time the compiler was run | |
if (getDate(__DATE__) && getTime(__TIME__)) { | |
parse = true; | |
// and configure the RTC with this info | |
if (RTC.write(tm)) { | |
config = true; | |
} | |
} | |
Serial.begin(9600); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
while (!Serial) ; // wait for Arduino Serial Monitor | |
delay(200); | |
if (parse && config) { | |
Serial.print("DS1307 configured Time="); | |
Serial.print(__TIME__); | |
Serial.print(", Date="); | |
Serial.println(__DATE__); | |
} else if (parse) { | |
Serial.println("DS1307 Communication Error :-{"); | |
Serial.println("Please check your circuitry"); | |
} else { | |
Serial.print("Could not parse info from the compiler, Time=\""); | |
Serial.print(__TIME__); | |
Serial.print("\", Date=\""); | |
Serial.print(__DATE__); | |
Serial.println("\""); | |
} | |
} | |
void loop() { | |
tmElements_t tm; | |
if (RTC.read(tm)) { | |
int prevSec; | |
String currentDateTime; | |
currentDateTime = (String) readTime(); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setTextSize(2); | |
display.setCursor(0,0); | |
display.print(currentDateTime); | |
Serial.println(currentDateTime); | |
display.display(); | |
} else { | |
if (RTC.chipPresent()) { | |
Serial.println("The DS1307 is stopped. Please run the SetTime"); | |
Serial.println("example to initialize the time and begin running."); | |
Serial.println(); | |
} else { | |
Serial.println("DS1307 read error! Please check the circuitry."); | |
Serial.println(); | |
} | |
delay(9000); | |
} | |
delay(1000); | |
} | |
bool getTime(const char *str) | |
{ | |
int Hour, Min, Sec; | |
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; | |
tm.Hour = Hour; | |
tm.Minute = Min; | |
tm.Second = Sec; | |
return true; | |
} | |
bool getDate(const char *str) | |
{ | |
char Month[12]; | |
int Day, Year; | |
uint8_t monthIndex; | |
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false; | |
for (monthIndex = 0; monthIndex < 12; monthIndex++) { | |
if (strcmp(Month, monthName[monthIndex]) == 0) break; | |
} | |
if (monthIndex >= 12) return false; | |
tm.Day = Day; | |
tm.Month = monthIndex + 1; | |
tm.Year = CalendarYrToTm(Year); | |
return true; | |
} | |
void print2digits(int number) { | |
if (number >= 0 && number < 10) { | |
Serial.write('0'); | |
} | |
Serial.print(number); | |
} | |
String readTime() { | |
tmElements_t tm; | |
if (RTC.read(tm)) { | |
String currentDateTime = returnTwoDigits(tm.Hour) + ":" + returnTwoDigits(tm.Minute) + ":" + returnTwoDigits(tm.Second) + "\n" + returnTwoDigits(tm.Day) + "/" + returnTwoDigits(tm.Month) + '/' + (String)tmYearToCalendar(tm.Year); | |
return currentDateTime; | |
} else { | |
if (RTC.chipPresent()) { | |
return "DS1307 stopped! Run setTime"; | |
} else { | |
return "DS1307 read error!"; | |
} | |
} | |
} | |
String returnTwoDigits(int number) { | |
if (number >= 0 && number < 10) { | |
return '0'+ (String) number; | |
} | |
return (String) number; | |
} | |
/* | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Time.h> //For using RTC 1306 Module on I2c bus | |
#include <DS1307RTC.h> | |
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); | |
void setup() { | |
Serial.begin(9600); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
// Show initial display buffer contents on the screen -- | |
// the library initializes this with an Adafruit splash screen. | |
display.display(); | |
} | |
void loop() { | |
int prevSec; | |
String currentDateTime; | |
currentDateTime = (String) readTime(); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setTextSize(2); | |
display.setCursor(0,0); | |
display.print(currentDateTime); | |
Serial.println(currentDateTime); | |
display.display(); | |
delay(1000); | |
} | |
String readTime() { | |
tmElements_t tm; | |
if (RTC.read(tm)) { | |
String currentDateTime = returnTwoDigits(tm.Hour) + ":" + returnTwoDigits(tm.Minute) + ":" + returnTwoDigits(tm.Second) + "\n" + returnTwoDigits(tm.Day) + "/" + returnTwoDigits(tm.Month) + '/' + (String)tmYearToCalendar(tm.Year); | |
return currentDateTime; | |
} else { | |
if (RTC.chipPresent()) { | |
return "DS1307 stopped! Run setTime"; | |
} else { | |
return "DS1307 read error!"; | |
} | |
} | |
} | |
String returnTwoDigits(int number) { | |
if (number >= 0 && number < 10) { | |
return '0'+ (String) number; | |
} | |
return (String) number; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment