const unsigned int TIME_ON = 10;
const unsigned int TIME_OFF = 20;
const unsigned long TIME_TO_START_CHANGING = 10000;
const unsigned long FREQUENCY_TO_CHANGE = 10000;
/* ----- DO NOT CHANGE ----- */
#include <Wire.h>
#include <RTClib.h>
const int LED_COUNT = 52;
const int SWITCH_COUNT = 10;
int leds[LED_COUNT] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, A15, A14, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
int switches[SWITCH_COUNT] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9};
const unsigned long DEBOUNCE = 100;
unsigned long millis_previous = 0;
int led_mode = -1;
unsigned long millis_start_pattern = 0;
unsigned long millis_new_pattern = 0;
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
for (int i = 0; i < LED_COUNT; i++) {
pinMode(leds[i], OUTPUT);
}
for (int i = 0; i < SWITCH_COUNT; i++) {
pinMode(switches[i], INPUT);
}
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
bool time_check = (now.hour() <= 10 || now.hour() >= 20);
bool button_0 = check_switch(0);
bool button_1 = check_switch(1);
bool button_2 = check_switch(2);
bool button_3 = check_switch(3);
bool button_4 = check_switch(4);
bool button_5 = check_switch(5);
bool button_6 = check_switch(6);
bool button_7 = check_switch(7);
bool button_8 = check_switch(8);
bool button_9 = check_switch(9);
bool is_pressed = (button_0 || button_1 || button_2 || button_3 || button_4 || button_5 || button_6 || button_7 || button_8 || button_9);
if (is_pressed) {
millis_new_pattern = millis();
led_mode = -1;
leds_off();
millis_start_pattern = millis();
millis_new_pattern = millis();
delay(100);
}
if ((millis() - millis_start_pattern >= TIME_TO_START_CHANGING)) {
if ((millis() - millis_new_pattern >= FREQUENCY_TO_CHANGE)) {
led_mode = (led_mode + 1) % 10;
millis_new_pattern = millis();
}
}
if (button_0 || (led_mode == 0)) {
loop_0(1000);
led_mode = 0;
}
else if (button_1 || (led_mode == 1)) {
loop_1(100);
led_mode = 1;
}
else if (button_2 || (led_mode == 2)) {
loop_2(250);
led_mode = 2;
}
else if (button_3 || (led_mode == 3)) {
loop_3(500);
led_mode = 3;
}
else if (button_4 || (led_mode == 4)) {
loop_4(200);
led_mode = 4;
}
else if (button_5 || (led_mode == 5)) {
loop_5(300);
led_mode = 5;
}
else if (button_6 || (led_mode == 6)) {
loop_6(250);
led_mode = 6;
}
else if (button_7 || (led_mode == 7)) {
loop_7(500);
led_mode = 7;
}
else if (button_8 || (led_mode == 8)) {
loop_8(250);
led_mode = 8;
}
else if (button_9 || (led_mode == 9)) {
loop_9(100);
led_mode = 9;
}
}
bool check_switch(int pin_switch) {
return analogRead(switches[pin_switch]) >= 800;
}
void turn_array_light_on(byte led_array[], byte array_size, bool array_state) {
for (int i = 0; i < array_size; i++) {
digitalWrite(leds[led_array[i]], array_state);
}
}
void loop_0(int duration) {
static unsigned long millis_loop_1 = millis();
static bool state_led_1 = false;
if (millis() - millis_loop_1 >= duration) {
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(leds[i], state_led_1 ? HIGH : LOW);
}
state_led_1 = !state_led_1;
millis_loop_1 = millis();
}
}
void loop_1(int duration) {
static unsigned long previousMillis = 0;
static int currentLED = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= duration) {
digitalWrite(leds[currentLED], HIGH);
previousMillis = currentMillis;
currentLED++;
if (currentLED >= LED_COUNT) {
currentLED = 0;
}
if (currentLED > 0) {
digitalWrite(leds[currentLED - 1], LOW);
} else {
digitalWrite(leds[LED_COUNT - 1], LOW);
}
}
}
void loop_2(int duration) {
static unsigned long previousMillis = 0;
static bool state = false;
if (millis() - previousMillis >= duration) {
previousMillis = millis();
state = !state;
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(leds[i], (i % 2 == 0) ? (state ? HIGH : LOW) : (state ? LOW : HIGH));
}
}
}
void loop_3(int duration) {
static unsigned long previousMillis = 0;
if (millis() - previousMillis >= duration) {
previousMillis = millis();
static int index = 0;
digitalWrite(leds[index], HIGH);
digitalWrite(leds[(index + 1) % LED_COUNT], HIGH);
digitalWrite(leds[(index + 2) % LED_COUNT], HIGH);
index = (index + 3) % LED_COUNT;
digitalWrite(leds[index], LOW);
}
}
void loop_4(int duration) {
static unsigned long previousMillis = 0;
if (millis() - previousMillis >= duration) {
previousMillis = millis();
static int index = 0;
digitalWrite(leds[index], HIGH);
digitalWrite(leds[(index + 4) % LED_COUNT], HIGH);
index = (index + 1) % LED_COUNT;
digitalWrite(leds[index], LOW);
}
}
void loop_5(int duration) {
static unsigned long previousMillis = 0;
if (millis() - previousMillis >= duration) {
previousMillis = millis();
static int index = 0;
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(leds[i], (i == index || i == (index + 6) % LED_COUNT) ? HIGH : LOW);
}
index = (index + 1) % LED_COUNT;
}
}
void loop_6(int duration) {
static unsigned long previousMillis = 0;
static bool state = true;
byte slice_1[17] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 36, 37, 38, 39, 40, 51};
byte slice_2[15] = {2, 3, 41, 42, 43, 50, 16, 15, 35, 34, 33, 49, 30, 31, 32};
byte slice_3[20] = {0, 1, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 48, 47, 44, 45, 46, 17};
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= duration) {
previousMillis = currentMillis;
turn_array_light_on(slice_1, 17, state);
turn_array_light_on(slice_2, 15, !state);
turn_array_light_on(slice_3, 20, state);
state = !state;
}
}
void loop_7(int duration) {
static unsigned long previousMillis = 0;
static bool sShapeOn = true;
static bool mShapeOn = false;
static unsigned long shapeStartTime = 0;
const unsigned long shapeDuration = 100;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= duration) {
previousMillis = currentMillis;
if ((sShapeOn && currentMillis - shapeStartTime >= shapeDuration) ||
(mShapeOn && currentMillis - shapeStartTime >= shapeDuration)) {
for (int i = 0; i < 52; i++) {
digitalWrite(leds[i], LOW);
}
sShapeOn = !sShapeOn;
mShapeOn = !mShapeOn;
shapeStartTime = currentMillis;
int startLed = sShapeOn ? 0 : 30;
int endLed = sShapeOn ? 30 : 52;
for (int i = startLed; i < endLed; i++) {
digitalWrite(leds[i], HIGH);
}
}
}
}
void loop_8(int duration) {
static unsigned long previousMillis = 0;
static int section = 0;
static bool state = true;
byte layer_0[2] = {27, 26};
byte layer_1[2] = {28, 25};
byte layer_2[2] = {29, 24};
byte layer_3[3] = {30, 47, 23};
byte layer_4[2] = {31, 22};
byte layer_5[5] = {32, 33, 48, 46, 21};
byte layer_6[4] = {34, 49, 45, 20};
byte layer_7[9] = {13, 14, 35, 15, 16, 17, 44, 18, 19};
byte layer_8[4] = {12, 36, 50, 43};
byte layer_9[5] = {11, 37, 51, 42, 0};
byte layer_10[6] = {10, 38, 39, 40, 41, 1};
byte layer_11[2] = {9, 2};
byte layer_12[2] = {8, 3};
byte layer_13[2] = {7, 4};
byte layer_14[2] = {6, 5};
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= duration) {
previousMillis = currentMillis;
switch (section) {
case 0:
turn_array_light_on(layer_0, 2, state);
break;
case 1:
turn_array_light_on(layer_1, 2, state);
break;
case 2:
turn_array_light_on(layer_2, 2, state);
break;
case 3:
turn_array_light_on(layer_3, 3, state);
break;
case 4:
turn_array_light_on(layer_4, 2, state);
break;
case 5:
turn_array_light_on(layer_5, 5, state);
break;
case 6:
turn_array_light_on(layer_6, 4, state);
break;
case 7:
turn_array_light_on(layer_7, 9, state);
break;
case 8:
turn_array_light_on(layer_8, 4, state);
break;
case 9:
turn_array_light_on(layer_9, 5, state);
break;
case 10:
turn_array_light_on(layer_10, 6, state);
break;
case 11:
turn_array_light_on(layer_11, 2, state);
break;
case 12:
turn_array_light_on(layer_12, 2, state);
break;
case 13:
turn_array_light_on(layer_13, 2, state);
break;
case 14:
turn_array_light_on(layer_14, 2, state);
state = !state;
break;
default:
break;
}
section = (section + 1) % 15;
}
}
void loop_9(int duration) {
static unsigned long previousMillis = 0;
static int index = 0;
byte pattern_1[26] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 33, 34, 35, 36, 37, 38, 39, 15, 51, 50, 16};
byte pattern_2[26] = {32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 46, 45, 44, 43, 42, 41, 40, 17, 47, 48, 49};
if (millis() - previousMillis >= duration) {
previousMillis = millis();
if (index < 26) {
digitalWrite(leds[pattern_1[index]], HIGH);
digitalWrite(leds[pattern_2[index]], HIGH);
} else {
digitalWrite(leds[pattern_1[sizeof(pattern_1) - 1 - (index - sizeof(pattern_1))]], LOW);
digitalWrite(leds[pattern_2[sizeof(pattern_2) - 1 - (index - sizeof(pattern_2))]], LOW);
}
index = (index + 1) % (sizeof(pattern_1) + sizeof(pattern_2));
}
}
void leds_off() {
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(leds[i], LOW);
}
}