#include <DS3232RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <FastLED.h>
DS3232RTC RTC;
#define NUM_LEDS 86 // Number of LED controles (remember I have 3 leds / controler
#define COLOR_ORDER GRB // Define color order for your strip
#define DATA_PIN 6 // Data pin for led comunication
#define analogInPin A0 // Analog input pin that the potentiometer is attached to
#define UPDATES_PER_SECOND 100
CRGB leds[NUM_LEDS]; // Define LEDs strip
byte digits[10][21] = { //2D Array for numbers on 7 segment
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, //0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0}, //1
{1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1}, //2
{1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, //3
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}, //4
{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1}, //5
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1}, //6
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, //7
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, //8
{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, //9
};
unsigned long stopwatch = 0;
unsigned long swsecond = 0;
unsigned long swminute = 0;
bool Dot = true; //Dot state
bool DST = false; //DST state
uint8_t rainbowhue = 0;
int outputValue = 0;
uint8_t vance = 0;
int i;
void setup() {
Wire.begin();
Serial.begin(115200);
LEDS.addLeds<WS2811, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Set LED strip type
BrightnessCheck(); // Set initial brightness
pinMode(2, INPUT_PULLUP); // Define DST adjust button pin
pinMode(4, INPUT_PULLUP); // Define Minutes adjust button pin
pinMode(5, INPUT_PULLUP); // Define Hours adjust button pin
//startseq();
}
int GetTime() {
tmElements_t Now;
RTC.read(Now);
int hour = Now.Hour;
int minutes = Now.Minute;
int second = Now.Second;
if (second % 2 == 0) {
Dot = false;
}
else {
Dot = true;
}
return (hour * 100 + minutes);
}
void BrightnessCheck() {
outputValue = analogRead(analogInPin);
outputValue = map(outputValue, 0, 1023, 0, 255);
LEDS.setBrightness(outputValue);
}
void TimeToArray() {
int Now = GetTime(); // Get time
int cursor = 86;
if (DST) { // if DST is true then add one hour
Now += 100;
}
if (Dot) {
leds[42] = ColorFromPalette( RainbowColors_p, vance);
leds[43] = ColorFromPalette( RainbowColors_p, vance);
}
else {
leds[42] = 0x000000;
leds[43] = 0x000000;
}
for (int i = 1; i <= 4; i++) {
int digit = Now % 10; // get last digit in time
if (i == 1) {
cursor = 65;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
else if (i == 2) {
cursor = 44;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
else if (i == 3) {
cursor = 21;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
else if (i == 4) {
cursor = 0;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
Now /= 10;
}
}
void DSTcheck() {
int buttonDST = digitalRead(2);
if (buttonDST == LOW) {
if (DST) {
DST = false;
}
else if (!DST) {
DST = true;
}
delay(500);
}
}
void TimeAdjust() {
int buttonH = digitalRead(5);
int buttonM = digitalRead(4);
if (buttonH == LOW || buttonM == LOW) {
delay(500);
tmElements_t Now;
RTC.read(Now);
int hour = Now.Hour;
int minutes = Now.Minute;
int second = Now.Second;
if (buttonH == LOW) {
if (Now.Hour == 23) {
Now.Hour = 0;
}
else {
Now.Hour += 1;
}
} else {
if (Now.Minute == 59) {
Now.Minute = 0;
}
else {
Now.Minute += 1;
}
}
RTC.write(Now);
}
}
void numColor() {
EVERY_N_MILLISECONDS(200) {
vance++;
}
}
void startseq() {
for (i = 0; i < NUM_LEDS; i++) {
fill_rainbow(leds, i, rainbowhue, 5);
FastLED.delay(25);
leds[NUM_LEDS] = CRGB::Black;
FastLED.delay(25);
}
}
void loop() {
BrightnessCheck(); // Check brightness
// DSTcheck(); // Check DST
// TimeAdjust(); // Check to se if time is geting modified
// TimeToArray(); // Get leds array with required configuration
numColor();
// EVERY_N_MILLISECONDS(1000) {
// startseq();
// }
timerx();
FastLED.show();
}
void timerx() {
stopwatch = millis();
swsecond = stopwatch / 1000;
swminute = swsecond / 60;
int Now = (swminute, swsecond); // Get time
int cursor = 86;
int k;
if (Dot) {
leds[42] = ColorFromPalette( RainbowColors_p, vance);
leds[43] = ColorFromPalette( RainbowColors_p, vance);
}
else {
leds[42] = 0x000000;
leds[43] = 0x000000;
}
for (int i = 1; i <= 4; i++) {
int digit = Now % 10; // get last digit in time
if (i == 1) {
cursor = 65;
for (k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
} }
else if (i == 2) {
cursor = 44;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
else if (i == 3) {
cursor = 21;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
else if (i == 4) {
cursor = 0;
for (int k = 0; k <= 20; k++) {
if (digits[digit][k] == 1) {
leds[cursor] = ColorFromPalette( RainbowColors_p, vance);
}
else if (digits[digit][k] == 0) {
leds[cursor] = 0x000000;
}
cursor ++;
}
}
Now /= 10;
}
}