Hello everyone,
I have a small question. It's actually a very simple code, but I couldn't figure out one point.
There are 4 LEDs and 1 button. each one representing time. Like 5 minutes, 10 minutes, 15 minutes, 20 minutes. Each time you press the button, the relevant LED lights up and the time is selected. When the 2nd button is pressed, the relay will remain open for the selected time and will automatically close at the end of the time.
I'm using "delay" here. But since it stops the entire flow, I cannot stop the process by pressing a 3rd button (or pressing the 2nd button once more).
I tried using Millis, but it didn't work as I wanted. It does the first part of the condition, but the relay pin does not close at the end of the time.
Thanks in advance for your help
int ledPin[4] = {8, 9, 10, 11};
int Relay = 7;
int dk=5; // Minute first value
unsigned long oldtime = 0;
unsigned long newtime;
unsigned long interval = 200;
const int buttonPin1 = 12; // Min Select
const int buttonPin2 = 13; // Start
const int buttonPin3 = 6; // Stop Test
int buttonState1 = HIGH;
int buttonState2 = HIGH;
int buttonState3 = HIGH;
int pushCounter = 0;
int numberOfLED = 4;
void setup() {
pinMode(Relay,OUTPUT);
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin2, INPUT);
for (int i = 0; i <= 4; i++) {
pinMode(ledPin[i], OUTPUT);
}
}
void loop() {
newtime = millis();
// Serial.println(millis());
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
if (buttonState1 == LOW) {
for (int i = 0; i < numberOfLED; i++) {
if (pushCounter % numberOfLED == i) {
digitalWrite(ledPin[i], HIGH);
dk=i; // Min Select (5,10, 15, 20)
}
else {
digitalWrite(ledPin[i], LOW);
}
}
pushCounter++;
delay(300);
}
// Serial.println(newtime);
// Serial.println(oldtime);
// 5 min condition
if (dk==0 && buttonState2 == LOW) {
Serial.println("5 min");
digitalWrite(Relay,HIGH); // Relay ON
delay(3000); // Wait 5 min
digitalWrite(Relay,LOW); // Relay OFF
digitalWrite(ledPin[0], LOW); // 5 min led OFF
pushCounter = 0; // LED Count zero
dk=5;
}
// 10 min condition
if (dk==1 && buttonState2 == LOW) {
Serial.println("10 min");
digitalWrite(Relay,HIGH); // Relay ON
delay(4000); // Wait 10 min
digitalWrite(Relay,LOW); // Relay OFF
digitalWrite(ledPin[1], LOW); // 10 min led OFF
pushCounter = 0; // LED Count zero
dk=5;
}
// 15 min condition
if (dk==2 && buttonState2 == LOW) {
Serial.println("15 min");
digitalWrite(Relay,HIGH); // Relay ON
delay(5000); // Wait 15 min
digitalWrite(Relay,LOW); // Relay OFF
digitalWrite(ledPin[2], LOW); // 15 min led OFF
pushCounter = 0; // LED Count zero
dk=5;
}
// 20 min condition
if (dk==3 && buttonState2 == LOW) {
Serial.println("20 min");
digitalWrite(Relay,HIGH); // Relay ON
delay(6000); // Wait 20 min
digitalWrite(Relay,LOW); // Relay OFF
digitalWrite(ledPin[3], LOW); // 20 min led OFF
pushCounter = 0; // LED Count zero
dk=5;
}
// 3rd button stop testing
if (buttonState3 == LOW) {
digitalWrite(Relay,LOW);
digitalWrite(ledPin[0], LOW);
pushCounter = 0;
dk=5;
}
}