#include DS3231 rtc(SDA, SCL); int TimeArray[4]; #ifdef __AVR__ #include #endif #include #define PixelPin 2 #define Pixels 22 #define Pixel_5min 7 #define Pixel_10min 5 #define Pixel_20min 0 #define Pixel_drei_1 6 #define Pixel_drei_2 8 #define Pixel_viertel_1 1 #define Pixel_viertel_2 4 #define Pixel_vor 9 #define Pixel_ab 10 #define Pixel_halb_1 2 #define Pixel_halb_2 3 #define Pixel_3 20 #define Pixel_4 21 #define Pixel_5 11 #define Pixel_6 12 #define Pixel_7 13 #define Pixel_8 14 #define Pixel_9 19 #define Pixel_10 18 #define Pixel_1 17 #define Pixel_2 15 #define Pixel_11_1 17 #define Pixel_11_2 16 #define Pixel_12_1 16 #define Pixel_12_2 15 int RGB_1[] = {255, 255, 255}; int RGB_2[] = {255, 255, 255}; int RGB_3[] = {255, 255, 255}; int RGB_4[] = {255, 255, 255}; Adafruit_NeoPixel Strip = Adafruit_NeoPixel(Pixels, PixelPin, NEO_RGB + NEO_KHZ400); #define ModeButton A0 #define ColorButton A1 #define TimeSetButton A2 unsigned long PushTime; unsigned long ReleaseTime; bool prevStateModeButton = 1; unsigned long ModeButtonPushTime; unsigned long ModeButtonReleaseTime; bool prevStateColorButton = 1; unsigned long ColorButtonPushTime; unsigned long ColorButtonReleaseTime; bool prevStateTimeSetButton = 1; unsigned long TimeSetButtonPushTime; unsigned long TimeSetButtonReleaseTime; unsigned long ChangeTime; int ColorMode = 0; String Dim = "down"; int Brightness = 12; int Mode = 1; String OnOrOff = "ON"; // SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP_SETUP void setup() { rtc.begin(); Strip.begin(); Strip.show(); pinMode(ModeButton, INPUT_PULLUP); pinMode(ColorButton, INPUT_PULLUP); pinMode(TimeSetButton, INPUT_PULLUP); Serial.begin(9600); //StartUpShow(); readTime(); showLEDs(TimeArray[1], TimeArray[2]); } // LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP_LOOP void loop() { String ButtonOutput = ButtonCheck(); if (ButtonOutput != "none") { ButtonAction(ButtonOutput); ColorModes(ColorMode); showLEDs(TimeArray[1], TimeArray[2]); if (ButtonOutput != "TimeSetButtonShort") { delay(599); readTime(); showLEDs(TimeArray[1], TimeArray[2]); } } readTime(); ModeAction(Mode); if (TimeArray[3] == 0 && TimeArray[2] % 5 == 0 && OnOrOff == "ON") { ColorModes(ColorMode); showLEDs(TimeArray[1], TimeArray[2]); delay(1000); } else if (OnOrOff == "OFF") { StripOff(); } } // FUNCTIONS_FUNCTIONS_FUNCTIONS_FUNCTIONS_FUNCTIONS_FUNCTIONS_FUNCTIONS_FUNCTIONS void readTime() { //reads time from RTC Module TimeArray[3] = rtc.getTime().sec; TimeArray[2] = rtc.getTime().min; TimeArray[1] = rtc.getTime().hour; TimeArray[0] = rtc.getTime().dow; //Serial.print("current time: "); //Serial.print(TimeArray[1]); //Serial.print(":"); //Serial.print(TimeArray[2]); //Serial.print(":"); //Serial.println(TimeArray[3]); //Serial.print("current Date of week: "); //Serial.println(TimeArray[0]); } void writeTime(int DOW, int Hour, int Min, int Sec) { //writes time to RTC Module rtc.setTime(Hour, Min, Sec); if (DOW == 1) { rtc.setDOW(MONDAY); } else if (DOW == 2) { rtc.setDOW(TUESDAY); } else if (DOW == 3) { rtc.setDOW(WEDNESDAY); } else if (DOW == 4) { rtc.setDOW(THURSDAY); } else if (DOW == 5) { rtc.setDOW(FRIDAY); } else if (DOW == 6) { rtc.setDOW(SATURDAY); } else if (DOW == 7) { rtc.setDOW(SUNDAY); } Serial.print("new Time: "); Serial.print(Hour); Serial.print(":"); Serial.print(Min); Serial.print(":"); Serial.println(Sec); Serial.print("new Date of week: "); Serial.println(DOW); } void StripOff() { //Shuts off all LEDs for (int i = 0; i < Pixels; i++) { Strip.setPixelColor(i, 0, 0, 0); } Strip.show(); } void showLEDs(int Hour, int Min) { // shows up LEDs for given Time StripOff(); int Chance_drei_viertel = 70; int Chance_10_halb = 30; long randomNumber = random(100); bool nextHour = false; if (5 <= Min && Min < 10) { Strip.setPixelColor(Pixel_5min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_ab, RGB_2[0], RGB_2[1], RGB_2[2]); } else if (10 <= Min && Min < 15) { Strip.setPixelColor(Pixel_10min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_ab, RGB_2[0], RGB_2[1], RGB_2[2]); } else if (15 <= Min && Min < 20) { if (randomNumber < Chance_drei_viertel) { Strip.setPixelColor(Pixel_viertel_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_viertel_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else { Strip.setPixelColor(Pixel_viertel_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_viertel_2, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_ab, RGB_2[0], RGB_2[1], RGB_2[2]); } } else if (20 <= Min && Min < 25) { if (randomNumber < Chance_10_halb) { Strip.setPixelColor(Pixel_10min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_vor, RGB_2[0], RGB_2[1], RGB_2[2]); Strip.setPixelColor(Pixel_halb_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_halb_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else { Strip.setPixelColor(Pixel_20min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_ab, RGB_2[0], RGB_2[1], RGB_2[2]); } } else if (25 <= Min && Min < 30) { Strip.setPixelColor(Pixel_5min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_vor, RGB_2[0], RGB_2[1], RGB_2[2]); Strip.setPixelColor(Pixel_halb_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_halb_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else if (30 <= Min && Min < 35) { Strip.setPixelColor(Pixel_halb_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_halb_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else if (35 <= Min && Min < 40) { Strip.setPixelColor(Pixel_5min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_ab, RGB_2[0], RGB_2[1], RGB_2[2]); Strip.setPixelColor(Pixel_halb_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_halb_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else if (40 <= Min && Min < 45) { if (randomNumber < Chance_10_halb) { Strip.setPixelColor(Pixel_10min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_ab, RGB_2[0], RGB_2[1], RGB_2[2]); Strip.setPixelColor(Pixel_halb_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_halb_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else { Strip.setPixelColor(Pixel_20min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_vor, RGB_2[0], RGB_2[1], RGB_2[2]); nextHour = true; } } else if (45 <= Min && Min < 50) { if (randomNumber < Chance_drei_viertel) { Strip.setPixelColor(Pixel_drei_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_drei_2, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_viertel_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_viertel_2, RGB_3[0], RGB_3[1], RGB_3[2]); nextHour = true; } else { Strip.setPixelColor(Pixel_viertel_1, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_viertel_2, RGB_3[0], RGB_3[1], RGB_3[2]); Strip.setPixelColor(Pixel_vor, RGB_2[0], RGB_2[1], RGB_2[2]); nextHour = true; } } else if (50 <= Min && Min < 55) { Strip.setPixelColor(Pixel_10min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_vor, RGB_2[0], RGB_2[1], RGB_2[2]); nextHour = true; } else if (55 <= Min && Min < 60) { Strip.setPixelColor(Pixel_5min, RGB_1[0], RGB_1[1], RGB_1[2]); Strip.setPixelColor(Pixel_vor, RGB_2[0], RGB_2[1], RGB_2[2]); nextHour = true; } if (nextHour == true) { Hour = Hour + 1; } if (Hour > 12) { Hour = Hour - 12; } switch (Hour) { case 0: Strip.setPixelColor(Pixel_12_1, RGB_4[0], RGB_4[1], RGB_4[2]); Strip.setPixelColor(Pixel_12_2, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 1: Strip.setPixelColor(Pixel_1, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 2: Strip.setPixelColor(Pixel_2, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 3: Strip.setPixelColor(Pixel_3, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 4: Strip.setPixelColor(Pixel_4, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 5: Strip.setPixelColor(Pixel_5, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 6: Strip.setPixelColor(Pixel_6, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 7: Strip.setPixelColor(Pixel_7, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 8: Strip.setPixelColor(Pixel_8, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 9: Strip.setPixelColor(Pixel_9, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 10: Strip.setPixelColor(Pixel_10, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 11: Strip.setPixelColor(Pixel_11_1, RGB_4[0], RGB_4[1], RGB_4[2]); Strip.setPixelColor(Pixel_11_2, RGB_4[0], RGB_4[1], RGB_4[2]); break; case 12: Strip.setPixelColor(Pixel_12_1, RGB_4[0], RGB_4[1], RGB_4[2]); Strip.setPixelColor(Pixel_12_2, RGB_4[0], RGB_4[1], RGB_4[2]); break; } Strip.show(); } void ColorModes(int ColorMode) { // converts "ColorMode" Int into RGB Values defined below switch (ColorMode) { case 0: //White { int RGB[] = {255, 255, 255}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 1: //red { int RGB[] = {255, 0, 0}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 2: //green { int RGB[] = {0, 255, 0}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 3: //blue { int RGB[] = {0, 0, 255}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 4: //magenta { int RGB[] = {255, 0, 255}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 5: //yellow { int RGB[] = {255, 255, 0}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 6: //cyan { int RGB[] = {0, 255, 255}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 7: //Random, all in one Color { int R = random(256); int G = random(256); int B = random(256); int RGB[] = {R, G, B}; memcpy(RGB_1, RGB, sizeof RGB); memcpy(RGB_2, RGB, sizeof RGB); memcpy(RGB_3, RGB, sizeof RGB); memcpy(RGB_4, RGB, sizeof RGB); } break; case 8: //Random, with fading { int R = random(256); int G = random(256); int B = random(256); int RGB1[] = {R, G, B}; int RGB2[] = {R, G, B}; int RGB3[] = {R, G, B}; int RGB4[] = {R, G, B}; int Minimum = min(R, min(G, B)); int Delta = min((R + G + B - Minimum) / 5 , (255 - Minimum) / 3); int Value_1 = Minimum + Delta; int Value_2 = Value_1 + Delta; int Value_3 = Value_2 + Delta; if (R <= G && R <= B) { RGB2[0] = Value_1; RGB3[0] = Value_2; RGB4[0] = Value_3; } else if (G <= R && G <= B) { RGB2[1] = Value_1; RGB3[1] = Value_2; RGB4[1] = Value_3; } else if (B <= R && B <= G) { RGB2[2] = Value_1; RGB3[2] = Value_2; RGB4[2] = Value_3; } memcpy(RGB_1, RGB1, sizeof RGB1); memcpy(RGB_2, RGB2, sizeof RGB2); memcpy(RGB_3, RGB3, sizeof RGB3); memcpy(RGB_4, RGB4, sizeof RGB4); Serial.println(Delta); } break; case 9: //Random, all in different Colors { int R = random(256); int G = random(256); int B = random(256); int RGB1[] = {R, G, B}; memcpy(RGB_1, RGB1, sizeof RGB1); R = random(256); G = random(256); B = random(256); int RGB2[] = {R, G, B}; memcpy(RGB_2, RGB2, sizeof RGB2); R = random(256); G = random(256); B = random(256); int RGB3[] = {R, G, B}; memcpy(RGB_3, RGB3, sizeof RGB3); R = random(256); G = random(256); B = random(256); int RGB4[] = {R, G, B}; memcpy(RGB_4, RGB4, sizeof RGB4); } break; } } String ButtonCheck() { //returns String which button has how long been pressed String Output; if (digitalRead(ModeButton) != prevStateModeButton) { if (digitalRead(ModeButton) == 0) { //ModeButton pushed PushTime = millis(); ChangeTime = millis(); } else { //ModeButton released ReleaseTime = millis(); if (ReleaseTime - PushTime < 200) { Output = "ModeButtonShort"; } } prevStateModeButton = digitalRead(ModeButton); } else if (digitalRead(ModeButton) == 0 && prevStateModeButton == 0 && millis() - PushTime > 500) { if (millis() - ChangeTime > 600) { ChangeTime = millis(); Output = "ModeButtonLong"; } } if (digitalRead(ColorButton) != prevStateColorButton) { if (digitalRead(ColorButton) == 0) { //ColorButton pushed PushTime = millis(); ChangeTime = millis(); } else { //ColorButton released ReleaseTime = millis(); if (ReleaseTime - PushTime < 200) { Output = "ColorButtonShort"; } } prevStateColorButton = digitalRead(ColorButton); } else if (digitalRead(ColorButton) == 0 && prevStateColorButton == 0 && millis() - PushTime > 500) { if (millis() - ChangeTime > 600) { ChangeTime = millis(); Output = "ColorButtonLong"; } } if (digitalRead(TimeSetButton) != prevStateTimeSetButton) { if (digitalRead(TimeSetButton) == 0) { //TimeSetButton pushed PushTime = millis(); ChangeTime = millis(); } else { //TimeSetButton released ReleaseTime = millis(); if (ReleaseTime - PushTime < 200) { Output = "TimeSetButtonShort"; } } prevStateTimeSetButton = digitalRead(TimeSetButton); } else if (digitalRead(TimeSetButton) == 0 && prevStateTimeSetButton == 0 && millis() - PushTime > 500) { if (millis() - ChangeTime > 600) { ChangeTime = millis(); Output = "TimeSetButtonLong"; } } if (Output == "") { Output = "none"; } return Output; } void ButtonAction(String Action) { // calls an Action triggered by a button if (Action == "ModeButtonShort") { if (Mode < 2) { Mode = Mode + 1; } else { Mode = 1; } TimeArray[1] = Mode; TimeArray[0] = 0; TimeArray[2] = 0; } else if (Action == "ModeButtonLong") { readTime(); TimeArray[0] ++; if (TimeArray[0] == 8) { TimeArray[0] = 1; } writeTime(TimeArray[0], TimeArray[1], TimeArray[2], TimeArray[3]); TimeArray[1] = TimeArray[0]; TimeArray[0] = 0; TimeArray[2] = 0; } else if (Action == "ColorButtonShort") { if (ColorMode == 9) { ColorMode = 0; } else { ColorMode ++; } ColorModes(ColorMode); TimeArray[0] = 0; TimeArray[1] = ColorMode; TimeArray[2] = 25; } else if (Action == "ColorButtonLong") { if (Dim == "down") { Brightness = Brightness - 1; if (Brightness == 1) { Dim = "up"; } } else if (Dim == "up") { Brightness = Brightness + 1; if (Brightness == 12) { Dim = "down"; } } Strip.setBrightness(255 * pow(0.7, 12 - Brightness)); TimeArray[0] = 0; TimeArray[1] = Brightness - 1; TimeArray[2] = 35; } else if (Action == "TimeSetButtonShort") { readTime(); if (TimeArray[2] < 55) { writeTime(TimeArray[0], TimeArray[1], TimeArray[2] - TimeArray[2] % 5 + 5, 0); } else { writeTime(TimeArray[0], TimeArray[1], 0, 0); } } else if (Action == "TimeSetButtonLong") { if (TimeArray[1] < 23) { writeTime(TimeArray[0], TimeArray[1] + 1, TimeArray[2], TimeArray[3]); } else { writeTime(TimeArray[0], 0, TimeArray[2], TimeArray[3]); } if (TimeArray[1] + 1 < 12) { TimeArray[2] = 25; } else { TimeArray[2] = 35; } } } void ModeAction(int Mode) { // converts Mode Int into ON or OFF status switch (Mode) { case 1: // always ON Mode OnOrOff = "ON"; break; case 2: // OFF at night and working hours if (TimeArray[0] == 1 || TimeArray[0] == 2 || TimeArray[0] == 3 || TimeArray[0] == 4) { if (TimeArray[1] < 16) { OnOrOff = "OFF"; } else if (TimeArray[1] < 23) { OnOrOff = "ON"; } else if (TimeArray[1] >= 23) { OnOrOff = "OFF"; } } else if (TimeArray[0] == 5) { if (TimeArray[1] < 14) { OnOrOff = "OFF"; } else if (TimeArray[1] >= 14) { OnOrOff = "ON"; } } else if (TimeArray[0] == 6) { if (TimeArray[1] < 2) { OnOrOff = "ON"; } else if (TimeArray[1] < 8) { OnOrOff = "OFF"; } else if (TimeArray[1] >= 8) { OnOrOff = "ON"; } } else if (TimeArray[0] == 7) { if (TimeArray[1] < 2) { OnOrOff = "ON"; } else if (TimeArray[1] < 8) { OnOrOff = "OFF"; } else if (TimeArray[1] < 23) { OnOrOff = "ON"; } else if (TimeArray[1] >= 23) { OnOrOff = "OFF"; } } break; } } void StartUpShow() { //shows up a cool intor demo on start up ColorMode = 0; ColorModes(9); uint16_t i, j; for (j = 0; j < 256; j++) { for (i = 0; i < Strip.numPixels(); i++) { Strip.setPixelColor(i, Wheel((i + j) & 255)); } Strip.show(); delay(20); } ColorMode = 0; ColorModes(ColorMode); } uint32_t Wheel(byte WheelPos) { //copied from Neopixel library examples "strandtest" WheelPos = 255 - WheelPos; if (WheelPos < 85) { return Strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return Strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return Strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); }