#include <FastLED.h>
#include <SevSeg.h>
#include "Button.h"
#include "Clock.h"
#include "config.h"
#define NUM_LEDS 140
#define LED_PIN 2
#define purple CRGB(0x8010ff) // Purple
#define green CRGB(0x00ff00) // Green
#define gold CRGB(0xffd700) // Gold
#define white CRGB(0xffffff) // White
#define red CRGB(0xff0000) // Red
#define blue CRGB(0x0000ff) // Blue
#define orange CRGB(0xff8c00) // Orange
#define lightblue CRGB(0x33ffff) // Lightblue
CRGB streakcolor=white;
int colorStatus=0;
const int COLON_PIN = 13;
int hour_current_status=99;
CRGB leds[NUM_LEDS];
Button hourButton(A0);
Button minuteButton(A1);
Button modalityButton(A2);
Button luminosityButton(A3);
Clock clock;
SevSeg sevseg;
int val;
int modality = 0;
int inputPin = 3;
int current_h_am=99;
int current_h_pm=99;
int end_m=-99;
int minStatus=0;
int h_start_e = 139;
int h_start_sono = 135;
int h_start_le=132;
int h_start_luna=128;
int h_start_due=71;
int h_start_tre=113;
int h_start_quattro=116;
int h_start_cinque=106;
int h_start_sei=124;
int h_start_sette=94;
int h_start_otto=85;
int h_start_nove=74;
int h_start_dieci=89;
int h_start_undici=100;
int h_start_dodici=79;
int m_start_e=58;
int m_start_un=60;
int m_start_dieci=52;
int m_start_quarto=64;
int m_start_venti=38;
int m_start_trenta=23;
int m_start_quaranta=43;
int m_start_cinquanta=29;
int m_start_cinque=15;
int lumens=35;
char charMinute;
char charSecond;
int intMinute;
int intSecond;
enum DisplayState {
DisplayClock,
};
DisplayState displayState = DisplayClock;
long lastStateChange = 0;
void changeDisplayState(DisplayState newValue) {
displayState = newValue;
lastStateChange = millis();
}
long millisSinceStateChange() {
return millis() - lastStateChange;
}
void setColon(bool value) {
digitalWrite(COLON_PIN, value ? LOW : HIGH);
}
void clockState() {
if (hourButton.pressed()) {
clock.incrementHour();
}
if (minuteButton.pressed()) {
clock.incrementMinute();
}
if (modalityButton.pressed()) {
switch (modality){
case 0:
modality=1;
FastLED.setBrightness(0);
FastLED.show();
break;
case 1:
modality=0;
FastLED.setBrightness(lumens);
FastLED.show();
break;
}
}
if (luminosityButton.pressed()) {
switch (lumens){
case 5:
if(streakcolor==purple)
{
streakcolor=green;
updateLedStripColor(streakcolor);
}
else if(streakcolor==green)
{
streakcolor=gold;
updateLedStripColor(streakcolor);
}
else if(streakcolor==gold)
{
streakcolor=white;
updateLedStripColor(streakcolor);
}
else if(streakcolor==white)
{
streakcolor=red;
updateLedStripColor(streakcolor);
}
else if(streakcolor==red)
{
streakcolor=blue;
updateLedStripColor(streakcolor);
}
else if(streakcolor==blue)
{
streakcolor=orange;
updateLedStripColor(streakcolor);
}
else if(streakcolor==orange)
{
streakcolor=purple;
updateLedStripColor(streakcolor);
}
lumens=10;
break;
case 10:
lumens=15;
break;
case 15:
lumens=20;
break;
case 20:
lumens=25;
break;
case 25:
lumens=30;
break;
case 30:
lumens=35;
break;
case 35:
lumens=5;
break;
}
FastLED.setBrightness(lumens);
FastLED.show();
}
if (modality==1) {
val = digitalRead(inputPin);
if (val == HIGH) {
FastLED.setBrightness(lumens);
FastLED.show();
}
if (val == LOW) {
FastLED.setBrightness(0);
FastLED.show();
}
}
}
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(lumens);
Serial.begin(115200);
clock.begin();
hourButton.begin();
hourButton.set_repeat(500, 200);
minuteButton.begin();
minuteButton.set_repeat(500, 200);
modalityButton.begin();
modalityButton.set_repeat(500, 200);
luminosityButton.begin();
luminosityButton.set_repeat(500, 200);
pinMode(inputPin, INPUT);
pinMode(COLON_PIN, OUTPUT);
byte digits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12};
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = true;
bool disableDecPoint = true;
}
////////////////////////////////////////////START HERE////////////////////////////////////////
void loop() {
clockState();
showTimeOnLedStrip();
}
void showTimeOnLedStrip() {
DateTime now = clock.now();
String stringMinute = String(now.minute());
intSecond=now.second();
if(intSecond<30) {
updateLedStripOff(10,3);
}
if(intSecond>=30) {
updateLedStripOn(10,3);
}
intMinute = charMinute - '0';
int minutesLen=stringMinute.length();
if (minutesLen<2) {
charMinute = stringMinute[0];
}
else {
charMinute = stringMinute[1];
}
intMinute = charMinute - '0';
if(minStatus!=intMinute)
{
if(intMinute==0 or intMinute==5) {
//updateLedStriOff(127,1);
updateLedStripOff(1,1);
updateLedStripOff(2,1);
updateLedStripOff(4,1);
updateLedStripOff(6,1);
updateLedStripOff(8,1);
minStatus=0;
}
if(intMinute==1 or intMinute==6) {
updateLedStripOn(1,1);
updateLedStripOn(2,1);
minStatus=1;
}
if(intMinute==2 or intMinute==7) {
updateLedStripOn(1,1);
updateLedStripOn(4,1);
minStatus=2;
}
if(intMinute==3 or intMinute==8) {
updateLedStripOn(1,1);
updateLedStripOn(6,1);
minStatus=3;
}
if(intMinute==4 or intMinute==9) {
updateLedStripOn(1,1);
updateLedStripOn(8,1);
minStatus=intMinute;
}
}
if (now.hour()!=current_h_am and now.hour()!=current_h_pm) {
if (now.hour()==1 or now.hour()==13) {
updateLedStripOff(h_start_dodici,6);
updateLedStripOff(h_start_sono,4);
updateLedStripOff(h_start_le,2);
updateLedStripOn(h_start_e,1);
updateLedStripOn(h_start_luna,4);
current_h_am=1;
current_h_pm=13;
}
if (now.hour()==2 or now.hour()==14) {
updateLedStripOff(h_start_e,1);
updateLedStripOff(h_start_luna,4);
updateLedStripOn(h_start_sono,4);
updateLedStripOn(h_start_le,2);
updateLedStripOn(h_start_due,3);
current_h_am=2;
current_h_pm=14;
}
if (now.hour()==3 or now.hour()==15) {
updateLedStripOff(h_start_due,3);
updateLedStripOn(h_start_tre,3);
current_h_am=3;
current_h_pm=15;
}
if (now.hour()==4 or now.hour()==16) {
updateLedStripOff(h_start_tre,3);
updateLedStripOn(h_start_quattro,7);
current_h_am=4;
current_h_pm=16;
}
if (now.hour()==5 or now.hour()==17) {
updateLedStripOff(h_start_quattro,7);
updateLedStripOn(h_start_cinque,6);
current_h_am=5;
current_h_pm=17;
}
if (now.hour()==6 or now.hour()==18) {
updateLedStripOff(h_start_cinque,6);
updateLedStripOn(h_start_sei,3);
current_h_am=6;
current_h_pm=18;
}
if (now.hour()==7 or now.hour()==19) {
updateLedStripOff(h_start_sei,3);
updateLedStripOn(h_start_sette,5);
current_h_am=7;
current_h_pm=19;
}
if (now.hour()==8 or now.hour()==20) {
updateLedStripOff(h_start_sette,5);
updateLedStripOn(h_start_otto,4);
current_h_am=8;
current_h_pm=20;
}
if (now.hour()==9 or now.hour()==21) {
updateLedStripOff(h_start_otto,4);
updateLedStripOn(h_start_nove,4);
current_h_am=9;
current_h_pm=21;
}
if (now.hour()==10 or now.hour()==22) {
updateLedStripOff(h_start_nove,4);
updateLedStripOn(h_start_dieci,5);
current_h_am=10;
current_h_pm=22;
}
if (now.hour()==11 or now.hour()==23) {
updateLedStripOff(h_start_dieci,5);
updateLedStripOn(h_start_undici,6);
current_h_am=11;
current_h_pm=23;
}
if (now.hour()==12 or now.hour()==0) {
updateLedStripOff(h_start_undici,6);
updateLedStripOn(h_start_dodici,6);
current_h_am=12;
current_h_pm=0;
}
}
if(now.minute()>=end_m or end_m==59) {
if (now.minute()>=0 and now.minute()<5) {
updateLedStripOff(m_start_cinquanta,9);
updateLedStripOff(m_start_cinque,6);
updateLedStripOff(m_start_e,1);
end_m=5;
}
if (now.minute()>=5 and now.minute()<10) {
updateLedStripOn(m_start_e,1);
updateLedStripOn(m_start_cinque,6);
end_m=10;
}
if (now.minute()>=10 and now.minute()<15) {
updateLedStripOn(m_start_e,1);
updateLedStripOff(m_start_cinque,6);
updateLedStripOn(m_start_dieci,5);
end_m=15;
}
if (now.minute()>=15 and now.minute()<20) {
updateLedStripOn(m_start_e,1);
updateLedStripOff(m_start_dieci,5);
updateLedStripOn(m_start_un,2);
updateLedStripOn(m_start_quarto,6);
end_m=20;
}
if (now.minute()>=20 and now.minute()<25) {
updateLedStripOn(m_start_e,1);
updateLedStripOff(m_start_un,2);
updateLedStripOff(m_start_quarto,6);
updateLedStripOn(m_start_venti,5);
end_m=25;
}
if (now.minute()>=25 and now.minute()<30) {
updateLedStripOn(m_start_e,1);
updateLedStripOn(m_start_cinque,6);
end_m=30;
}
if (now.minute()>=30 and now.minute()<35) {
updateLedStripOn(m_start_e,1);
updateLedStripOff(m_start_venti,5);
updateLedStripOff(m_start_cinque,6);
updateLedStripOn(m_start_trenta,6);
end_m=35;
}
if (now.minute()>=35 and now.minute()<40) {
updateLedStripOn(m_start_e,1);
updateLedStripOn(m_start_cinque,6);
end_m=40;
}
if (now.minute()>=40 and now.minute()<45) {
updateLedStripOn(m_start_e,1);
updateLedStripOff(m_start_trenta,6);
updateLedStripOff(m_start_cinque,6);
updateLedStripOn(m_start_quaranta,8);
end_m=45;
}
if (now.minute()>=45 and now.minute()<50) {
updateLedStripOn(m_start_e,1);
updateLedStripOn(m_start_cinque,6);
end_m=50;
}
if (now.minute()>=50 and now.minute()<55) {
updateLedStripOn(m_start_e,1);
updateLedStripOff(m_start_quaranta,8);
updateLedStripOff(m_start_cinque,6);
updateLedStripOn(m_start_cinquanta,9);
end_m=55;
}
if (now.minute()>=55 and now.minute()<=59) {
updateLedStripOn(m_start_e,1);
updateLedStripOn(m_start_cinque,6);
end_m=59;
}
}
}
void updateLedStripOn(int start_at, int lenght) {
int n;
for(n=start_at;n<start_at+lenght;n+=1) {
leds[n] = streakcolor;
FastLED.show();
}
}
void updateLedStripOff(int start_at, int lenght) {
int n;
for(n=start_at;n<start_at+lenght;n+=1) {
leds[n] = CRGB::Black;
FastLED.show();
}
}
void updateLedStripColor(CRGB streakcolor){
int n;
for(n=0;n<140;n+=1) {
leds[n] = CRGB::Black;
}
current_h_am=99;
current_h_pm=99;
end_m=-99;
minStatus=0;
FastLED.show();
}