/* code for binay clock. SSSSSS / MMMMMM / HHHHH -- Using 17 LEDs, Uno uses use "Analog in" pins as pins 14-18. binary numbers represented as hours: 1,2,4,8,16 mins: 1,2,4,8,16,32 sec: 1,2,4,8,16,32 */ const int HRsLEDs[] = {14,15,16,17,18}; // LEDs used to show hours, to fit on Uno pins, this is using analog pins. const int MINSsLEDs[] = { 2,3,4,5,6,7}; // LEDs used to show mins const int SECsLEDs[] = { 8,9,10,11,12,13}; // LEDs used to show secs (uses pin 13 which will flash the onboard LES as well). int DAYs; // total numbbe rof days int HRs; // total number of hours int MINs; // remaining number of mins int SECs; // remaining number if secs int HRbin; // binary value for hours int MINbin; // binary value for minutes int SECbin; // binary value for seconds int remain; // used for calculating days/hours/mins/secs byte ledData=0; // This is the actual number that's going to be displayed. Byte is used since only 8 LEDs are used. After 255 it overflows back to 0. // use long declarations for the timers. long timerRef; // Reference time (use this to set the time) long diffms; // Time difference in milliseconds long timerNow; // Time since last reset (in ms) // int ledNoHR = 5; // number of LEDs being used // int ledNoMIN = 6; // number of LEDs being used // int ledNoSEC = 6; // number of LEDs being used void setup() { // Here we initialize all the LED pins to output and start our 'clock' timer for(int i=0; i < 5; i++) { pinMode(HRsLEDs[i], OUTPUT); } for(int i=0; i < 6; i++) { pinMode(MINSsLEDs[i], OUTPUT); } for(int i=0; i < 6; i++) { pinMode(SECsLEDs[i], OUTPUT); } // Serial.begin(9600); // Serial Output uncomment if you want to send serial (debugging) timerRef = millis(); // default to set timer to 0 // one can set time here by setting the reference to any time you would like (keep it negative though) // timerRef = -1*(19*3600000+43*60000); // sets to 7:43PM } void loop() { timerNow = millis(); diffms = timerNow - timerRef; // get current time (ms) compare to referance time, convert to binary. // 1 day = 3600000 * 24 ms :: 1 HR = 3600000ms :: 1 min = 60000ms :: 1 sec = 1000ms // I'm also counting days, but they are not displayed so no matter at this time. DAYs=diffms/(3600000*24); HRs=(diffms-(DAYs*24*3600000))/3600000; MINs=(diffms-(DAYs*24*3600000)-(HRs*3600000))/60000; SECs=(diffms-(DAYs*24*3600000)-(HRs*3600000)-(MINs*60000))/1000; char Hbinary[6] = {0}; //will be the Hours char Mbinary[7] = {0}; //will be the minutes char Sbinary[7] = {0}; //will be the seconds HRbin = HRs + 32; //Adding 32 so that there will always be 6 digits in the string itoa(HRbin,Hbinary,2); //Convert to a string using base 2 and save it in the array named binary char* Hstring = Hbinary + 1; //get rid of the most significant digit as there is an extra added for(byte i = 0; i < 5; i++){ digitalWrite(i+14,Hstring[i] - '0'); //write to the pin (the - '0' converts the bit of the string to HIGH or LOW) } MINbin = MINs + 64; //Adding 64 so that there will always be 7 digits in the string itoa(MINbin,Mbinary,2); //Convert to a string using base 2 and save it in the array named binary char* Mstring = Mbinary + 1; //get rid of the most significant digit as there is an extra added for(byte i = 0; i < 6; i++){ digitalWrite(i+2,Mstring[i] - '0'); //write to the pin (the - '0' converts the bit of the string to HIGH or LOW) } SECbin = SECs + 64; //Adding 64 so that there will always be 7 digits in the string itoa(SECbin,Sbinary,2); //Convert to a string using base 2 and save it in the array named binary char* Sstring = Sbinary + 1; //get rid of the most significant digit as there is an extra added for(byte i = 0; i < 6; i++){ digitalWrite(i+8,Sstring[i] - '0'); //write to the pin (the - '0' converts the bit of the string to HIGH or LOW) } // small delay to avoid inconvience to a small number of electrons. delay(100); // also helps a lot on the simulation! }