El Condor
Published © CC BY

Arduino Based Binary Clock

My very own binary clock based on Arduino and an 8x8 LED Matrix backpack.

BeginnerWork in progress30 minutes6,885
Arduino Based Binary Clock

Things used in this project

Story

Read more

Schematics

Arduino Breadboard for Binary Clock

Connections for the Arduino Binary Clock

Code

Arduino Sketch for Binary Clock

C/C++
Use this Sketch to upload to your Arduino. Connect an Adafruit LED matrix backpack to your Arduino and add two buttons (don't forget the resistors). Use the buttons to adjust the time.
/*************************************************** 
  Binary clock created by Solino C. de Baay
  Based on libraries Time (ArduinoClock Sketch by
  Techno (sǝɹoɟ ǝǝןuuɐ) 
  And AdaFruit 8x8 LED Backpack library.
  Implemented clock setting and two buttons.
 ****************************************************/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"

int sunit = 0;
int munit = 0;
int hunit = 0;
int hour=0; 
int minute = 0; 
int second = 0; 
int TIME; 
const byte P1=2;
const byte P2=3;
const int pixelsToDraw = 20;
int pixelX[pixelsToDraw];
int pixelY[pixelsToDraw];
uint16_t pixelCMD[pixelsToDraw];

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

void setup() {
  Serial.begin(9600);
  Serial.println("Arduino Clock");
  
  matrix.begin(0x70);  // pass in the address

  pinMode(P1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(P1), changehours, FALLING);
 
  pinMode(P2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(P2), changeminutes, FALLING);
}  

void loop() {
  // set up a local variable to hold the last time we moved forward one second
  // (static variables are initialized once and keep their values between function calls)
  // move forward one second every 1000 milliseconds
  static unsigned long lastTick = 0; 
  
  if (millis() - lastTick >= 1000) {
    lastTick = millis();
    second++; 
  }
  
  // move forward one minute every 60 seconds
  if (second >= 60) {
    minute++;
    second = 0; // reset seconds to zero
  }
  
  // move forward one hour every 60 minutes
  if (minute >=60) {
    hour++;
    minute = 0; // reset minutes to zero
  }
  
  if (hour >=24) {
    hour=0;
    minute = 0; // reset minutes to zero
  }
  
  //sets the variable sunit, munit and hunit for the unit digits using a       //modulus function
  sunit = second%10;
  munit = minute%10; 
  hunit = hour%10;
  //begin selecting pixels and draw them on the LED Matrix
  selectPixels(sunit,second,munit,minute,hunit,hour);
  matrix.clear();
  drawTime();
  matrix.writeDisplay(); 
  //For debuging:
  //Serial.println(displayTimeInString());
  //Serial.println(String(sunit));
}
  
void selectPixels(int sunit, int second, int munit, int minute, int hunit, int hour){  
  //seconds units
  if(sunit == 1 || sunit == 3 || sunit == 5 || sunit == 7 || sunit == 9) {
  addTimePixel(1,3,7,LED_ON);
  addTimePixel(2,3,5,LED_ON); } else { addTimePixel(1,3,7,LED_OFF);
  addTimePixel(2,3,5,LED_OFF);}//8LED (7,5)  
  if(sunit == 2 || sunit == 3 || sunit == 6 || sunit == 7) {
  addTimePixel(1,2,7,LED_ON);
  addTimePixel(2,2,4,LED_ON); } else { addTimePixel(1,2,7,LED_OFF);
  addTimePixel(2,2,4,LED_OFF);}//4LED (7,4)  
  if(sunit == 4 || sunit == 5 || sunit == 6 || sunit == 7) {
  addTimePixel(1,1,7,LED_ON);
  addTimePixel(2,1,3,LED_ON); } else { addTimePixel(1,1,7,LED_OFF);
  addTimePixel(2,1,3,LED_OFF);}//2LED (7,3)  
  if(sunit == 8 || sunit == 9) {
  addTimePixel(1,0,7,LED_ON);
  addTimePixel(2,0,2,LED_ON); } else { addTimePixel(1,0,7,LED_OFF);
  addTimePixel(2,0,2,LED_OFF); }//1LED (7,2)    

  //seconds 
  if((second >= 10 && second < 20) || (second >= 30 && second < 40) || (second >= 50 && second < 60))  {
  addTimePixel(1,6,6,LED_ON);
  addTimePixel(2,6,5,LED_ON); } else { addTimePixel(1,6,6,LED_OFF);
  addTimePixel(2,6,5,LED_OFF);}//4LED (6,5)
  if(second >= 20 && second < 40)  {
  addTimePixel(1,5,6,LED_ON);
  addTimePixel(2,5,4,LED_ON); } else { addTimePixel(1,5,6,LED_OFF);
  addTimePixel(2,5,4,LED_OFF);}//2LED (6,4)
  if(second >= 40 && second < 60) {
  addTimePixel(1,4,6,LED_ON);
  addTimePixel(2,4,3,LED_ON);  } else { addTimePixel(1,4,6,LED_OFF);
  addTimePixel(2,4,3,LED_OFF);}//1LED (6,3)

  //minutes units
  if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {
  addTimePixel(1,10,4,LED_ON);
  addTimePixel(2,10,5,LED_ON); } else { addTimePixel(1,10,4,LED_OFF);
  addTimePixel(2,10,5,LED_OFF);}//8LED (4,5)
  if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {
  addTimePixel(1,9,4,LED_ON);
  addTimePixel(2,9,4,LED_ON);  } else { addTimePixel(1,9,4,LED_OFF);
  addTimePixel(2,9,4,LED_OFF);}//4LED (4,4)
  if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {
  addTimePixel(1,8,4,LED_ON);
  addTimePixel(2,8,3,LED_ON);  } else { addTimePixel(1,8,4,LED_OFF);
  addTimePixel(2,8,3,LED_OFF);}//2LED (4,3)
  if(munit == 8 || munit == 9) {
  addTimePixel(1,7,4,LED_ON);
  addTimePixel(2,7,2,LED_ON);  } else { addTimePixel(1,7,4,LED_OFF);
  addTimePixel(2,7,2,LED_OFF);}//1LED (4,2)

  //minutes 
  if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60))  {
  addTimePixel(1,13,3,LED_ON);
  addTimePixel(2,13,5,LED_ON); } else { addTimePixel(1,13,3,LED_OFF);
  addTimePixel(2,13,5,LED_OFF);}//4LED (3,5)
  if(minute >= 20 && minute < 40)  {
  addTimePixel(1,12,3,LED_ON);
  addTimePixel(2,12,4,LED_ON); } else { addTimePixel(1,12,3,LED_OFF);
  addTimePixel(2,12,4,LED_OFF);}//2LED (3,4)
  if(minute >= 40 && minute < 60) {
  addTimePixel(1,11,3,LED_ON);
  addTimePixel(2,11,3,LED_ON); } else { addTimePixel(1,11,3,LED_OFF);
  addTimePixel(2,11,3,LED_OFF);}//1LED (3,3)

  //hour units
  if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {
  addTimePixel(1,17,1,LED_ON);
  addTimePixel(2,17,5,LED_ON); } else { addTimePixel(1,17,1,LED_OFF);
  addTimePixel(2,17,5,LED_OFF);}//8LED (1,5)
  if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {
  addTimePixel(1,16,1,LED_ON);
  addTimePixel(2,16,4,LED_ON); } else { addTimePixel(1,16,1,LED_OFF);
  addTimePixel(2,16,4,LED_OFF);}//4LED (1,4)
  if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {
  addTimePixel(1,15,1,LED_ON);
  addTimePixel(2,15,3,LED_ON); } else { addTimePixel(1,15,1,LED_OFF);
  addTimePixel(2,15,3,LED_OFF);}//2LED (1,3)
  if(hunit == 8 || hunit == 9) {
  addTimePixel(1,14,1,LED_ON);
  addTimePixel(2,14,2,LED_ON); } else { addTimePixel(1,14,1,LED_OFF);
  addTimePixel(2,14,2,LED_OFF);}//1LED (1,2)

  //hour
  if(hour >= 10 && hour < 20)  {
  addTimePixel(1,19,0,LED_ON);
  addTimePixel(2,19,5,LED_ON); } else { addTimePixel(1,19,0,LED_OFF);
  addTimePixel(2,19,5,LED_OFF);}//2LED (0,5)
  if(hour >= 20 && hour < 24)  {
  addTimePixel(1,18,0,LED_ON);
  addTimePixel(2,18,4,LED_ON); } else { addTimePixel(1,18,0,LED_OFF);
  addTimePixel(2,18,4,LED_OFF);}//1LED (0,4)
}

//Fill the matrix memory with the next set of coordinates for the LED's to be
//switched on
void drawTime(){
  for(int i=0; i<pixelsToDraw; i++){
    matrix.drawPixel(pixelX[i], pixelY[i], pixelCMD[i]);
    }
}

//fill the coordinate arrays pixelX and pixelY with the correct values and 
//set the command to either switch the LED on or off
void addTimePixel(int pixel, int pos, int val, uint16_t cmd){  
  pixelCMD[pos] = cmd;
  switch(pixel){
    case 1:
    pixelX[pos] = val;
    case 2:
    pixelY[pos] = val;
  }
  
}

//Debugging function, use this to write the current time to the Serial output
String displayTimeInString(){
  String TimeString = String(hour);
  TimeString += ":";
  TimeString += String(minute);
  TimeString += ":";
  TimeString += String(second);
  return TimeString;
  }

//interrupt function to change the hours
void changehours() {
  hour = hour + 1;
  if(hour==24)hour=0;  
  Serial.println("Hours adjusted");
  Serial.println(displayTimeInString());
}

//interrupt function to change the minutes
void changeminutes() {
  second = 0;
  minute = minute + 1;
  Serial.println("Minutes adjusted");
  Serial.println(displayTimeInString());  
  }

//LED matrix coordinate reference ;o)  
//    01234567
//  0 00000000
//  1 00000000
//  2 01001001
//  3 01011011
//  4 11011011
//  5 11011011
//  6 00000000
//  7 00000000
  

Credits

El Condor

El Condor

1 project • 17 followers
Aspiring artist with a side-job as an IT Admin. Part-time write, poët, tinkerer, inventor. Full-time dreamer.

Comments