led-ring-clock-ntp/led-ring-clock.ino

293 lines
7.5 KiB
Arduino
Raw Normal View History

2016-11-26 05:01:55 +01:00
//
2018-09-09 04:56:52 +02:00
// Copyright (c) 2016-2018 jackw01
2016-11-26 05:01:55 +01:00
// This code is distrubuted under the MIT License, see LICENSE for details
//
2018-09-09 04:56:52 +02:00
#include <math.h>
2016-11-26 05:01:55 +01:00
#include <FastLED.h>
#include <Wire.h>
#include <EEPROM.h>
2018-09-09 04:56:52 +02:00
#include <RTClib.h>
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
#include "config.h"
2016-11-26 05:01:55 +01:00
2017-09-29 02:06:10 +02:00
CRGB leds[ledRingSize];
2016-11-26 05:01:55 +01:00
RTC_DS1307 rtc;
2018-09-09 04:56:52 +02:00
// Globals to keep track of state
int clockMode, colorScheme;
int lastLoopTime = 0;
int lastButtonClickTime = 0;
2017-09-26 23:16:01 +02:00
int lastDebugMessageTime = 0;
2018-09-09 04:56:52 +02:00
uint8_t currentBrightness;
uint8_t previousBrightness[16];
DateTime now;
2016-11-26 05:01:55 +01:00
void setup() {
2018-09-09 04:56:52 +02:00
// Begin serial port
Serial.begin(serialPortBaudRate);
2016-11-26 05:01:55 +01:00
// Init FastLED
2017-09-29 02:06:10 +02:00
FastLED.addLeds<NEOPIXEL, pinLeds>(leds, ledRingSize);
2016-11-26 05:01:55 +01:00
FastLED.setTemperature(Halogen);
FastLED.show();
2018-09-09 04:56:52 +02:00
// Connect to the RTC
2016-11-26 05:01:55 +01:00
Wire.begin();
rtc.begin();
2018-09-09 04:56:52 +02:00
// Set button pin
2016-11-26 05:01:55 +01:00
pinMode(pinButton, INPUT);
2018-09-09 04:56:52 +02:00
// Read saved config from EEPROM
colorScheme = EEPROM.read(eepromAddrColorScheme);
clockMode = EEPROM.read(eepromAddrClockMode);
2017-09-26 23:16:01 +02:00
2018-09-09 04:56:52 +02:00
// If button is pressed at startup, light all LEDs
2017-09-26 23:16:01 +02:00
if (digitalRead(pinButton) == LOW) {
2017-09-29 02:06:10 +02:00
for (int i = 0; i < ledRingSize; i++) leds[i] = white;
2017-09-26 23:16:01 +02:00
FastLED.show();
delay(60000);
}
2016-11-26 05:01:55 +01:00
}
void loop() {
2018-09-09 04:56:52 +02:00
int currentTime = millis();
if (currentTime - lastLoopTime > runLoopIntervalMs) {
lastLoopTime = millis();
// Handle button
if (digitalRead(pinButton) == LOW && currentTime - lastButtonClickTime > buttonClickRepeatDelayMs) {
delay(buttonLongPressDelayMs);
if (digitalRead(pinButton) == LOW) {
lastButtonClickTime = currentTime;
colorScheme ++;
if (colorScheme >= colorSchemeCount) colorScheme = 0;
EEPROM.write(0, colorScheme);
} else {
clockMode ++;
if (clockMode >= ClockModeCount) clockMode = 0;
EEPROM.write(1, clockMode);
}
}
// Print debug message
if (currentTime > lastDebugMessageTime + debugMessageIntervalMs) {
lastDebugMessageTime = currentTime;
printDebugMessage();
}
// Update brightness - do a moving average to smooth out noisy potentiometers
int sum = 0;
for (uint8_t i = 15; i > 0; i--) {
previousBrightness[i] = previousBrightness[i - 1];
sum += previousBrightness[i];
}
previousBrightness[0] = map(analogRead(pinBrightness), 0, 1023, minBrightness, 255);
sum += previousBrightness[0];
currentBrightness = sum / 16;
FastLED.setBrightness(currentBrightness);
// Show clock
now = rtc.now();
clearLeds();
showClock();
}
2016-11-26 05:01:55 +01:00
}
2018-09-09 04:56:52 +02:00
// Display the current clock
2016-11-26 05:01:55 +01:00
void showClock() {
2018-09-09 04:56:52 +02:00
switch (clockMode) {
case ClockModeRingClock:
ringClock();
break;
case ClockModeDotClock:
dotClock();
break;
case ClockModeDotClockColorChange:
rainbowDotClock();
break;
case ClockModeDotClockTimeColor:
timeColorClock();
break;
case ClockModeGlowClock:
glowClock();
break;
}
2016-11-26 05:01:55 +01:00
}
2018-09-09 04:56:52 +02:00
// Print debugging info over serial
2017-09-26 23:16:01 +02:00
void printDebugMessage() {
Serial.print("Current date/time: ");
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Display mode: ");
Serial.println(clockMode);
Serial.print("Color scheme: ");
Serial.println(colorScheme);
Serial.print("Brightness: ");
2017-09-29 02:06:10 +02:00
Serial.println(currentBrightness);
2018-09-09 04:56:52 +02:00
Serial.println("");
2017-09-26 23:16:01 +02:00
}
2016-11-26 05:01:55 +01:00
// Show a ring clock
void ringClock() {
2018-09-09 04:56:52 +02:00
int h = hourPosition();
int m = minutePosition();
int s = secondPosition();
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
if (m > h) {
for (int i = 0; i < m; i++) leds[i] = minuteColor();
for (int i = 0; i < h; i++) leds[i] = hourColor();
2016-11-26 05:01:55 +01:00
} else {
2018-09-09 04:56:52 +02:00
for (int i = 0; i < h; i++) leds[i] = hourColor();
for (int i = 0; i < m; i++) leds[i] = minuteColor();
2016-11-26 05:01:55 +01:00
}
2018-09-09 04:56:52 +02:00
if (showSecondHand) leds[s] = secondColor();
2016-11-26 05:01:55 +01:00
FastLED.show();
}
// Show a more traditional dot clock
void dotClock() {
2018-09-09 04:56:52 +02:00
int h = hourPosition();
int m = minutePosition();
int s = secondPosition();
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
for (int i = h - 1; i < h + 2; i++) leds[wrap(i)] = hourColor();
leds[m] = minuteColor();
if (showSecondHand) [s] = secondColor();
2016-11-26 05:01:55 +01:00
FastLED.show();
}
// Show a dot clock with hands that change color based on their position
void rainbowDotClock() {
2018-09-09 04:56:52 +02:00
int h = hourPosition();
int m = minutePosition();
int s = secondPosition();
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
CRGB newHourColor = CHSV(map(now.hour(), 0, 24, 0, 255), 255, 255);
CRGB newMinuteColor = CHSV(map(now.minute(), 0, 59, 0, 255), 255, 255);
CRGB newSecondColor = CHSV(map(now.second(), 0, 59, 0, 255), 255, 255);
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
for (int i = h - 1; i < h + 2; i++) leds[wrap(i)] = newHourColor;
leds[m] = newMinuteColor;
if (showSecondHand) leds[s] = newSecondColor;
2016-11-26 05:01:55 +01:00
FastLED.show();
}
// Show a dot clock where the color is based on the time
void timeColorClock() {
2018-09-09 04:56:52 +02:00
int h = hourPosition();
int m = minutePosition();
int s = secondPosition();
float decHour = decimalHour();
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
CRGB pixelColor = CHSV((uint8_t)mapFloat(fmod(20.0 - decHour, 24.0), 0.0, 24.0, 0.0, 255.0), 255, 255);
2016-11-26 05:01:55 +01:00
2018-09-09 04:56:52 +02:00
for (int i = h - 1; i < h + 2; i++) leds[wrap(i)] = pixelColor;
leds[m] = pixelColor;
if (showSecondHand) leds[s] = pixelColor;
2016-11-26 05:01:55 +01:00
FastLED.show();
}
// Show a dot clock where the hands overlap with additive blending
void glowClock() {
2018-09-09 04:56:52 +02:00
int h = hourPosition();
int m = minutePosition();
int s = secondPosition();
2016-11-26 05:01:55 +01:00
2017-09-29 02:06:10 +02:00
for (int i = -6; i < ledRingSize + 6; i++) {
2016-11-26 05:01:55 +01:00
int j;
for (j = 0; j <= 4; j++) {
2018-09-09 04:56:52 +02:00
if (h + j == i || h - j == i) blendAdd(wrap(i), CRGB(255, 0, 0), 1 - mapFloat(j, 0.0, 6.0, 0.1, 0.99));
2016-11-26 05:01:55 +01:00
}
for (j = 0; j <= 2; j++) {
2018-09-09 04:56:52 +02:00
if (m + j == i || m - j == i) blendAdd(wrap(i), CRGB(0, 255, 0), 1 - mapFloat(j, 0.0, 3.0, 0.1, 0.99));
2016-11-26 05:01:55 +01:00
}
2018-09-09 04:56:52 +02:00
if (showSecondHand) {
for (j = 0; j <= 1; j++) {
if (s + j == i || s - j == i) blendAdd(wrap(i), CRGB(0, 0, 255), 1 - mapFloat(j, 0.0, 1.0, 0.1, 0.65));
}
2016-11-26 05:01:55 +01:00
}
}
FastLED.show();
}
2018-09-09 04:56:52 +02:00
// Get positions mapped to ring size
int hourPosition() {
if (twelveHour) {
int hour;
if (now.hour() > 12) hour = (now.hour() - 12) * (ledRingSize / 12);
else hour = now.hour() * (ledRingSize / 12);
return hour + int(map(now.minute(), 0, 59, 0, (ledRingSize / 12) - 1));;
} else {
int hour = now.hour() * (ledRingSize / 24);
return hour + int(map(now.minute(), 0, 59, 0, (ledRingSize / 24) - 1));;
}
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
int minutePosition() {
return map(now.minute(), 0, 59, 0, ledRingSize - 1);
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
int secondPosition() {
return map(now.second(), 0, 59, 0, ledRingSize - 1);
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
float decimalHour() {
return (float)now.hour() + mapFloat(now.minute() + mapFloat(now.second(), 0.0, 59.0, 0.0, 1.0), 0.0, 59.0, 0.0, 1.0);
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
// Get colors
CRGB hourColor() {
return colorSchemes[colorScheme][0];
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
CRGB minuteColor() {
return colorSchemes[colorScheme][1];
}
2017-03-26 23:16:25 +02:00
2018-09-09 04:56:52 +02:00
CRGB secondColor() {
return colorSchemes[colorScheme][2];
2017-03-26 23:16:25 +02:00
}
2018-09-09 04:56:52 +02:00
// Clear the LED ring
2016-11-26 05:01:55 +01:00
void clearLeds() {
2017-09-29 02:06:10 +02:00
for (int i = 0; i < ledRingSize; i++) leds[i] = CRGB(0, 0, 0);
2016-11-26 05:01:55 +01:00
}
// Enhanced additive blending
2018-09-09 04:56:52 +02:00
void blendAdd(int position, CRGB color, float factor) {
leds[position].r += color.r * factor;
leds[position].g += color.g * factor;
leds[position].b += color.b * factor;
2016-11-26 05:01:55 +01:00
}
// Wrap around LED ring
int wrap(int i) {
2017-09-29 02:06:10 +02:00
if (i >= ledRingSize) return i - ledRingSize;
else if (i < 0) return ledRingSize + i;
2016-11-26 05:01:55 +01:00
else return i;
}
// Because Arduino does not
2018-09-09 04:56:52 +02:00
float mapFloat(float x, float inMin, float inMax, float outMin, float outMax) {
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
2016-11-26 05:01:55 +01:00
}