Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Binary Clock(update minute and hour variable with current time before writing to device).
/*
Layout of pins:
3 10
4 7 11
1 5 8 12
2 6 9 13
*/
#include <EEPROM.h>
int startupDone = 0;
int i = 0;
int sinceStart = 0;
int second = 0;
int minute = 0;
int hour = 0;
int munit = 0;
int hunit = 0;
void setup() {
for (i = 0; i < 13; i++) {
pinMode(i+1, OUTPUT);
}
}
void loop() {
static unsigned long lastTick = 0;
// Run the start up sequence.
if (!startupDone) {
startupDone = 1;
// Read the saved time.
second = EEPROM.read(1);
if (second == 255) {
second = 0;
}
minute = EEPROM.read(2);
if (minute == 255) {
minute = 0;
}
hour = EEPROM.read(3);
if (hour == 255) {
hour = 0;
}
for (i = 0; i < 13; i++) {
digitalWrite(i+1, HIGH);
delay(80);
digitalWrite(i+1, LOW);
}
for (i = 13; i >= 0; i--) {
digitalWrite(i-1, HIGH);
delay(80);
digitalWrite(i-1, LOW);
}
}
// Update second every 1000 milliseconds.
sinceStart = millis();
if (sinceStart - lastTick >= 1000) {
lastTick = sinceStart;
second++;
} else {
// Return here instead of continuing, this way we save EEPROM writes.
return;
}
// Update minute every 60 seconds.
if (second >= 60) {
minute++;
second = 0;
}
// Update hour every 60 minutes.
if (minute >= 60) {
hour++;
minute = 0;
}
// Only 24 hours a day.
if (hour >= 24) {
hour = 0;
minute = 0;
}
// Write times to EEPROM.
EEPROM.write(1, second);
EEPROM.write(2, minute);
EEPROM.write(3, hour);
munit = minute%10;
hunit = hour%10;
// Minute units.
if (munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
if (munit == 2 || munit == 3 || munit == 6 || munit == 7) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
if (munit == 4 || munit == 5 || munit == 6 || munit == 7) {
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
}
if (munit == 8 || munit == 9) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
// Minutes.
if ((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60)) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);
}
if (minute >= 20 && minute < 40) {
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}
if (minute >= 40 && minute < 60) {
digitalWrite(7, HIGH);
} else {
digitalWrite(7, LOW);
}
// Hour units.
if (hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {
digitalWrite(6, HIGH);
} else {
digitalWrite(6, LOW);
}
if (hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
if (hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
if (hunit == 8 || hunit == 9) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
// Hour.
if (hour >= 10 && hour < 20) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
if (hour >= 20 && hour < 24) {
digitalWrite(1, HIGH);
} else {
digitalWrite(1, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment