Binary Clock on LED Matrix

Hello,
I am new to the forum and I just wanted to share a quick demo that I wrote about using the Arduino UNO R4 Wifi board. This is a very basic sketch that will create a simple binary clock on the LED matrix.

// Spellapeaka
// Binary Clock
#include "Arduino_LED_Matrix.h"

// Variable Declarations
unsigned long millsVal;
int interval = 1000;
ArduinoLEDMatrix matrix;

// Set the starting time to 7:43 am
byte hour = 0b0000111;
byte minute = 0b00101011;
byte second = 0b00000000;

// Create the LED Matrix Frame
byte frame[8][12];

void setup() {

  //Begin communication with the matrix and capture first mills value
  matrix.begin();
  millsVal = millis();

  // initialize the LED Matrix Frame to all 0's
  for(int i = 0; i < 8; i++)
    for(int j = 0; j < 12; j++)
      frame[i][j] = 0;
}

void loop() {
  // Calulate mills difference
  int diff = millis() - millsVal;

  // see if one second has passed (1000 milliseconds)
  if(diff > interval) {
    // reset mills value
    millsVal = millis();

    // Advance the clock by one second
    advanceClock();

    //Set the Frame values and draw it to the matrix
    setFrame();
    matrix.renderBitmap(frame, 8, 12);
  }
}

// Will add 1 second to the clock
void advanceClock(){
  second++;
  if (second > 59){
    second = 0;
    minute++;
    if(minute > 59){
      minute = 0;
      hour++;
      if(hour > 11){
        hour = 0;
      }
    }
  }
}

// Set the LED's in the Frame based on the values in hour, minute and second variables
// uses the bitRead function to get the individual bits in the byte value
void setFrame() {
  //Start at 2,2
  //set hour
  frame[2][2] = bitRead(hour,0);
  frame[2][3] = bitRead(hour,1);
  frame[2][4] = bitRead(hour,2);
  frame[2][5] = bitRead(hour,3);

  //set Minute
  frame[4][2] = bitRead(minute,0);
  frame[4][3] = bitRead(minute,1);
  frame[4][4] = bitRead(minute,2);
  frame[4][5] = bitRead(minute,3);
  frame[4][6] = bitRead(minute,4);
  frame[4][7] = bitRead(minute,5);

  //set Second
  frame[6][2] = bitRead(second,0);
  frame[6][3] = bitRead(second,1);
  frame[6][4] = bitRead(second,2);
  frame[6][5] = bitRead(second,3);
  frame[6][6] = bitRead(second,4);
  frame[6][7] = bitRead(second,5);

}

Enjoy :slight_smile:

2 Likes

This will work just as well:

// Set the starting time to 7:43 am
byte hour = 7;
byte minute = 43;
byte second = 0;

because, inside the Arduino, all the numbers will be binary. Even if you type them as decimal, they get converted to binary for storage in the Arduino.

2 Likes

Use that "WiFi" part to get the time from a NTP server (better accuracy and get your money's worth from the board you bought).

1 Like

Thank you odometer. I do like it this way better as it gives you the first frame that should be written though.

Thank you runaway_pancake! That's a great idea. I haven't messed around with the Arduino for a few years. My wife bought me this a a Christmas gift and I am just cracking into it. That sounds like my next step on this!