LilyPad Arduino Binary Clock

by Nelvig in Circuits > Arduino

34 Views, 0 Favorites, 0 Comments

LilyPad Arduino Binary Clock

F5574FJMB28PRQA.jpg

I like the binary clock project in the book, "30 Arduino Projects for the Evil Genius" by Simon Monk. The only problem was that his clock had problem keeping time. '

I tweaked the code a little and added the RTC module and I think this is an improvement.

The reed switch is so you can set the time. When activated with a magnet the minutes and hours move faster so you can run it through the cycle until it reaches the time you want to set it at. When you remove the magnet from the switch, it slows down to normal and keeps time from there.

Supplies

1 x LilyPad Arduino 328

1 x DS3231 RTC module

1 x Reed switch

4 x red LEDs,

6 x green LEDs

6 x yellow LEDs

14 × 220Ω resistor

1 × 10K resistor


You need the "Time" file from the arduino library.3

The Circuit

Clock.jpg

This is the circuit on a breadboard.

The Code

Here's my code:

#include <Wire.h>

#include <RTClib.h>


RTC_DS3231 rtc;


// 4-bit hours (12h format)

int hourLEDs[] = {A1, A2, A3, A0};


// 6-bit minutes

int minuteLEDs[] = {5, 4, 3, 2, 1, 13};


// 6-bit seconds

int secondLEDs[] = {11, 10, 9, 8, 7, 6};


// All LEDs in order for spin animation

int loopLEDs[] = {5, 4, 3, 2, 1, 13, 11, 10, 9, 8, 7, 6, A1, A2, A3, A0};


// Reed switch connected to 5V, using INPUT_PULLUP

const int switchPin = 12;


unsigned long lastUpdate = 0;

const unsigned long normalDelay = 1000;

const unsigned long fastDelay = 100;


int lastSyncHour = -1;


void setup() {

Wire.begin();

rtc.begin();


// Kör bara EN gång för att ställa tiden:

// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));


for (int i = 0; i < 4; i++) {

pinMode(hourLEDs[i], OUTPUT);

}

for (int i = 0; i < 6; i++) {

pinMode(minuteLEDs[i], OUTPUT);

pinMode(secondLEDs[i], OUTPUT);

}

pinMode(switchPin, INPUT_PULLUP);


// ⏱️ Direkt uppdatera display med tid från RTC vid uppstart

DateTime now = rtc.now();

updateDisplay(now.twelveHour() == 0 ? 12 : now.twelveHour(), now.minute(), now.second());

}


void loop() {

bool fastForward = digitalRead(switchPin) == HIGH;

int delayTime = fastForward ? fastDelay : normalDelay;


DateTime now = rtc.now();


// Fast-forwardläge

if (fastForward && millis() - lastUpdate >= delayTime) {

int newMinute = now.minute() + 1;

int newHour = now.hour();

int newDay = now.day();

int newMonth = now.month();

int newYear = now.year();


if (newMinute >= 60) {

newMinute = 0;

newHour++;

if (newHour >= 24) {

newHour = 0;

newDay++;

}

}


rtc.adjust(DateTime(newYear, newMonth, newDay, newHour, newMinute, now.second()));

lastUpdate = millis();

}


// 🔄 Synka om från RTC varje hel timme

if (now.minute() == 0 && now.second() == 0 && now.hour() != lastSyncHour) {

now = rtc.now();

lastSyncHour = now.hour();

}


// ⏳ Spin-animation vid varje full timme

if (now.minute() == 0 && now.second() == 0) {

int hour = now.twelveHour();

if (hour == 0) hour = 12;

spin(hour);

}


// 💡 Uppdatera LED-visning

updateDisplay(now.twelveHour() == 0 ? 12 : now.twelveHour(), now.minute(), now.second());


delay(delayTime);

}


void updateDisplay(int hour, int minute, int second) {

setOutput(hourLEDs, 4, hour);

setOutput(minuteLEDs, 6, minute);

setOutput(secondLEDs, 6, second);

}


void setOutput(int *ledArray, int numLEDs, int value) {

for (int i = 0; i < numLEDs; i++) {

digitalWrite(ledArray[i], bitRead(value, i));

}

}


void spin(int count) {

for (int i = 0; i < count; i++) {

for (int j = 0; j < 16; j++) {

digitalWrite(loopLEDs[j], HIGH);

delay(50);

digitalWrite(loopLEDs[j], LOW);

}

}

}

Downloads

Final Clock

Here’s a movie of the final result.

Downloads