I have not been able to get anyone to be interested in my request for migrating Nick's Pong Clock to the very nice 16x24 LED panel that Adafruit sells.
I am using two of these panels, just like Nick's clock, which uses a different panel from Sure with the same Holtek controller.
So, I have been trying with my very limited programming skills to try to make a very simple digits based clock of my own. I doubt I will ever understand enough to duplicate, or migrate Nick's clock myself. His clock is just really nice, and has many different modes. The one I am trying to duplicate is called "Normal". His font set is much nicer as well.
I have found pretty much everything I need here on the forums, and I am very grateful to those who have shared the snippets of programming that I have cobbled together to make this simple clock work, well mostly work.
I seem to have most of it all working as planned so far, but have run into a formatting problem that I just cannot seem to figure out.
The time, which displays in 12 hours mode, is formatted like 1:23:15 etc. there is a padded space in front of single digits in the hours section. It all seems to run as expected, until 12:00 a.m, or 12:00 p.m.
It then prints to the matrix with a padded space before the hours, though I have not explicitly called for one that I can tell, except in the case of single digit hours. If I comment out the padded space line, the 12 prints correctly, but then the single digit hours print justified all of the war to the left, which is not what I want. This pushes the incrementing rightmost digits of the seconds off the right side of the panel. All of the other hours seem to be correct.
Can anyone share a bit of time to take a look, and teach me what may wrong with this part of the code that I borrowed from the forums? The code I used was written for an LCD panel, and I adapted it from that.
Keep in mind that I have deviated from the wiring for this panel, to have it be compatible with the shield that I made for Nick's Pong Clock. Pretty simple to figure out.
I have run into similar issues trying to do a small clock with an LCD pane, which I never did solve in the past.
Any help would be appreciated.
Many thanks,
Kirby
Code: Select all
#include "HT1632.h"
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
#define DATA 10
#define WR 11
#define CS 4
#define CS2 5
HT1632LEDMatrix matrix = HT1632LEDMatrix(DATA, WR, CS, CS2);
void setup() {
// Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
// if (! rtc.isrunning()) {
// Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(__DATE__, __TIME__));
//Serial.begin(9600);
matrix.begin(HT1632_COMMON_16NMOS);
// matrix.fillScreen(); //Lights all pixels for 1/2 second, good for testing
//delay(500);
}
void loop() {
DateTime now = rtc.now();
matrix.fillRect(0,0,48,16,0); //This is an awesome bit that I picked up on the Adfruit forum!!!! Wow!!!!
// Eliminates the annoying flicker casused by using a repetitive clearscreen
// Wonder if it will help scrolling text? - Yes it does.
// draw some text!
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextColor(1); // 'lit' LEDs
//Experimental Padding for hours, and conversion to 12hr -- Think it works, have to wait until 10!!
// Had to try a whole new bit of programming here. Maybe I did not copy it right first attempts.
// Still need to see what happens at midnight, and then at 1 p.m. to see if it works.
const uint8_t h = now.hour();
const uint8_t hr_12 = h%12;
matrix.setCursor(0,0); // Functions to move the cursor to first line first position
if(hr_12 < 10){ // Zero padding if value less than 10 ie."09" instead of "9"
matrix.print(" ");
matrix.print((h > 12) ? h - 12 : ((h == 0) ? 12 : h), DEC); // Conversion to AM/PM
}
else{
matrix.print((h > 12) ? h - 12 : ((h == 0) ? 12 : h), DEC); // Conversion to AM/PM
}
matrix.print(':');
if(now.minute() < 10){ // Zero padding if value less than 10 ie."09" instead of "9"
matrix.print("0");
matrix.print(now.minute(), DEC);
}
else{
matrix.print(now.minute(), DEC);
}
matrix.print(':');
if(now.second() < 10){ // Zero padding if value less than 10 ie."09" instead of "9"
matrix.print("0");
matrix.print(now.second(), DEC);
}
else{
matrix.print(now.second(), DEC);
}
// matrix.setCursor(13, 0); // start at top left, with one pixel of spacing
//matrix.print(':');
/*
//Experimental Padding for minutes -- WORKS!!!
if(now.minute() < 10){ // Zero padding if value less than 10 ie."09" instead of "9"
matrix.setCursor(18, 0);
matrix.print("0");
matrix.setCursor(24, 0);
matrix.print(now.minute(), DEC);
}
else{
matrix.setCursor(18, 0);
matrix.print(now.minute(), DEC);
}
matrix.setCursor(29, 0); // start at top left, with one pixel of spacing
matrix.print(':');
//Experimental Padding for seconds - Works!
if(now.second() < 10){ // Zero padding if value less than 10 ie."09" instead of "9"
matrix.setCursor(34, 0);
matrix.print("0");
matrix.setCursor(40, 0);
matrix.print(now.second(), DEC);
}
else{
matrix.setCursor(34, 0);
matrix.print(now.second(), DEC);
}
*/
///Day of week function, again from Adafruit forums!! You gotta' dig sometimes, there are some real treasures!
//Second, more correct way to do it compared to the switch/case technique, though that was educational as well!
char* dayStr[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
//Borrowed this following basic programming from Nick Hall, can I make it work? Yup, it works!
char suffix[4][3]={
"st", "nd", "rd", "th"}; //date suffix array, used in slide, normal and jumble modes. e,g, 1st 2nd ...
// Changed some variable names, like RTC etc.
//print the day suffix "nd" "rd" "th" etc. First work out date 2 letter suffix - eg st, nd, rd, th
byte s = 3; //the pos to read our suffix array from.
byte date = now.day();
if(date == 1 || date == 21 || date == 31) {
s = 0;
}
else if (date == 2 || date == 22) {
s = 1;
}
else if (date == 3 || date == 23) {
s = 2;
}
matrix.setCursor(0, 8);
matrix.print(dayStr[now.dayOfWeek()]);
matrix.setCursor(24, 8);
matrix.print(now.day(), DEC);
matrix.setCursor(37, 8);
matrix.print(suffix[s]);
matrix.writeScreen();
delay(1000);
}