Adafruit 16x24 Red LED Panel Clock Time Formatting Problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Cleveguy
 
Posts: 10
Joined: Sat Apr 12, 2014 1:04 pm

Adafruit 16x24 Red LED Panel Clock Time Formatting Problem

Post by Cleveguy »

Greetings,
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);     
    }



User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adafruit 16x24 Red LED Panel Clock Time Formatting Probl

Post by adafruit_support_mike »

You're getting caught by a quirk of modular math: any number modulus itself equals zero.

In these lines:

Code: Select all

    const uint8_t h = now.hour();
    const uint8_t hr_12 = h%12;
When the hour is 12, your second line evaluates to:

Code: Select all

    const uint8_t hr_12 = 12 % 12;
so hr_12 will be 0.

Zero is less than 10, so the test that decides whether to pad the value will decide "yes" for 12 % 12.

BTW - you don't want to declare those two values as 'const'. That means "I'm going to set this value once and never change it again".

There are all sorts of ways you can deal with the problem of 12 wrapping around to 0, but the easiest is to add a test right after you set the value:

Code: Select all

    uint8_t hr_12 = h % 12;
    
    if ( 0 == hr_12 ) {
        hr_12 = 12;
    }

User avatar
Cleveguy
 
Posts: 10
Joined: Sat Apr 12, 2014 1:04 pm

Re: Adafruit 16x24 Red LED Panel Clock Time Formatting Probl

Post by Cleveguy »

Thank you so much!

It looks like that worked. I wish I understood in more details what all is going on with all of those code bits.
The whole program is a bit of a Frankenstein, bits borrowed from here and there, stitched together with dodgy thread!

While searching for the answer, I also came upon the Time library at the Arduino.cc site which has these functions:

-----------------------------------------------------------------------------

"there are also functions to return the hour in 12 hour format

hourFormat12(); // The hour now in 12 hour format

isAM(); // Returns true if time now is AM

isPM(); // Returns true if time now is PM

now(); // Returns the current time as seconds
// since Jan 1 1970"

----------------------------------------------------------------------------

I already started to play with that a bit, but only after I implemented your recommendation.
A wonderful learning experience!

I have attached the current final version of what is running, cleaned up.
Perhaps it may be useful to someone.
Also attached a small pic of the panels running.

Thanks so much for your support.

Kirby Heintzelman
12:00 with no padding!
12:00 with no padding!
16x24_Clock.JPG (324.76 KiB) Viewed 488 times
Attachments
LED16x24_Clockv12.ino.zip
(2.42 KiB) Downloaded 31 times

User avatar
Franklin97355
 
Posts: 23903
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit 16x24 Red LED Panel Clock Time Formatting Probl

Post by Franklin97355 »

Nice work! Don't worry about feeling afloat in a sea of unknown, the more you build the easier it will get and soon you will be helping others and impressing your friends.

User avatar
Cleveguy
 
Posts: 10
Joined: Sat Apr 12, 2014 1:04 pm

Re: Adafruit 16x24 Red LED Panel Clock Time Formatting Probl

Post by Cleveguy »

Well,

Seems there is a fly in the ointment.

Not sure what is happening, but it seems that I am getting some random error in the time display.

Usually a loss of a minute, or several minutes.

Especially if I disconnect the power, and move the kit, and reconnect power. Just tried it again, and I lost 8 mins.

If I upload the code again, it seems to fix it.

It is mostly on the minutes, but I have also seen the hours lose time as well.

I noticed this also last night, when I moved the clock to observer and test the padding issue I was having.

Not sure if it is programming, or a hardware problem. Maybe the clock chip lost it's cookies from all of my experimentation.

Darn it!

It is always something!

Is it possible that this is being caused by not having pull-up resistors on SCA and SCL?
The project that I built my shield for may be turning on internal pull-ups on pins 4 & 5?

It is a little unfair to ask, since i am not using the Adafruit 1307 Breakout, which I see has resistors, and a cap between VCC & Gnd.

If anyone has a suggestion, I am very happy to listen.

Thanks, very grateful….

User avatar
Franklin97355
 
Posts: 23903
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit 16x24 Red LED Panel Clock Time Formatting Probl

Post by Franklin97355 »

Code: Select all

  rtc.adjust(DateTime(__DATE__, __TIME__)); 
This code will set the RTC time to the time the program was compiled and should be run once to set the RTC and never run again. You have it running every time you reset or restart the program.

User avatar
Cleveguy
 
Posts: 10
Joined: Sat Apr 12, 2014 1:04 pm

Re: Adafruit 16x24 Red LED Panel Clock Time Formatting Probl

Post by Cleveguy »

Oh dear, surely I knew that once!

<redfaced>

Must be a senior moment.

Seems to be better now that I corrected that.

What do you think about the lack of pull-up resistors?

Thanks Franklin!
Attachments
LED16x24_Clockv14.ino.zip
Updated per Franklin's support. LED 13 now blinks when RTC is active.
(2.56 KiB) Downloaded 38 times

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”