Project in progress

Arduino Based Binary Clock © CC BY

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

• 5,096 views
• 17 respects

Components and supplies

 Arduino UNO
×1
 Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Red
×1
 SparkFun Pushbutton switch 12mm
×2

Project

A while ago I acquired an 8x8 LED matrix from Adafruit. I was wondering what to make with it, and I created a nice mini Binary Clock.

The project was based on a couple of other clocks available on the internet. However, a few implemented second counting, which I was really looking for.

I implemented code to display the seconds in the same manner as the minutes. Using a screen draw routine from an Arduino Snake game, I had to fidget around with the coordinate and modulus calculations to get the pixels to show up exactly right.

Steps

First, I had to make sure how to wire things up and make sure the backpack worked. I used the standard Adafruit LED library and demo sketch to check everything.

Then I set about coding a binary clock. It used some code available on the internet (even Hackster.io) to create the sketch. I implemented second counting and a drawing routine that fills two coordinate arrays, so the whole screen gets rewritten every second.

To Do:

I bought a RTC breakout board recently and I am going to see whether I can implement that later on. Now I am seeing a bit of drift in the clock.

It will most probably mean rewriting some of the code because counting does not need to be handled by the Arduino, but I will have to interpret the RTC data to separate integers to use the pixel setting code.

Code

Arduino Sketch for Binary ClockC/C++
```/***************************************************
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>

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];

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) {
if(sunit == 2 || sunit == 3 || sunit == 6 || sunit == 7) {
if(sunit == 4 || sunit == 5 || sunit == 6 || sunit == 7) {
if(sunit == 8 || sunit == 9) {

//seconds
if((second >= 10 && second < 20) || (second >= 30 && second < 40) || (second >= 50 && second < 60))  {
if(second >= 20 && second < 40)  {
if(second >= 40 && second < 60) {

//minutes units
if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {
if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {
if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {
if(munit == 8 || munit == 9) {

//minutes
if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60))  {
if(minute >= 20 && minute < 40)  {
if(minute >= 40 && minute < 60) {

//hour units
if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {
if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {
if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {
if(hunit == 8 || hunit == 9) {

//hour
if(hour >= 10 && hour < 20)  {
if(hour >= 20 && hour < 24)  {
}

//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(displayTimeInString());
}

//interrupt function to change the minutes
void changeminutes() {
second = 0;
minute = minute + 1;
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

```

Schematics

Connections for the Arduino Binary Clock

• 1 project
• 14 followers

April 28, 2017

Members who respect this project

and 9 others

See similar projects
you might like

Binary Clock with Arduino and Plywood

Project tutorial by Kalle Wallin

• 3,804 views
• 1 comment
• 12 respects

Amazing Binary Clock using LED Matrix Module

Project in progress by LAGSILVA

• 11,921 views
• 63 respects

eDOT - Arduino Based Precision Clock and Weather Station

Project tutorial by antiElectron

• 14,005 views
• 35 respects

Digital Clock

Project in progress by Team SharKode

• 8,929 views
• 31 respects

Arduino 24h Binary Clock with Seconds (17 LEDs)

Project in progress by Peter Lunk

• 5,884 views