Skip to content

Instantly share code, notes, and snippets.

@Xpktro
Created March 19, 2018 18:59
Show Gist options
  • Save Xpktro/dce2873fdacf752cd03e66794031f057 to your computer and use it in GitHub Desktop.
Save Xpktro/dce2873fdacf752cd03e66794031f057 to your computer and use it in GitHub Desktop.
Binary clock

A digital (pure) binary clock using Arduino.

A clock that uses the binary numeric system to display the hour. Reading it is simple once you get the trick, but don't worry, I'm explaining it right here:

The schematics have a horizontal layout for the leds but it can be rearranged as follows (being seconds the led group to the far right):

+----+----+----+----+----+----+----+
|    |    |    | 08 | 04 | 02 | 01 | Hours
|    | 32 | 16 | 08 | 04 | 02 | 01 | Minutes
| PM | 32 | 16 | 08 | 04 | 02 | 01 | Seconds
+----+----+----+----+----+----+----+
  ^-- AM/PM

As you see, each led corresponds to a power of two and each row stands for a time partition: hours, minutes and seconds, also there is a single led for AM/PM. To calculate the current time, just add the amounts corresponding to the lit leds for each row.

As an example, I'm showing the calculation for the given display of lit leds:

XX = ON  |  OO = OFF
+----+----+----+----+----+----+----+
|    |    |    | OO | XX | OO | XX |           00 + 04 + 00 + 01 = 05 Hours
|    | OO | XX | OO | OO | XX | XX | 00 + 16 + 00 + 00 + 02 + 01 = 19 Mins
| OO | XX | OO | XX | OO | OO | XX | 32 + 00 + 08 + 00 + 00 + 01 = 41 Secs
+----+----+----+----+----+----+----+
  ^-- AM                                     Result -> 05:19:41 AM

The buttons set the hour, being the left one the selector, and the right one the selection modifier.

uint8_t lSeconds[] = {A0, A1, A2, A3, A4, A5};
uint8_t lMinutes[] = {4, 5, 6, 7, 8, 9};
uint8_t lHours[] = {10, 11, 12, 13};
uint8_t iPM = 3;
uint8_t iSelectButton = 2;
uint8_t iChangeButton = 1;
uint8_t iButtonDelay = 350;
uint8_t iState = 0; // 0: Clock | 1: Update
uint8_t iSelectState = 0; // 0: Minutes | 1: Hours
uint8_t iSeconds = 0;
uint8_t iMinutes = 0;
uint8_t iHours = 12;
uint8_t bPM = 0;
uint8_t iTemp = 0;
uint8_t iBitTemp = 0;
uint16_t iClock = 0;
void printBinary(int number, uint8_t leds[], int size) {
iBitTemp = number;
iTemp = 0;
do {
digitalWrite(leds[iTemp], (iBitTemp & 1)? HIGH : LOW);
iBitTemp = iBitTemp >> 1;
} while(++iTemp < size);
}
void updatePM() {
digitalWrite(iPM, (bPM = !bPM)? HIGH : LOW);
}
void updateHours(uint8_t number) {
iHours += number;
if(iHours > 12) {
iHours -= 12;
updatePM();
}
printBinary(iHours, lHours, 4);
}
void updateMinutes(uint8_t number) {
iMinutes += number;
if(iMinutes >= 60) {
iMinutes -= 60;
updateHours(1);
}
printBinary(iMinutes, lMinutes, 6);
}
void updateSeconds(uint8_t number) {
iSeconds += number;
if(iSeconds >= 60) {
iSeconds -= 60;
updateMinutes(1);
}
printBinary(iSeconds, lSeconds, 6);
}
void printAll() {
printBinary(iSeconds, lSeconds, 6);
printBinary(iMinutes, lMinutes, 6);
printBinary(iHours, lHours, 4);
}
void setup() {
Serial.begin(9600);
while (!Serial) {;}
do {
pinMode(lSeconds[iTemp], OUTPUT);
pinMode(lMinutes[iTemp], OUTPUT);
if(iTemp < 4)
pinMode(lHours[iTemp], OUTPUT);
} while(++iTemp < 6);
pinMode(iPM, OUTPUT);
pinMode(iSelectButton, INPUT);
pinMode(iChangeButton, INPUT);
printAll();
updatePM();
}
void loop() {
if(iState >= 1) {
if(!digitalRead(iSelectButton)) {
iSelectState++;
if(iSelectState > 1) {
iSelectState = 0;
iState = !iState;
iClock = 0;
iSeconds = 0;
printAll();
}
delay(iButtonDelay + 300);
} else {
if(!digitalRead(iChangeButton)) {
if(iSelectState) {
updateHours(1);
} else {
updateMinutes(1);
}
iClock = 0;
delay(iButtonDelay + 200);
}
if(iClock >= 700) {
if(iSelectState) {
printBinary(0, lHours, 4);
} else {
printBinary(0, lMinutes, 6);
}
} else {
printAll();
}
}
} else {
if(!digitalRead(iSelectButton)) {
iState = !iState;
iSelectState = 0;
delay(iButtonDelay);
} else if(!iClock) {
updateSeconds(1);
}
}
delay(10);
if((iClock += 10) >= 1000)
iClock = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment