Wednesday, 18 December 2013

A binary clock

I know clocks are cheap, you can get a watch these days for less than £1! But how about a binary clock?

This one tells the time in four columns, from right to left:

- minutes (yellow LEDs) 1 2 4 8

- minutes (green LEDs) 10 20 40

- hours (red LEDs) 1 2 4 8

- hours (blue LEDs) 10 20

Just add up the binary digits to get the time. the display is like this:

2013 12 18 13 35 27

The time shown above is

10 +3 hours, 30 + 3 minutes or 13:33

The program for this is:

// binary clock

#define LED1M 2
#define LED2M 3
#define LED4M 4
#define LED8M 5

#define LED10M 6
#define LED20M 7
#define LED40M 8

#define LED1H 9
#define LED2H 10
#define LED4H 11
#define LED8H 12

#define LED10H A0
#define LED20H A1

#define MBUTTON A4
#define HBUTTON A5

static unsigned long tick;
int sec = 0;
int minu = 0;
int hr = 0;
int munit;
int hunit;
   
void setup()
{  
  pinMode(LED1M, OUTPUT);
  pinMode(LED2M, OUTPUT);
  pinMode(LED4M, OUTPUT);
  pinMode(LED8M, OUTPUT);
  
  pinMode(LED10M, OUTPUT);
  pinMode(LED20M, OUTPUT);
  pinMode(LED40M, OUTPUT);
                
  pinMode(LED1H, OUTPUT);
  pinMode(LED2H, OUTPUT);
  pinMode(LED4H, OUTPUT);
  pinMode(LED8H, OUTPUT);
  
  pinMode(LED10H, OUTPUT);
  pinMode(LED20H, OUTPUT);
  
  pinMode(MBUTTON, INPUT_PULLUP); // set minutes button
  pinMode(HBUTTON, INPUT_PULLUP); // set hours button
  
  tick = millis(); // start clock
  
}

void loop()
{
   
   if(millis() - tick >= 1000) // basic timing
   {
     tick = millis();
     sec++; // count seconds

   }
   
   if(sec >= 60)
   {
     minu++; // count minus
     sec = 0;

   }
   
   if(minu >= 60)
   {
     hr++; // count hrs
     minu = 0;
   }
   
   if(hr >= 24)
   {
     hr = 0; // reset hrs
     minu = 0; // and min
   }
   
   munit = minu%10; // get min units
   hunit = hr%10; // get hr units

   // convert 1st column to binary
   if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) 
   {  
     digitalWrite(LED1M, HIGH);
   }
   else 
   {
     digitalWrite(LED1M,LOW);
   }
   if(munit == 2 || munit == 3 || munit == 6 || munit == 7) 
   {
     digitalWrite(LED2M, HIGH);
   }
   else 
   {
     digitalWrite(LED2M,LOW);
   }
   if(munit == 4 || munit == 5 || munit == 6 || munit == 7) 
   {
     digitalWrite(LED4M, HIGH);
   }
   else 
   {
     digitalWrite(LED4M,LOW);
   }
   if(munit == 8 || munit == 9)
   {
     digitalWrite(LED8M, HIGH);
   }
   else 
   {
     digitalWrite(LED8M,LOW);
   }
   
   // convert 2nd column to binary
   if((minu >= 10 && minu < 20) || (minu >= 30 && minu < 40) || (minu >= 50 && minu < 60))  
   {
     digitalWrite(LED10M, HIGH);
   } 
   else 
   {
     digitalWrite(LED10M,LOW);
   }
   if(minu >= 20 && minu < 40)  
   {
     digitalWrite(LED20M, HIGH);
   }
   else 
   {
     digitalWrite(LED20M,LOW);
   }
   if(minu >= 40 && minu < 60) 
   {
     digitalWrite(LED40M, HIGH);
   }
   else 
   {
     digitalWrite(LED40M,LOW);
   }

   // convert 3rd column to binary
   if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) 
   {
     digitalWrite(LED1H, HIGH);} 
   else 
   {
     digitalWrite(LED1H,LOW);
   }
   if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) 
   { 
     digitalWrite(LED2H, HIGH);
   } 
   else 
   {
     digitalWrite(LED2H,LOW);
   }
   if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) 
   {digitalWrite(LED4H, HIGH);
   } 
   else
   {
     digitalWrite(LED4H,LOW);
   }
   if(hunit == 8 || hunit == 9) 
   {
     digitalWrite(LED8H, HIGH);
   }
   else 
   {
     digitalWrite(LED8H,LOW);
   }

   // convert 4th column to binary
   if(hr >= 10 && hr < 20) 
   {
     digitalWrite(LED10H, HIGH);
   } 
   else 
   {
     digitalWrite(LED10H, LOW);
   }
   if(hr >= 20 && hr < 24)  
   {
     digitalWrite(LED20H, HIGH);
   } 
   else
   {
     digitalWrite(LED20H,LOW);
   }
   
   if(digitalRead(MBUTTON) == LOW)
   {
     minu++;
     sec = 0;
     delay(250); // allow for contact bounce?
   }
   
   if(digitalRead(HBUTTON) == LOW)
   {
     hr++;
     sec = 0;
     delay(250);
   }
}
   

No comments: