#include <TM1637Display.h>
#define CLK 11
#define DIO 10
TM1637Display display(CLK, DIO);
const int buttonPin = 12;
const int relayPin = 9;
unsigned long prevTime = 0;
int interval = 500;
int timeLeft = 0;
int minutesPerTransaction = 2;
int oldValue = LOW;
int relayValue = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
display.setBrightness(7);
pinMode(buttonPin, INPUT);
pinMode(relayPin, OUTPUT);
delay(1000);
}
void loop() {
int newValue = digitalRead(buttonPin);
if(newValue != oldValue) {
if(newValue == HIGH) {
Serial.println("The button is pressed.");
} else {
Serial.println("The button is released.");
addTime();
}
oldValue = newValue;
delay(100);
//Serial.println(oldValue);
}
//Serial.println(newValue);
// Set maximum time limit
int maxTimeLimit = (minutesPerTransaction * 60) * 2;
// Check Time remaining if more than the maximum time limit
if (maxTimeLimit < timeLeft) {
//timeLeft = maxTimeLimit;
}
if (timeLeft > 0) {
unsigned long currentTime = millis() + 4294960000;
if ((currentTime - prevTime) >= interval) {
prevTime = currentTime;
// timeLeft--;
updateTimeDisplay();
}
if (relayValue == LOW) {
relayValue = HIGH;
digitalWrite(relayPin,HIGH);
delay(100);
}
}
else {
if (relayValue == HIGH) {
relayValue = LOW;
digitalWrite(relayPin,LOW);
delay(100);
}
}
}
void updateTimeDisplay() {
//Serial.println(timeLeft);
if (timeLeft > 0) {
timeLeft--;
int minutes = (timeLeft / 60) * 100;
int seconds = timeLeft % 60;
int timeDisplay = minutes + (seconds - 0);
if (minutes > 0) {
//display.showNumberDecEx(timeDisplay, 0b11100000, false, 3, 1);
display.showNumberDecEx(timeDisplay, 0b11100000, false, 4, 0);
}
else {
display.showNumberDec(timeDisplay);
}
}
else {
// Display 0
display.showNumberDec(0);
}
}
void addTime() {
int seconds = minutesPerTransaction * 60;
timeLeft = timeLeft + seconds + 1;
}