First off I want to thank everyone who put in their hard work to create this library for the ht1632c. Incredible work.
This was my first Arduino project, and other than some minor C programming classes back in middle school (some 15 years ago) a wake-up call to programming.
By picking apart the example code included with the library, and everyone's comments in this forum I was able to get my displays up and running. Did have a few set backs with using outdated info on what pins to connect to on my Arduino Uno, and code not being updated for Arduino 1.0. Other than that it's been a great learning experience.
I was wondering if any of you would mind looking at my code and give me some tips on where I may have gone right/wrong.
Key points:
- Video of working display
http://bit.ly/HPPW3J- I don't yet have a chip to keep time (fingers crossed on my sample request). Because of this, time gets off pretty quickly. I think I saw something about a windows program that can auto-send the time to my arduino. Maybe that will be a temporary option.
- When I remove the usb my display garbles. Video of error
http://bit.ly/HF9DxV- I hope to incorporate Evanrich's bluetooth/android idea in the future.
- I'm curious on how everyone went about creating some sort of shell to mount these in.
- Would this be a good guide to replace my Arduino once I'm done testing?
http://bit.ly/IeBZZm- PS. Was playing around with running my two displays from my
http://isound.net/ 16,000mah battery/charger. Might run some tests to see how many hours I can get out of running the displays. Might be a good option for creating a mobile platform.
Code:
/*
* Mehr Matrix
* This code was created by Marco Melloni using/modifying example code from the following:
* ht1632 Library examples http://bit.ly/HCl4W8
* ht1632 arduino community http://bit.ly/HClcVU
* Countdown timer example by "siliconfish" http://bit.ly/HCkSWW
*/
#include <Time.h>
#include <ht1632c.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
int daysleft = 0;
int hoursleft = 0;
int minsleft = 0;
int secsleft = 0;
// http://www.epochconverter.com for timeStamp
// DST 2am second Sunday in March -> 2am first Sunday in November
// int timezoneOffset = -28800; //Pacific Standard Time
int timezoneOffset = -25200; //Pacific Daylight Saving Time
signed long eventTimestamp01 = 1342051200 - timezoneOffset; //Comic-Con 2012
//PORT, data, wr, clk, cs
ht1632c dotmatrix = ht1632c(&PORTD, 7, 6, 4, 5, GEOM_32x16, 2);
void setup() {
Serial.begin(9600);
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
Serial.println("Example T1297674000");
dotmatrix.clear();
dotmatrix.pwm(15);
}
void loop(){
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus()!= timeNotSet)
{
digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
displayMode();
}
else {
matrixError();
}
delay(1000);
}
void matrixError()
{
dotmatrix.setfont(FONT_5x7W);
char tmp1[20] = " ERROR";
byte len1 = strlen(tmp1);
for (int i = 0; i < len1; i++)
dotmatrix.putchar(6*i, 0, tmp1[i], ORANGE);
dotmatrix.sendframe();
dotmatrix.setfont(FONT_5x7W);
char tmp2[20] = " SET TIME";
byte len2 = strlen(tmp2);
for (int i = 0; i < len2; i++)
dotmatrix.putchar(6*i, 8, tmp2[i], GREEN);
dotmatrix.sendframe();
delay(2000);
dotmatrix.clear();
}
void displayMode()
{
// Control Current Mode
matrixCountdown01();
}
void matrixCountdown01()
{
// Display Event Name
dotmatrix.setfont(FONT_5x7W);
char tmp[20] = " COMIC-CON";
byte len = strlen(tmp);
for (int i = 0; i < len; i++)
dotmatrix.putchar(6*i, 0, tmp[i], RED);
dotmatrix.sendframe();
// Display Countdown
dotmatrix.setfont(FONT_5x7);
if (eventTimestamp01 < now()) {
daysleft = 0;
hoursleft = 0;
minsleft = 0;
secsleft = 0;
char tmp[20];
sprintf(tmp, "%02d:%02d:%02d:%02d", daysleft, hoursleft, minsleft, secsleft);
byte len = strlen(tmp);
for (int i = 0; i < len; i++)
dotmatrix.putchar(6*i, 9, tmp[i], GREEN);
dotmatrix.sendframe();
}
else {
daysleft = (eventTimestamp01 - now())/60/60/24;
hoursleft = (eventTimestamp01 - now())/60/60 - daysleft*24;
minsleft = (eventTimestamp01 - now())/60 - hoursleft*60 - daysleft*24*60;
secsleft = 60 - second();
char tmp[20];
sprintf(tmp, "%02d:%02d:%02d:%02d", daysleft, hoursleft, minsleft, secsleft);
byte len = strlen(tmp);
for (int i = 0; i < len; i++)
dotmatrix.putchar(6*i, 9, tmp[i], GREEN);
dotmatrix.sendframe();
}
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
//Serial.print(TIME_REQUEST,BYTE); Replaced in Arduino 1.0
Serial.write( byte(TIME_REQUEST));
return 0; // the time will be sent later in response to serial mesg
}