Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Christmas countdown program for Particle Photon
#include <time.h>
#define TARGET_MONTH 12
#define TARGET_DAY 25
#define TARGET_YEAR 2016
#define CLEAR_COMMAND 0x76
#define CURSOR_COMMAND 0x79
struct tm target = {0};
// The following 3 functions have code for communicating
// with the display via Serial1 but also have code
// commented out to use SPI instead.
void configureCommChannel()
{
// The 7-segment display default baud rate is 9600
Serial1.begin(9600);
// SPI.setClockSpeed(250, KHZ);
// SPI.setDataMode(SPI_MODE0);
// SPI.begin(A2);
}
void writeToScreen(byte b)
{
Serial1.write(b);
// digitalWrite(A2, LOW);
// SPI.transfer(b);
// digitalWrite(A2, HIGH);
}
void writeToScreen(byte bytes[], int len)
{
Serial1.write(bytes, len);
// digitalWrite(A2, LOW);
// for (int i = 0; i < len; i++)
// {
// SPI.transfer(bytes[i]);
// }
// digitalWrite(A2, HIGH);
}
void parseTargetDate(int year, int month, int day)
{
target.tm_year = year - 1900;
target.tm_mon = month - 1;
target.tm_mday = day;
}
void clearDisplay()
{
writeToScreen(CLEAR_COMMAND);
}
void setCursorPosition(int position)
{
byte commands[] = {CURSOR_COMMAND, position};
writeToScreen(commands, 2);
}
// displayDaysUntilEvent is specific to the 7-segment
// display because of how the cursor position must be
// moved before writing each digit. If you choose a
// different display this code could probably be more
// straight forward.
void displayDaysUntilEvent(int days)
{
byte hundreds = 0;
byte tens = 0;
clearDisplay();
hundreds = days / 100;
if(hundreds > 0)
{
setCursorPosition(1);
writeToScreen(hundreds);
}
tens = (days % 100) / 10;
if(tens > 0 || hundreds > 0)
{
setCursorPosition(2);
writeToScreen(tens);
}
setCursorPosition(3);
writeToScreen(days % 10);
}
// The setup() function is called one time right after
// power up. We handle all initialization here.
void setup() {
Time.zone(-8); // Set the timezone to PST
configureCommChannel(); // Initialize communication
parseTargetDate(TARGET_YEAR, TARGET_MONTH, TARGET_DAY);
clearDisplay();
}
// The loop() function will be executed over and over after
// setup() has finished.
void loop() {
int diffInSeconds = 0;
// Calculate the difference between now and our target
diffInSeconds = difftime(mktime(&target), Time.local());
if(diffInSeconds > 0)
{
displayDaysUntilEvent(diffInSeconds / 86400 + 1);
}
// Check again in 10 seconds
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment