#include <Wire.h>
#include <DS1307RTC.h>
#include <FastLED.h>
#define LED_PIN 5
#define NUM_LEDS 330
#define BRIGHTNESS 255
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define HOUR_BUTTON_PIN 4
#define MINUTE_BUTTON_PIN 7
#define BRIGHTNESS_BUTTON_PIN 6
#define COLOR_BUTTON_PIN 10
#define MINUTE_5_BUTTON_PIN 3
bool useSecondItIs = false;
uint8_t lastHour = 255;
bool hourButtonPressed = false;
bool minuteButtonPressed = false;
bool minute5ButtonPressed = false;
uint8_t brightnessLevels[] = {50, 100, 150, 200, 255}; // 5 brightness levels
uint8_t brightnessIndex = 1; // Start at second brightness level
CRGB colors[] = {CRGB::Gold, CRGB::Red, CRGB::Blue, CRGB::Green, CRGB::Cyan, CRGB::Magenta, CRGB::Yellow, CRGB::White}; // 8 color options
uint8_t colorIndex = 0; // Start with the first color
const PROGMEM uint16_t WORDS[25][25] = {
{44,45}, // "a"
{132,133,134,135,136,137}, // "one"
{154,155,156,157,158,159}, // "two"
{144,145,146,147,148,149,150,151,152,153 }, // "three"
{168,169,170,171,172,173,174,174}, // "four"
{160,161,162,163,164,165,166,167}, // "five"
{138,139,140,141,142,143}, // "six"
{210,211,212,213,214,215,216,217,218,219}, // "seven"
{176,177,178,179,180,181,182,183,184,185}, // "eight"
{110,111,112,113,114,115,116,117}, // "nine"
{220,221,222,223,224,225}, // "ten"
{186,187,188,189,190,191,192,193,194,195,196,197}, // "eleven"
{198,199,200,201,202,203,204,205,206,207,208}, // "twelve"
{48,49,50,51,52,53,54,55,56,57,58,59,60,61}, // "quarter"
{76,77,78,79,80,81,82,83,84,85,86,87}, // "twenty"
{88,89,90,91,92,93,94,95}, // "half"
{124,125,126,127,128,129,130,131}, // "past"
{106,107,108,109}, // "to"
{32,33,34,35}, // "is"
{38,39,40,41}, // "it"
{68,69,70,71,72,73,74,75}, // "five" (for minutes)
{98,99,100,101,102,103}, // "ten" (for minutes)
{230,231,232,233,234,235,236,237,238,239,240,241}, // "O'clock"
{6,7,8,9}, // "is"
{0,1,2,3} // "it"
};
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(brightnessLevels[brightnessIndex]);
Serial.begin(9600);
useSecondItIs = false;
pinMode(HOUR_BUTTON_PIN, INPUT_PULLUP);
pinMode(MINUTE_BUTTON_PIN, INPUT_PULLUP);
pinMode(BRIGHTNESS_BUTTON_PIN, INPUT_PULLUP);
pinMode(COLOR_BUTTON_PIN, INPUT_PULLUP);
pinMode(MINUTE_5_BUTTON_PIN, INPUT_PULLUP);
}
void convertTimeToWords(uint8_t hours, uint8_t minutes, int8_t* CurrentTime) {
minutes = (minutes / 5) * 5;
if (minutes >= 0 && minutes < 5) { CurrentTime[0] = 22; CurrentTime[1] = -1; }
else if (minutes == 5 || minutes == 55) { CurrentTime[0] = 20; CurrentTime[1] = -1; }
else if (minutes == 10 || minutes == 50) { CurrentTime[0] = 21; CurrentTime[1] = -1; }
else if (minutes == 15 || minutes == 45) { CurrentTime[0] = 13; CurrentTime[1] = -1; }
else if (minutes == 20 || minutes == 40) { CurrentTime[0] = 14; CurrentTime[1] = -1; }
else if (minutes == 25 || minutes == 35) { CurrentTime[0] = 14; CurrentTime[1] = 20; }
else if (minutes == 30) { CurrentTime[0] = 15; CurrentTime[1] = -1; }
CurrentTime[2] = (minutes >= 5 && minutes < 35) ? 16 : (minutes >= 35 && minutes <= 59) ? 17 : -1;
if (minutes >= 35) hours = (hours + 1) % 12;
if (hours > 12) hours = hours % 12;
CurrentTime[3] = (hours == 0) ? 12 : hours;
}
void displayAdditionalMinutes(uint8_t minutes) {
uint8_t remainingMinutes = minutes % 5;
for (uint8_t i = 0; i < 4; i++) {
leds[328 - i*2] = (i < remainingMinutes) ? colors[colorIndex] : CRGB::Black;
}
}
void displayTime(int8_t* CurrentTime, uint8_t minutes) {
FastLED.clear();
if (CurrentTime[3] != lastHour) {
useSecondItIs = !useSecondItIs;
lastHour = CurrentTime[3];
}
lightUpWord(useSecondItIs ? 24 : 19);
lightUpWord(useSecondItIs ? 23 : 18);
for (uint8_t i = 0; i < 4; i++) {
if (CurrentTime[i] != -1) {
lightUpWord(CurrentTime[i]);
}
}
displayAdditionalMinutes(minutes);
FastLED.show();
}
void lightUpWord(uint8_t wordIndex) {
for (uint8_t i = 0; i < 25; i++) {
uint16_t led = pgm_read_word(&WORDS[wordIndex][i]);
if (led != 0 && led < NUM_LEDS) {
leds[led] = colors[colorIndex]; // Use the selected color
}
}
}
void loop() {
tmElements_t tm;
leds[244] = colors[colorIndex];
if (RTC.read(tm)) {
int8_t CurrentTime[4] = {-1, -1, -1, -1};
uint8_t hours = tm.Hour;
uint8_t minutes = tm.Minute;
Serial.print(hours < 10 ? "0" : "");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes < 10 ? "0" : "");
Serial.println(minutes);
// Check if the hour button is pressed to adjust the hour
if (digitalRead(HOUR_BUTTON_PIN) == LOW) {
if (!hourButtonPressed) {
hourButtonPressed = true;
hours = (hours + 1) % 24;
tm.Hour = hours;
RTC.write(tm);
Serial.println("Time increased by 1 hour:");
Serial.print(hours < 10 ? "0" : "");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes < 10 ? "0" : "");
Serial.println(minutes);
convertTimeToWords(hours, minutes, CurrentTime);
displayTime(CurrentTime, minutes);
delay(100); // debounce delay
}
} else {
hourButtonPressed = false;
}
// Check if the minute button is pressed to adjust the minutes
if (digitalRead(MINUTE_BUTTON_PIN) == LOW) {
if (!minuteButtonPressed) {
minuteButtonPressed = true;
minutes = (minutes + 1) % 60;
tm.Minute = minutes;
RTC.write(tm);
Serial.println("Time increased by 1 minute:");
Serial.print(hours < 10 ? "0" : "");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes < 10 ? "0" : "");
Serial.println(minutes);
convertTimeToWords(hours, minutes, CurrentTime);
displayTime(CurrentTime, minutes);
delay(100); // debounce delay
}
} else {
minuteButtonPressed = false;
}
// Check if the minute5 button is pressed to adjust the minutes by 5
if (digitalRead(MINUTE_5_BUTTON_PIN) == LOW) {
if (!minute5ButtonPressed) {
minute5ButtonPressed = true;
minutes = (minutes + 5) % 60;
tm.Minute = minutes;
RTC.write(tm);
Serial.println("Time increased by 5 minutes:");
Serial.print(hours < 10 ? "0" : "");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes < 10 ? "0" : "");
Serial.println(minutes);
convertTimeToWords(hours, minutes, CurrentTime);
displayTime(CurrentTime, minutes);
delay(100); // debounce delay
}
} else {
minute5ButtonPressed = false;
}
// Check if the brightness button is pressed to adjust brightness level
if (digitalRead(BRIGHTNESS_BUTTON_PIN) == LOW) {
brightnessIndex = (brightnessIndex + 1) % 5; // Cycle through brightness levels
FastLED.setBrightness(brightnessLevels[brightnessIndex]);
displayTime(CurrentTime, minutes);
delay(500); // debounce delay
}
// Check if the color button is pressed to adjust the color
if (digitalRead(COLOR_BUTTON_PIN) == LOW) {
colorIndex = (colorIndex + 1) % 8; // Cycle through color options
displayTime(CurrentTime, minutes);
delay(500); // debounce delay
}
convertTimeToWords(hours, minutes, CurrentTime);
displayTime(CurrentTime, minutes);
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(1000);
}
}