Created
January 28, 2014 17:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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