I picked up the epaper display and breakout board that SparkFun sells. I’m trying to make a clock out of it. It’s been pretty slow-going trying to figure out how to interface with the display, but I’m making progress.
I’m pulling the time from a Chronodot DS3231 RTC breakout board. That’s easy enough. Since the epaper display library seems to want to have a char sent to it (instead of int or string), I’m passing the hours and minutes from the RTC library into strings along with my am/pm designator. I convert the 24 hour time to 12 hours and pass that into a string too. I take the hrs, minutes, and am/pm and pass those into a string and convert the whole thing into a char array for the epaper display.
The problem is, I seem to be ending up with a 0 at the end of my display. I can’t figure it out. My char array should be 10 chars. I add a leading space (or two, as required) to center the text on the display. I have the char array configured to a length of 10. If I make it 9, it just displays a bunch of zeros. If I make it 11 or greater, it seems to have no effect. I just can’t seem to figure out where the zero is coming from.
Is there an error in my code or methodology that’s causing this?
Another problem is that at some point a wrote a “K” to the last char of the top line of the display and part of the “K” stuck there. I haven’t been able to get rid of it. So I wonder if I have a bad display. I don’t think that’s the case though, because the 0 doesn’t appear there when I display my startup splash screen. It’s only there during my loop.
Here’s my code…
#include <Wire.h>
#include "ePaper.h" // This file includes defines for each displayable character
int EIOpin = 8; // Input/output pin for chip selection
int XCKpin = 9; // Clock input pin for taking display data
int LATCHpin = 10; // Latch pulse input pin for display data
int SLEEPBpin = 11; // Sleep Pin for the display
int DI0pin = 12; // Input pin for display data
//setup display with pin definitions
ePaper epaper = ePaper(EIOpin, XCKpin, LATCHpin, SLEEPBpin, DI0pin);
int seconds;
int minutes;
int hours;
int adjHours;
int dayOfWeek;
int dayOfMonth;
int month;
int year;
String hrStr;
String minStr;
String timeStr;
String space;
String ampm;
void setup()
{
Wire.begin();
Serial.begin(9600);
//epaper.writeTop("EPaper");
epaper.writeTop(" EPAPER ");
epaper.writeBottom(" Clock V1 ");
epaper.writeDisplay();
delay(3000);
epaper.deleteDisplay();
}
void loop()
{
//poll the DS3231 for the date and time
get_time();
get_date();
//display hours
switch(hours) {
case 0:
adjHours = 12;
break;
case 1:
adjHours = 1;
break;
case 2:
adjHours = 2;
break;
case 3:
adjHours = 3;
break;
case 4:
adjHours = 4;
break;
case 5:
adjHours = 5;
break;
case 6:
adjHours = 6;
break;
case 7:
adjHours = 7;
break;
case 8:
adjHours = 8;
break;
case 9:
adjHours = 9;
break;
case 10:
adjHours = 10;
break;
case 11:
adjHours = 11;
break;
case 12:
adjHours = 12;
break;
case 13:
adjHours = 1;
break;
case 14:
adjHours = 2;
break;
case 15:
adjHours = 3;
break;
case 16:
adjHours = 4;
break;
case 17:
adjHours = 5;
break;
case 18:
adjHours = 6;
break;
case 19:
adjHours = 7;
break;
case 20:
adjHours = 8;
break;
case 21:
adjHours = 9;
break;
case 22:
adjHours = 10;
break;
case 23:
adjHours = 11;
break;
}
//pass adjusted hours int inro a string
hrStr = String(adjHours);
minStr = String(minutes);
//figure out AM/PM
if (hours < 12) {
ampm = " AM ";
}
else if (hours = 0) {
ampm = " AM ";
}
else {
ampm = " PM ";
}
//figure out leading spaces
if (adjHours > 9) {
space = " "; //1 space
}
else {
space = " "; //2 spaces
}
//construct the string to be displayed
String timeStr = (space + hrStr + minStr + ampm);
//convert the string to be displayed into a char array, limit to 10 places
char timeChr[10];
timeStr.toCharArray(timeChr, 10);
//clear the display and write the char array
epaper.deleteDisplay();
epaper.writeTop(timeChr);
epaper.writeDisplay();
//wait almost a minute to update time
delay(50000);
} //end of loop
/////////////
//DS3231 RTC interface
void initChrono()
{
set_time();
set_date();
}
void set_date()
{
Wire.beginTransmission(104);
Wire.write(3);
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void get_date()
{
Wire.beginTransmission(104);
Wire.write(3);//set register to 3 (day)
Wire.endTransmission();
Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void set_time()
{
Wire.beginTransmission(104);
Wire.write(0);
Wire.write(decToBcd(seconds));
Wire.write(decToBcd(minutes));
Wire.write(decToBcd(hours));
Wire.endTransmission();
}
void get_time()
{
Wire.beginTransmission(104);
Wire.write(0);//set register to 0
Wire.endTransmission();
Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
seconds = bcdToDec(Wire.read() & 0x7f);
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0x3f);
}
///////////////////////////////////////////////////////////////////////
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}