#include <TM1637.h>
TM1637 TM1, TM2, TM3, TM4;
int values[4] = {21, 22, 23, 24};
// Define the button pins for incrementing
const int incrementButtonPins[4] = {34, 32, 5, 16};
// Define the button pins for decrementing
const int decrementButtonPins[4] = {35, 33, 17, 4};
// Variables to track the previous button states for incrementing
int previousIncrementButtonStates[4] = {HIGH, HIGH, HIGH, HIGH};
// Variables to track the previous button states for decrementing
int previousDecrementButtonStates[4] = {HIGH, HIGH, HIGH, HIGH};
void setup() {
Serial.begin(115200);
TM1.begin(14, 12, 4);
TM1.displayClear();
TM1.setBrightness(7);
TM2.begin(26, 27, 4);
TM2.displayClear();
TM2.setBrightness(7);
TM3.begin(22, 23, 4);
TM3.displayClear();
TM3.setBrightness(7);
TM4.begin(18, 19, 4);
TM4.displayClear();
TM4.setBrightness(7);
// Set the button pins as inputs with internal pull-up resistors
for (int i = 0; i < 4; i++) {
pinMode(incrementButtonPins[i], INPUT_PULLUP);
pinMode(decrementButtonPins[i], INPUT_PULLUP);
}
}
void loop() {
for (int i = 0; i < 4; i++) {
// Read the current state of the increment button
int currentIncrementButtonState = digitalRead(incrementButtonPins[i]);
// Check if the button state has changed from HIGH to LOW
if (previousIncrementButtonStates[i] == HIGH && currentIncrementButtonState == LOW) {
// Increment the corresponding value
values[i]++;
Serial.print("Value ");
Serial.print(i);
Serial.print(" (incremented): ");
Serial.println(values[i]);
// Wait until the button is released
while (digitalRead(incrementButtonPins[i]) == LOW);
delay(50); // Additional delay to avoid multiple increments from button bouncing
}
// Update the previous button state for incrementing
previousIncrementButtonStates[i] = currentIncrementButtonState;
// Read the current state of the decrement button
int currentDecrementButtonState = digitalRead(decrementButtonPins[i]);
// Check if the button state has changed from HIGH to LOW
if (previousDecrementButtonStates[i] == HIGH && currentDecrementButtonState == LOW) {
// Decrement the corresponding value
values[i]--;
Serial.print("Value ");
Serial.print(i);
Serial.print(" (decremented): ");
Serial.println(values[i]);
// Wait until the button is released
while (digitalRead(decrementButtonPins[i]) == LOW);
delay(50); // Additional delay to avoid multiple decrements from button bouncing
}
// Update the previous button state for decrementing
previousDecrementButtonStates[i] = currentDecrementButtonState;
// Update the corresponding display
switch (i) {
case 0:
TM1.displayInt(values[i]);
break;
case 1:
TM2.displayInt(values[i]);
break;
case 2:
TM3.displayInt(values[i]);
break;
case 3:
TM4.displayInt(values[i]);
break;
}
}
}