Hi guys!
Bit of background:
I love buiding all kinds of clocks and calendars.
Back in the early 2000’s I built my first binary clock using a lot of 74LS93’s and a hand full of diodes. Nowadays things have gotten a lot easier with very accessible microcontrollers as our beloved Arduino.
Current situation:
I’m currently working on a new version of a binary clock using a chain of WS2812 leds on a strip forming a matrix, an RTC module and an UNO. As diffusers I’m using pingpong balls inspired by mr_fid's beautiful design. As mr_fid doesn't use addressable leds, I had to write a code from scratch. It may not be the most efficient, but I've got things running correctly now. At the moment the LED's are displaying binary digits in a white color.
Desired situation:
In order to get the maximum visual effect I would like to switch between two kind of visual effects using a switch that pulls a pin either high or low.
- HIGH = The matrix of leds to display a slow (horizontal) rainbow fade when the binary digits light up (like in the video below).
- LOW = All the LED's displaying white.
My programming skills are not fantastic, but I can manage. However at this moment of time I’m approaching an area beyond my knowledge and would herefore like to seek some guidance. I would like to know how to embed this rainbow scheme in my code and how to toggle between it.
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_NeoPixel.h>
#define LED_PIN 8
#define LED_COUNT 20
RTC_DS1307 rtc;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Definition of the LED layout
const int columnHeights[] = { 2, 4, 3, 4, 3, 4 };
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1)
;
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
DateTime now = rtc.now();
// Get hours, minutes, and seconds
int hours = now.hour();
int minutes = now.minute();
int seconds = now.second();
// Display hours in binary
displayBinary(hours / 10, 0, true); // First column
displayBinary(hours % 10, 2, false); // Second column
displayBinary(minutes / 10, 6, true); // Third column
displayBinary(minutes % 10, 9, false); // Fourth column
displayBinary(seconds / 10, 13, true); // Fifth column
displayBinary(seconds % 10, 16, false); // Sixth column
//debug (to check the time in the serial monitor)
Serial.print(now.hour());
Serial.print(now.minute());
Serial.print(now.second());
delay(1000); // Update every second
}
// Function to display a decimal number in binary on my LED matrix
void displayBinary(int value, int startLED, bool invert) {
int height = columnHeights[startLED / 3];
for (int i = 0; i < height; i++) {
int index = invert ? height - 1 - i : i;
if (value & (1 << index)) {
strip.setPixelColor(startLED + i, 255, 255, 255); // LEDs on
} else {
strip.setPixelColor(startLED + i, 0, 0, 0); // LEDs off
}
}
strip.show();
}