// Constants
const int numSamples = 12; // Defined how many samples we have
const int longPressDuration = 5000; // long press duration to capture it as long 5 seconds
const int blinkDuration = 500; // 0.5 second for blink
// Pin definitions for buttons and led's (You can change them if you want)
int buttonPins[numSamples] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int redLedPins[numSamples] = { 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44 };
int greenLedPins[numSamples] = { 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45 };
int mainRedButtonPin = 46;
int mainGreenButtonPin = 47;
int mainRedLedPin = 48;
int mainGreenLedPin = 49;
// Below 1000 is millisecods in 1 second, 60 are seconds in one minute, next 60 are minute in 1 hour, and multiply them with 24
unsigned long allowedTime = 1000UL * 60 * 60 * 24; // Set the allowed sample time in milliseconds ()
// Creating arrays to track states for each button
unsigned long buttonPressTime[numSamples];
unsigned long sampleStartTime[numSamples];
bool isSampleActive[numSamples];
bool isLongPress[numSamples];
// Helper functions to set LED ON/OFF
void setLed(int pin, bool state) {
digitalWrite(pin, state);
}
// Helper function to receive pin number of red and green led to make them blink
void blinkLeds(int pin1, int pin2, int times) {
for (int i = 0; i < times; i++) {
setLed(pin1, HIGH);
setLed(pin2, HIGH);
delay(blinkDuration);
setLed(pin1, LOW);
setLed(pin2, LOW);
delay(blinkDuration);
}
}
void resetTimers() { // This function will reset time array
for (int i = 0; i < numSamples; i++) {
isSampleActive[i] = false;
sampleStartTime[i] = 0;
}
}
void setup() {
// Initialize pins
for (int i = 0; i < numSamples; i++) { // Loop on all buttons
pinMode(buttonPins[i], INPUT_PULLUP); // Set button pin as input with pullup (We need to get a low signal on button press)
pinMode(redLedPins[i], OUTPUT); // Set led pin as output
pinMode(greenLedPins[i], OUTPUT); // Set led pin as output
setLed(redLedPins[i], HIGH); // Initially turn on red led's
setLed(greenLedPins[i], LOW); // Initially turn off green led's
}
pinMode(mainRedButtonPin, INPUT_PULLUP); // Set side red button pin as input with pullup (We need to get a low signal on button press)
pinMode(mainGreenButtonPin, INPUT_PULLUP); // Set side green button pin as input with pullup (We need to get a low signal on button press)
pinMode(mainRedLedPin, OUTPUT); // Set side button red led pin as output
pinMode(mainGreenLedPin, OUTPUT); // Set side button green led pin as output
setLed(mainRedLedPin, HIGH); // Turn on side button red led
setLed(mainGreenLedPin, HIGH); // Turn on side button green led
// Initialize state variables
resetTimers(); // Reset all times in start
}
void loop() {
// Check each sample button
for (int i = 0; i < numSamples; i++) {
int buttonState = digitalRead(buttonPins[i]); // Read the state of the button
unsigned long currentTime = millis(); // Get the current time
if (buttonState == LOW) { // If button is pressed
buttonPressTime[i] = currentTime; // Record the time when button was first pressed
while (digitalRead(buttonPins[i]) == LOW) { ; } // wait until button is released
if (millis() - buttonPressTime[i] > longPressDuration) { // If the button has been pressed for longer than the long press duration
isLongPress[i] = true; // Set the long press flag
}
if (isLongPress[i]) { // If it was a long press
if (isSampleActive[i]) { // If the sample is currently active
blinkLeds(redLedPins[i], greenLedPins[i], 3); // Blink both LEDs to indicate reset
isSampleActive[i] = false; // Deactivate the sample
setLed(redLedPins[i], HIGH); // Turn on red LED
setLed(greenLedPins[i], LOW); // Turn off green LED
}
} else { // If it was a short press
if (!isSampleActive[i]) { // If the sample is not currently active
isSampleActive[i] = true; // Activate the sample
sampleStartTime[i] = currentTime; // Record the start time of the sample
setLed(redLedPins[i], LOW); // Turn off red LED
setLed(greenLedPins[i], HIGH); // Turn on green LED
} else { // If the sample is already active
blinkLeds(redLedPins[i], greenLedPins[i], 3); // Blink both LEDs to indicate reset
sampleStartTime[i] = currentTime; // Reset the start time of the sample
setLed(redLedPins[i], LOW); // Turn off red LED
setLed(greenLedPins[i], HIGH); // Turn on green LED
}
}
buttonPressTime[i] = 0; // Reset the button press time
isLongPress[i] = false; // Reset the long press flag
}
// Check if the sample has been active for more than 24 hours
if (isSampleActive[i] && (currentTime - sampleStartTime[i] > allowedTime)) {
isSampleActive[i] = false; // Deactivate the sample
setLed(redLedPins[i], HIGH); // Turn on red LED
setLed(greenLedPins[i], LOW); // Turn off green LED
}
}
// Check the state of the main red button
int mainRedButtonState = digitalRead(mainRedButtonPin); // Read the state of the main red button
// Check the state of the main green button
int mainGreenButtonState = digitalRead(mainGreenButtonPin); // Read the state of the main green button
if ((mainRedButtonState == LOW) || (mainGreenButtonState == LOW)) {
delay(500); // If any red or green button is pressed wait for 500 milliseconds
int mainRedButtonState = digitalRead(mainRedButtonPin); // Read the state of the main red button
// Check the state of the main green button
int mainGreenButtonState = digitalRead(mainGreenButtonPin); // Read the state of the main green button
}
// Check if both main red and green buttons are pressed for a reset
if (mainRedButtonState == LOW && mainGreenButtonState == LOW) {
unsigned long startTime = millis(); // Record the start time of the simultaneous press
while (digitalRead(mainRedButtonPin) == LOW && digitalRead(mainGreenButtonPin) == LOW) {
if (millis() - startTime > longPressDuration) { // If both buttons are pressed for longer than the long press duration
blinkLeds(mainRedLedPin, mainGreenLedPin, 3); // Blink both main LEDs to indicate reset
resetTimers(); // Reset all sample timers
for (int i = 0; i < numSamples; i++) {
setLed(redLedPins[i], HIGH); // Turn on all red LEDs
setLed(greenLedPins[i], LOW); // Turn off all green LEDs
}
setLed(mainRedLedPin, HIGH); // Turn on main red LED
setLed(mainGreenLedPin, HIGH); // Turn on main green LED
break; // Exit the while loop
}
}
}
if (mainRedButtonState == LOW) { // If the main red button is pressed
for (int i = 0; i < numSamples; i++) {
setLed(greenLedPins[i], LOW); // Turn off all green LEDs
setLed(redLedPins[i], HIGH); // Turn on all red LEDs
}
while (digitalRead(mainRedButtonPin) == LOW) { ; } // Wait until button is released
// Revert LEDs to their indicated colors without interrupting timers
for (int i = 0; i < numSamples; i++) {
if (isSampleActive[i]) {
setLed(redLedPins[i], false); // Turn off red LED
setLed(greenLedPins[i], true); // Turn on green LED
} else {
setLed(redLedPins[i], true); // Turn on red LED
setLed(greenLedPins[i], false); // Turn off green LED
}
}
}
if (mainGreenButtonState == LOW) { // If the main green button is pressed
for (int i = 0; i < numSamples; i++) {
setLed(redLedPins[i], LOW); // Turn off all red LEDs
setLed(greenLedPins[i], HIGH); // Turn on all green LEDs
}
while (digitalRead(mainGreenButtonPin) == LOW) { ; } // Wait until button is released
// Revert LEDs to their indicated colors without interrupting timers
for (int i = 0; i < numSamples; i++) {
if (isSampleActive[i]) {
setLed(redLedPins[i], false); // Turn off red LED
setLed(greenLedPins[i], true); // Turn on green LED
} else {
setLed(redLedPins[i], true); // Turn on red LED
setLed(greenLedPins[i], false); // Turn off green LED
}
}
}
}