Hi all! Been working on a project, one that apparently where nothing wants to work properly. My current issue(Out of many...) Is if i leave my "Clock" for a little while, it all works and everything, but then the display just glitches/Skips. i will be posting a video to explain what i mean, and also my code.
As you can see, when every it lags or whatever you may call it, the "L" rapidly blinks twice. Mind you, its still keeping accurate time, its just... idk lagging
Currently using Oled 128x64 display, rtc ds3231 and Arduino nano
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#include <Servo.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;
char message[]=" ";
int x, minX;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup() {
Serial.begin(9600);
delay(3000); // wait for console opening
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(2023, 4, 22, 10, 22, 00));
}
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(SSD1306_BLACK);
display.drawRect(117, 25, 3, 3, BLACK); // Put degree symbol ( ° )
display.setTextSize(2);
display.setTextColor(WHITE);
display.setTextWrap(false);
x = display.width();
minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2
}
void loop() {
DateTime now = rtc.now();
/*============Display Date=================*/
display.fillRect(0,0,128,16,SSD1306_BLACK);
display.fillRect(0,16,128,28,SSD1306_BLACK);
//display.fillRect(0,28,128,46,SSD1306_BLACK);
display.fillRect(0,46,128,64,SSD1306_BLACK);
/* Display appropriate message based on time of day */
if (now.hour() >= 24 || now.hour() < 8) { // 11pm till 8am
display.setCursor(x, 48);
display.print("Have a great morning, Twig!");
} else if (now.hour() >= 8 && now.hour() < 19) { // 8am till 7pm
display.setCursor(x, 48);
display.print("Have a good day!!!");
} else if (now.hour() >= 19 && now.hour() < 24) { // 7pm till 11pm
display.setCursor(x, 48);
display.print("Night Night!");
}
/* Update scrolling message */
x = x - 4; // scroll speed, make more positive to slow down the scroll
if (x < minX) x = display.width();
display.setTextSize(2);
display.setCursor(x, 48);
display.print(message);
display.setTextSize(1);
display.setCursor(0,0);
display.setTextColor(SSD1306_WHITE);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
char currentDate [16];
uint8_t thisDay, thisMonth ;
thisDay = now.day();
thisMonth = now.month();
sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month
display.setTextSize(1);
display.setCursor(62,0);
display.setTextColor(SSD1306_WHITE);
display.print(currentDate);
display.setTextSize(1);
display.setCursor(102,0);
display.print(now.year(), DEC);
/*================Display Time================*/
char buffer[16]; // Declare buffer before using it
uint8_t thisSec, thisMin;
thisSec = now.second();
thisMin = now.minute();
uint8_t thisHour = now.hour();
bool isPM = thisHour >= 12; // determine if it's PM
thisHour = thisHour % 12; // convert to 12-hour format
if (thisHour == 0) {
thisHour = 12; // 12 midnight is 12am
}
sprintf(buffer, "%2d:%02d:%02d", thisHour, thisMin, thisSec);// format time with AM/PM indicator
display.setTextSize(2);
display.setCursor(5,20);
display.setTextColor(WHITE);
display.print(buffer);
sprintf(buffer, "%s", isPM ? "PM" : "AM");
display.setTextSize(2);
display.setCursor(105,15);
display.setTextColor(WHITE);
display.print(buffer);
display.display();
}