Hi, Ive been trying to make this clock for a few weeks now and rather than creating a new topic at every road block I've been able to get it pretty close to how I want it by purely banging my head into my computer. However, at this point some problems are arriving.
Im using a DS3231
Arduino Uno
Cheap 128x64 .96" oled
It's my first project.
I apologize if there are too many questions for a single post and I'm happy do delete if I'm breaking any rules. In the off chance this post is acceptable here are my questions.
First, I thought I had it printing in a 12 hour format until midnight last night it displayed 0:00. Based on what I've provided is anyone able to help me correct this? I'm okay at copy/paste but the simplest explanations will take me some time to soak up.
Second, it will go into the dashboard of my car so I realized I will need buttons to set it so I don't have to remove my dashboard every day light savings or if the rechargeable LIR2032 ever dies (will it die?). My question is, is it possible to add buttons to this existing code or do I need to rewrite the whole thing? When I tried some people's codes for adding buttons they never included rtc.now or now.hour (which I think gets the time automatically from my computer?). Also, I've deleted the year because I don't typically need to see that but will the clock need it to keep track of itself when I'm setting it?
Third, this beautiful additional adafruit_GFX font is nearly perfect aesthetically but quite a memory hog bringing me up to 83% of program storage space. Will adding buttons push it over the limit?
Please let me know if I should delete the post and go back to banging my head. And thanks either way, if not for being able to assist me, then for existing, this forum has already been such great help!
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#include <DS3231.h>
#include <Fonts/FreeMonoBold24pt7b.h>
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Lord's Day", "Mondizzle", "Tuesdizzle", "Winesday", "NewFriday", "Fridizzle", "Caturday"};
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3c
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
display.setRotation(1);
Serial.begin(9600);
delay(3000); // wait for console opening
display.setFont();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
display.clearDisplay();
display.display(); // this command will display all the data which is in buffer
display.setTextColor(WHITE, BLACK);
}
void loop() {
DateTime now = rtc.now();
/*============Display Date=================*/
display.setFont();
display.setTextSize(1);
display.setCursor(4, 5);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
char currentDate [16];
uint8_t thisDay, thisMonth ;
thisDay = now.day();
thisMonth = now.month();
sprintf (currentDate, "%02d.%02d", thisMonth, thisDay); //add leading zeros to the day and month
display.setTextSize(2);
display.setCursor(3, 103);
display.print(currentDate);
display.setTextSize(2);
display.setCursor(0, 85);
/*================Display Time================*/
char buffer [16];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = now.hour();
if (thisHour > 12) thisHour = thisHour - 12;
if (thisHour == 0) thisHour == 12; {
}
if (now.hour() >= 18) {
display.dim(true);
}
else if (now.hour() <= 5) {
display.dim(true);
}
else {
display.dim(false);
}
sprintf (buffer, "%2d%02d", thisHour, thisMin);
display.setFont (&FreeMonoBold24pt7b);
display.setTextSize(1);
display.setCursor(2, 47);
display.print(buffer);
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buffer, 2, 47, &x1, &y1, &w, &h);
display.fillRect(x1, y1, w + 2, h + 2, BLACK);
sprintf (buffer, "%2d%02d", thisHour, thisMin);
display.setFont (&FreeMonoBold24pt7b);
display.setTextSize(1);
display.setCursor(2, 47);
display.print(buffer);
display.display();
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
display.setCursor(x_pos, y_pos);
display.setTextSize(text_size);
display.print(text);
display.display();
}