SHARE
TWEET

Arduino Binary Clock

a guest Nov 19th, 2011 358 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. An open-source binary clock for Arduino.
  4.  
  5. Based on code by Rob Faludi (http://www.faludi.com) and
  6. Daniel Spillere Andrade (www.danielandrade.net).
  7. Code under CC, see http://creativecommons.org/license/cc-gpl
  8.  
  9. Depends on the time library from http://www.arduino.cc/playground/Code/Time
  10.  
  11. */
  12.  
  13. #include <Time.h>  
  14.  
  15. void setup() {
  16.                
  17.         // Initialize the LED pins 1-13 (connect LEDs as described at http://www.danielandrade.net/2008/07/15/binary-clock-with-arduino/)
  18.         for (int i=1; i<=13; i++) {
  19.            pinMode(i, OUTPUT);
  20.         }  
  21.    
  22.         setClock(16,31);// Set clock to current time (hh,mm). Time has to be set manually in the code before uploading!
  23. }
  24.  
  25. void loop() {
  26.  
  27.           setLEDs();
  28.           delay(1000);
  29. }
  30.  
  31. void setLEDs() {  
  32.              
  33.         int munit = minute()%10; //sets the variable munit and hunit for the unit digits
  34.         int hunit = hour()%10;
  35.        
  36.         //minutes units
  37.         if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {digitalWrite(1, HIGH);} else {digitalWrite(1,LOW);}
  38.         if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {digitalWrite(2, HIGH);} else {digitalWrite(2,LOW);}
  39.         if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {digitalWrite(3, HIGH);} else {digitalWrite(3,LOW);}
  40.         if(munit == 8 || munit == 9) {digitalWrite(4, HIGH);} else {digitalWrite(4,LOW);}
  41.  
  42.         //minutes
  43.         if((minute() >= 10 && minute() < 20) || (minute() >= 30 && minute() < 40) || (minute() >= 50 && minute() < 60))  {digitalWrite(5, HIGH);} else {digitalWrite(5,LOW);}
  44.         if(minute() >= 20 && minute() < 40)  {digitalWrite(6, HIGH);} else {digitalWrite(6,LOW);}
  45.         if(minute() >= 40 && minute() < 60) {digitalWrite(7, HIGH);} else {digitalWrite(7,LOW);}
  46.  
  47.         //hour units
  48.         if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {digitalWrite(8, HIGH);} else {digitalWrite(8,LOW);}
  49.         if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {digitalWrite(9, HIGH);} else {digitalWrite(9,LOW);}
  50.         if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {digitalWrite(10, HIGH);} else {digitalWrite(10,LOW);}
  51.         if(hunit == 8 || hunit == 9) {digitalWrite(11, HIGH);} else {digitalWrite(11,LOW);}
  52.  
  53.         //hour
  54.         if(hour() >= 10 && hour() < 20)  {digitalWrite(12, HIGH);} else {digitalWrite(12,LOW);}
  55.         if(hour() >= 20 && hour() < 24)  {digitalWrite(13, HIGH);} else {digitalWrite(13,LOW);}
  56. }
  57.  
  58. void setClock(int hour, int minute) {
  59.  
  60.   setTime(hour,minute,00,00,00,2000);
  61.  
  62. }
  63.  
  64.  
RAW Paste Data
Top