Skip to content

Instantly share code, notes, and snippets.

@aboutwout
Last active December 29, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aboutwout/19d1f91e88ace2eae6b0 to your computer and use it in GitHub Desktop.
Save aboutwout/19d1f91e88ace2eae6b0 to your computer and use it in GitHub Desktop.
My Arduino based led clock
// Clock.cpp
#include <Clock.h>
Clock::Clock (byte pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
Digit _digits[NUM_DIGITS] = { Digit() };
bool Clock::initialize (byte digits_b[NUM_DIGITS][NUM_PARTS][NUM_LEDS])
{
for (byte i = 0; i < NUM_DIGITS; i++)
{
byte tmp[NUM_PARTS][NUM_LEDS];
for (byte a = 0; a < NUM_PARTS; a++)
{
for (byte b = 0; b < NUM_LEDS; b++)
{
tmp[a][b] = digits_b[i][a][b];
}
}
_digits[i].setPin(_pin);
_digits[i].setLeds(tmp);
}
return true;
}
void Clock::setTime (byte hrs, byte mins)
{
setHrs(hrs);
setMins(mins);
}
void Clock::setHrs (byte hrs)
{
_hrs = hrs;
byte num1 = 0;
byte num2 = 0;
if (hrs >= 0)
{
num2 = hrs % 10;
num1 = (hrs - num2) / 10;
}
_digits[0].set(num1);
_digits[1].set(num2);
}
void Clock::setMins (byte mins)
{
_mins = mins;
byte num3 = 0;
byte num4 = 0;
if (mins >= 0)
{
num4 = mins % 10;
num3 = (mins - num4) / 10;
}
_digits[2].set(num3);
_digits[3].set(num4);
}
// Clock.h
#include <Arduino.h>
#include <Digit.h>
#ifndef Clock_h
#define Clock_h
#define NUM_DIGITS 4
#define NUM_PARTS 7
#define NUM_LEDS 2
class Clock
{
public:
Clock(byte pin);
bool initialize(byte digits_b[NUM_DIGITS][NUM_PARTS][NUM_LEDS]);
void setTime(byte hrs, byte mins);
void setHrs(byte hrs);
void setMins(byte mins);
private:
byte _hrs;
byte _mins;
byte _pin;
Digit _digits[NUM_DIGITS];
};
#endif
// Digit.cpp
#include <Digit.h>
const byte bMap[NUM_NUMBERS][NUM_PARTS] = {
{0, 1, 2, 4, 5, 6}, // 0
{2, 5}, // 1
{0, 2, 3, 4, 6}, // 2
{0, 2, 3, 5, 6}, // 3
{1, 2, 3, 5}, // 4
{0, 1, 3, 5, 6}, // 5
{0, 1, 3, 4, 5, 6}, // 6
{0, 2, 5}, // 7
{0, 1, 2, 3, 4, 5, 6}, // 8
{0, 1, 2, 3, 5, 6} // 9
};
void Digit::setPin(byte pin)
{
// Serial.println("Digit: Setting pin");
_pin = pin;
}
void Digit::setLeds(byte leds_c[NUM_PARTS][NUM_LEDS])
{
// Serial.println("Digit: Setting leds");
byte a = 0;
for (byte i = 0;i < NUM_PARTS; i++)
{
for (byte j = 0;j < NUM_LEDS; j++)
{
_leds[i][j] = leds_c[i][j];
_all[a++] = leds_c[i][j];
}
}
}
void Digit::set (byte num)
{
num = 2;
if (_num != num)
{
// Serial.println("Digit: Set number");
Serial.println("---");
_num = num;
// _all_parts_off();
Serial.print("Size of part array: ");
Serial.println(sizeof(bMap[num]));
Serial.print("Values [");
Serial.print(num);
Serial.print("]: ");
for (byte a = 0; a < sizeof(bMap[num]); a++)
{
Serial.print(bMap[num][a]);
Serial.print("(");
Serial.print(a);
Serial.print(")");
Serial.print(", ");
// _part_on(bMap[num][a]);
}
Serial.println(';');
}
}
void Digit::_part_on (byte part)
{
// Serial.print(part);
// Serial.print(", ");
// set leds in this part to on
// find leds in this part and turn them on
}
void Digit::_part_off (byte part)
{
// set leds in this part to off
// find leds in this part and turn them off
}
void Digit::_all_parts_off ()
{
// set all leds to on
}
void Digit::clear ()
{
_all_parts_off();
}
// Digit.h
#include <Arduino.h>
// #include <Adafruit_NeoPixel.h>
#ifndef Digit_h
#define Digit_h
#define NUM_NUMBERS 10
#define NUM_PARTS 7
#define NUM_LEDS 2
class Digit
{
public:
void set(byte num);
void clear();
void setPin(byte pin);
void setLeds(byte leds_c[NUM_PARTS][NUM_LEDS]);
const byte bMap[NUM_NUMBERS][NUM_PARTS];
private:
byte _num;
byte _pin;
byte _leds[NUM_PARTS][NUM_LEDS];
byte _all[NUM_PARTS * NUM_LEDS];
void _part_on (byte part);
void _part_off (byte part);
void _all_parts_off ();
};
#endif
// myclock.ino
#include <Time.h>
#include <Clock.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
#define DATA_PIN 13
#define NUM_DIGITS 4
#define NUM_PARTS 7
#define NUM_LEDS 2
Clock myClock(DATA_PIN);
void setup () {
// Serial.begin(9600);
// Serial.println("Digit: Setting pin");
setSyncProvider(requestSync); //set function to call when sync required
byte led_map[NUM_DIGITS][NUM_PARTS][NUM_LEDS] = {
{
{2, 3},
{0, 1},
{4, 5},
{6, 7},
{8, 9},
{12, 13},
{10, 11}
},
{
{16, 17},
{14, 15},
{18, 19},
{20, 21},
{22, 23},
{26, 27},
{24, 25}
},
{
{30, 31},
{28, 29},
{32, 33},
{34, 35},
{36, 37},
{40, 41},
{38, 39}
},
{
{44, 45},
{42, 43},
{46, 47},
{48, 49},
{50, 51},
{54, 55},
{52, 53}
}
};
if (myClock.initialize(led_map))
{
myClock.setTime(0, 0);
}
}
void loop ()
{
if (Serial.available())
{
processSyncMessage();
}
myClock.setTime(hour(), minute());
if (timeStatus() != timeNotSet)
{
// digitalWrite(13, timeStatus() == timeSet); // on if synced, off if needs refresh
//myClock.setTime(hour(), minute());
}
delay(1000);
}
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 (byte 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);
//Serial.print(TIME_REQUEST, BYTE);
return 0; // the time will be sent later in response to serial mesg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment