The timer runs SUPER fast.
#include <TM1637Display.h>
// Define the connections pins:
#define CLK 10
#define DIO 11
TM1637Display display = TM1637Display(CLK, DIO);
int oneButtonPin = 7;
int twoButtonPin = 8;
int threeButtonPin = 9;
int limitSWpin = 6;
int relay = 2;
int times[3] = {30, 60, 90};
int outputTime = 1000;
int one, two, three, lim;
int start = 0; int readyFlag = 0; int index = 0;
int selectedTime = 0;
unsigned long startMillis = 0;
void setup() {
pinMode(oneButtonPin, INPUT_PULLUP);
pinMode(twoButtonPin, INPUT_PULLUP);
pinMode(threeButtonPin, INPUT_PULLUP);
pinMode(limitSWpin, INPUT_PULLUP);
pinMode(relay, OUTPUT);
display.clear(); // Clear the display
display.setBrightness(7); // Set the brightness
delay(100);
}
void loop() {
readButtons();
if (!start ) {
display.showNumberDec(times[index]);
if (one)index = 0;
else if (two) index = 1;
else if (three) index = 2;
if (lim) {
start = 1;
selectedTime = times[index];
startMillis = millis();
}
}
if (start) {
while (selectedTime > 0 ) {
display.showNumberDec(selectedTime, 4);
if (startMillis - millis() >= 1000) {
selectedTime -= 1;
startMillis = millis();
}
}
digitalWrite(relay, HIGH);
delay(outputTime);
digitalWrite(relay, LOW);
start = 0;
}
}
void readButtons() {
one = !digitalRead(oneButtonPin);
two = !digitalRead(twoButtonPin);
three = !digitalRead(threeButtonPin);
lim = !digitalRead(limitSWpin);
//if (one || two || three || lim) delay(100);
}