#include <RTClib.h>
#include <Adafruit_NeoPixel.h>
#define PIN1 3
#define PIN2 5
#define PIN3 6
#define PIN4 9
#define BUTTONPIN 12
#define DOTPIN 10
#define NUMPIXELS 28
int BRIGHTNESS = 240 ;// 0 - 255
// Define all 4 Modules
Adafruit_NeoPixel module1 = Adafruit_NeoPixel(NUMPIXELS, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel module2 = Adafruit_NeoPixel(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel module3 = Adafruit_NeoPixel(NUMPIXELS, PIN3, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel module4 = Adafruit_NeoPixel(NUMPIXELS, PIN4, NEO_GRB + NEO_KHZ800);
// Define both dots in the middle
Adafruit_NeoPixel dots = Adafruit_NeoPixel(1, DOTPIN, NEO_GRB + NEO_KHZ800);
int delayval = 0;
RTC_DS1307 rtc;
// RGB colors
#define RED 255
#define GREEN 23
#define BLUE 235
// Splitting numbers variables
int hour_tens;
int hour_ones;
int min_tens;
int min_ones;
// Variable for storing previous number in order to clear LED strip
int previous_min_ones;
int previous_min_tens;
int previous_hour_ones;
int previous_hour_tens;
// -----------------------------
// Button operation variables
// Boolsche Variablen, die gesetzt werden
bool mode1 = false;
bool timeSetMode = false;
bool mode3 = false;
// Timer Variablen
unsigned long pressStartTime = 0;
bool pressHandled = false;
// Debounce Variablen
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // Entprellzeit in Millisekunden
int buttonState;
int lastButtonState = HIGH; // Initialer Zustand des Knopfes, assuming pull-up
// -----------------------------
// Flag for DST (Daylight Saving Time) correction
bool isDST = false; // Set this to true if currently in DST
void setup() {
// Start pixels
module1.begin();
module2.begin();
module3.begin();
module4.begin();
dots.begin();
Serial.begin(9600);
pinMode(BUTTONPIN, INPUT_PULLUP);
pinMode(A0, INPUT);
// Real Time Clock
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
// Display "----" in RED if RTC isn't found
module1.clear();
module2.clear();
module3.clear();
module4.clear();
for (int i = 24; i < 28; i++) {
module1.setPixelColor(i, module1.Color(255, 0, 0));
module1.show();
module3.setPixelColor(i, module3.Color(255, 0, 0));
module3.show();
module4.setPixelColor(i, module4.Color(255, 0, 0));
module4.show();
delay(delayval);
}
for (int i = 0; i < 4; i++) {
module2.setPixelColor(i, module2.Color(255, 0, 0));
module2.show();
}
while (1) {} // Hold Error "----"
}
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // calibrate RTC
}
}
void loop() {
if(timeSetMode){ // Set-Time-Mode
settingsTime();
}
else{ // Normal Operation
modeButton();
// Set Brightness according to value of potentiometer
BRIGHTNESS = (float)(analogRead(A0)/1023.0)*255.0;
module1.setBrightness(BRIGHTNESS);
module2.setBrightness(BRIGHTNESS);
module3.setBrightness(BRIGHTNESS);
module4.setBrightness(BRIGHTNESS);
dots.setBrightness(BRIGHTNESS);
dots.setPixelColor(0, dots.Color(RED, GREEN, BLUE));
dots.show();
DateTime displayTime = rtc.now();
Serial.print(displayTime.hour(), DEC);
Serial.print(':');
Serial.print(displayTime.minute(), DEC);
Serial.print(':');
Serial.println(displayTime.second(), DEC);
// Splitting Hours and minutes into digits
hour_tens = displayTime.hour() / 10;
hour_ones = displayTime.hour() % 10;
min_tens = displayTime.minute() / 10;
min_ones = displayTime.minute() % 10;
// Showing LED stripes
strip_module1(hour_tens);
strip_module2(hour_ones);
strip_module3(min_tens);
strip_module4(min_ones);
// Clearing LED Strip if number refreshes
if (min_ones != previous_min_ones) {
module4.clear();
previous_min_ones = min_ones;
}
if (min_tens != previous_min_tens) {
module3.clear();
previous_min_tens = min_tens;
}
if (hour_ones != previous_hour_ones) {
module2.clear();
previous_hour_ones = hour_ones;
}
if (hour_tens != previous_hour_tens) {
module1.clear();
previous_hour_tens = hour_tens;
}
}
}
void modeButton(){
// Lese den aktuellen Zustand des Buttons
int reading = digitalRead(BUTTONPIN);
// Überprüfe, ob sich der Button-Zustand geändert hat (Entprellen)
if (reading != lastButtonState) {
lastDebounceTime = millis(); // Setze die Entprellzeit zurück
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// Wenn der aktuelle Zustand des Buttons anders ist als der gespeicherte Zustand
if (reading != buttonState) {
buttonState = reading;
// If button is pressed (buttonDown)
if (buttonState == LOW) {
pressStartTime = millis();
pressHandled = false;
Serial.println("Button pressed");
}
else {
// If button is released (buttonUp)
if (pressStartTime != 0) {
Serial.println("Button released");
if (millis() - pressStartTime <= 2000){
if (isDST) {
adjustTime(1); // Add one hour when the button is pressed if not in DST
}
else {
adjustTime(-1); // Subtract one hour when the button is pressed if in DST
}
Serial.println("Mode 1 aktiviert");
}
if (millis() - pressStartTime > 2000 && millis() - pressStartTime <= 5000){
timeSetMode = true;
Serial.println("Mode 2 aktiviert");
}
if(millis() - pressStartTime > 5000){
Serial.println("Mode 3 aktiviert");
}
}
pressStartTime = 0;
pressHandled = false;
}
}
}
// Speichere den aktuellen Button-Zustand
lastButtonState = reading;
// Things executed directly after reaching required elapsed time without Buttonup event
// Berechne die verstrichene Zeit, wenn der Knopf gedrückt ist
/*if (buttonState == LOW && pressStartTime != 0) {
unsigned long elapsedTime = millis() - pressStartTime;
if (elapsedTime >= 2000 && !pressHandled) {
timeSetMode = true;
Serial.println("timeSetMode activated");
pressHandled = true; // Verhindert, dass dieser Block erneut ausgeführt wird
}
if (elapsedTime >= 5000) {
mode3 = true;
Serial.println("Mode 3 activated");
}
}*/
}
void settingsTime() {
module1.clear();
module2.clear();
module3.clear();
module4.clear();
dots.clear();
// Display "----"
for (int i = 24; i < 28; i++) {
module1.setPixelColor(i, module1.Color(RED, GREEN, BLUE));
module1.show();
module3.setPixelColor(i, module3.Color(RED, GREEN, BLUE));
module3.show();
module4.setPixelColor(i, module4.Color(RED, GREEN, BLUE));
module4.show();
} for (int i = 0; i < 4; i++) {
module2.setPixelColor(i, module2.Color(255, 0, 0));
module2.show();
}
}
// Function for changing Summer to Wintertime or vice versa
void adjustTime(int hours) {
DateTime now = rtc.now();
DateTime newTime = now + TimeSpan(0, hours, 0, 0);
rtc.adjust(newTime);
// Toggle DST flag if the button is pressed
isDST = !isDST;
}
// MODULE 1
void module1_a1() {
for(int i=0;i<4;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module1_a2() {
for(int i=4;i<8;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module1_a3() {
for(int i=8;i<12;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module1_a4() {
for(int i=12;i<16;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module1_a5() {
for(int i=16;i<20;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module1_a6() {
for(int i=20;i<24;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module1_a7() {
for(int i=24;i<28;i++){
module1.setPixelColor(i, module1.Color(RED,GREEN,BLUE));
module1.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void strip_module1(int Num){
switch (Num){
case 0:
module1_a1();
module1_a2();
module1_a3();
module1_a4();
module1_a5();
module1_a6();
break;
case 1:
module1_a1(),
module1_a6();
break;
case 2:
module1_a2();
module1_a3();
module1_a5(),
module1_a6();
module1_a7();
break;
case 3:
module1_a1();
module1_a2();
module1_a5();
module1_a6();
module1_a7();
break;
case 4:
module1_a1();
module1_a4();
module1_a6();
module1_a7();
break;
case 5:
module1_a1();
module1_a2();
module1_a4();
module1_a5();
module1_a7();
break;
case 6:
module1_a1();
module1_a2();
module1_a3();
module1_a4();
module1_a5();
module1_a7();
break;
case 7:
module1_a1();
module1_a5();
module1_a6();
break;
case 8:
module1_a1();
module1_a2();
module1_a3();
module1_a4();
module1_a5();
module1_a6();
module1_a7();
break;
case 9:
module1_a1();
module1_a2();
module1_a4();
module1_a5();
module1_a6();
module1_a7();
break;
}
}
// MODULE 2
void module2_a1() {
for(int i=0;i<4;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module2_a2() {
for(int i=4;i<8;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module2_a3() {
for(int i=8;i<12;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module2_a4() {
for(int i=12;i<16;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module2_a5() {
for(int i=16;i<20;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module2_a6() {
for(int i=20;i<24;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module2_a7() {
for(int i=24;i<28;i++){
module2.setPixelColor(i, module2.Color(RED,GREEN,BLUE));
module2.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void strip_module2(int Num){
switch (Num){
case 0:
module2_a2();
module2_a3();
module2_a4();
module2_a5();
module2_a6();
module2_a7();
break;
case 1:
module2_a2(),
module2_a7();
break;
case 2:
module2_a1();
module2_a2();
module2_a3(),
module2_a5();
module2_a6();
break;
case 3:
module2_a1();
module2_a2();
module2_a3();
module2_a6();
module2_a7();
break;
case 4:
module2_a1();
module2_a2();
module2_a4();
module2_a7();
break;
case 5:
module2_a1();
module2_a3();
module2_a4();
module2_a6();
module2_a7();
break;
case 6:
module2_a1();
module2_a3();
module2_a4();
module2_a5();
module2_a6();
module2_a7();
break;
case 7:
module2_a2();
module2_a3();
module2_a7();
break;
case 8:
module2_a1();
module2_a2();
module2_a3();
module2_a4();
module2_a5();
module2_a6();
module2_a7();
break;
case 9:
module2_a1();
module2_a2();
module2_a3();
module2_a4();
module2_a6();
module2_a7();
break;
}
}
// MODULE 3
void module3_a1() {
for(int i=0;i<4;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module3_a2() {
for(int i=4;i<8;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module3_a3() {
for(int i=8;i<12;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module3_a4() {
for(int i=12;i<16;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module3_a5() {
for(int i=16;i<20;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module3_a6() {
for(int i=20;i<24;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module3_a7() {
for(int i=24;i<28;i++){
module3.setPixelColor(i, module3.Color(RED,GREEN,BLUE));
module3.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void strip_module3(int Num){
switch (Num){
case 0:
module3_a1();
module3_a2();
module3_a3();
module3_a4();
module3_a5();
module3_a6();
break;
case 1:
module3_a3(),
module3_a4();
break;
case 2:
module3_a1();
module3_a2();
module3_a4(),
module3_a5();
module3_a7();
break;
case 3:
module3_a2();
module3_a3();
module3_a4();
module3_a5();
module3_a7();
break;
case 4:
module3_a3();
module3_a4();
module3_a6();
module3_a7();
break;
case 5:
module3_a2();
module3_a3();
module3_a5();
module3_a6();
module3_a7();
break;
case 6:
module3_a1();
module3_a2();
module3_a3();
module3_a5();
module3_a6();
module3_a7();
break;
case 7:
module3_a3();
module3_a4();
module3_a5();
break;
case 8:
module3_a1();
module3_a2();
module3_a3();
module3_a4();
module3_a5();
module3_a6();
module3_a7();
break;
case 9:
module3_a2();
module3_a3();
module3_a4();
module3_a5();
module3_a6();
module3_a7();
break;
}
}
// MODULE 4
void module4_a1() {
for(int i=0;i<4;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module4_a2() {
for(int i=4;i<8;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module4_a3() {
for(int i=8;i<12;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module4_a4() {
for(int i=12;i<16;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module4_a5() {
for(int i=16;i<20;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module4_a6() {
for(int i=20;i<24;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void module4_a7() {
for(int i=24;i<28;i++){
module4.setPixelColor(i, module4.Color(RED,GREEN,BLUE));
module4.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void strip_module4(int Num){
switch (Num){
case 0:
module4_a1();
module4_a2();
module4_a3();
module4_a4();
module4_a5();
module4_a6();
break;
case 1:
module4_a3(),
module4_a4();
break;
case 2:
module4_a1();
module4_a2();
module4_a4(),
module4_a5();
module4_a7();
break;
case 3:
module4_a2();
module4_a3();
module4_a4();
module4_a5();
module4_a7();
break;
case 4:
module4_a3();
module4_a4();
module4_a6();
module4_a7();
break;
case 5:
module4_a2();
module4_a3();
module4_a5();
module4_a6();
module4_a7();
break;
case 6:
module4_a1();
module4_a2();
module4_a3();
module4_a5();
module4_a6();
module4_a7();
break;
case 7:
module4_a3();
module4_a4();
module4_a5();
break;
case 8:
module4_a1();
module4_a2();
module4_a3();
module4_a4();
module4_a5();
module4_a6();
module4_a7();
break;
case 9:
module4_a2();
module4_a3();
module4_a4();
module4_a5();
module4_a6();
module4_a7();
break;
}
}