// Example for the TM1637 display with TM1637_RT library.
// By Koepel, 28 February 2023.
//
// Library: "TM1637_RT", https://github.com/RobTillaart/TM1637_RT
#include <TM1637.h>
TM1637 TM1, TM2, TM3, TM4; // Each display needs its own object
int display1 = 21;
int display2 = 22;
int display3 = 23;
int display4 = 24;
/*const int buttonPin1up = 34;
const int buttonPin1down = 35;
const int buttonPin2up = 32;
const int buttonPin2down = 33;
const int buttonPin3up = 5;
const int buttonPin3down = 17;
const int buttonPin4up = 16;
const int buttonPin4down = 4;
*/
const int buttonPinsup[4] = {34,32,5,16};
const int buttonPinsdown[4] = {35,33,17,4};
int UPVALUES[4] = {0,0,0,0};
int DOWNVALUES[4] = {0,0,0,0};
int prevButtonup[4] = {HIGH,HIGH,HIGH,HIGH}
int prevButtondown[4] = {HIGH,HIGH,HIGH,HIGH}
char buffer[5]; // buffer for third display
void setup()
{
Serial.begin(115200);
// -----------------------------------------
// First display
// -----------------------------------------
TM1.begin(14, 12, 4); // clockpin, datapin, #digits
TM1.displayClear();
TM1.setBrightness(7); // full brightness, default is 3
TM1.displayInt(display1, 0); // Initial text on third display
// -----------------------------------------
// Second display
// -----------------------------------------
TM2.begin(26, 27, 4); // clockpin, datapin, #digits
TM2.displayClear();
TM2.setBrightness(7); // full brightness, default is 3
TM2.displayInt(display2, 0); // Initial text on third display
// -----------------------------------------
// Third display
// -----------------------------------------
TM3.begin(22, 23, 4); // clockpin, datapin, #digits
TM3.displayClear();
TM3.setBrightness(7); // full brightness, default is 3
TM3.displayInt(display3, 0); // Initial text on third display
// -----------------------------------------
// Fourth display
// -----------------------------------------
TM4.begin(18, 19, 4); // clockpin, datapin, #digits
TM4.displayClear();
TM4.setBrightness(7); // full brightness, default is 3
TM4.displayInt(display4, 0); // Initial text on third display
// displays OK
for (int i = 0; i < 4; i++) {
pinMode(buttonPinsup[i], INPUT_PULLUP);
}
for (int i = 0; i < 4; i++) {
pinMode(buttonPinsdown[i], INPUT_PULLUP);
}
}
void loop()
{
for (int i = 0; i < 4; i++) {
// Read the current state of the button
int currentButtonup = digitalRead(buttonPinsup[i]);
int currentButtondown = digitalRead(buttonPinsdown[i]);
// Check if the button state has changed from HIGH to LOW
if (prevButtonup[i] == HIGH && currentButtonup == LOW) {
// Increment the corresponding UPVALUE
UPVALUES[i]++;
// Print the updated UPVALUE
Serial.print("UPVALUE ");
Serial.print(i);
Serial.print(": ");
Serial.println(UPVALUES[i]);
// Wait until the button is released
while (digitalRead(buttonPinsup[i]) == LOW);
// Additional delay to avoid multiple increments from button bouncing
delay(50);
}
if (prevButtonup[i] == HIGH && currentButtonup == LOW) {
// Increment the corresponding UPVALUE
UPVALUES[i]++;
// Print the updated UPVALUE
Serial.print("UPVALUE ");
Serial.print(i);
Serial.print(": ");
Serial.println(UPVALUES[i]);
// Wait until the button is released
while (digitalRead(buttonPinsup[i]) == LOW);
// Additional delay to avoid multiple increments from button bouncing
delay(50);
}
// Update the previous button state
prevButtonup[i] = currentButtonup;
prevButtondown[i] = currentButtondown;
}