Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Created January 28, 2014 17:51
Show Gist options
  • Select an option

  • Save cameronp98/8672611 to your computer and use it in GitHub Desktop.

Select an option

Save cameronp98/8672611 to your computer and use it in GitHub Desktop.
Console based binary clock in C. The show() function would obviously do something like turn LEDs on and off if IO pins were available.
#include <stdio.h>
#include <time.h>
#define SECOND 1
#define MINUTE 60
#define HOUR 3600
int diff = 0;
void show(int unit, char end);
main() {
time_t start = time(0), now = time(0);
while (1) {
diff = (now - start);
show(SECOND, ' ');
show(MINUTE, ' ');
show(HOUR, '\n');
now = time(0);
}
}
void show(int unit, char end) {
int i, secs = (diff/unit) % 60;
for (i = 6; i >= 0; --i) {
if (secs & (1 << i))
putchar('+');
else
putchar('-');
}
putchar(end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment