Well, im trying to do a timer for four relays, when i touch on the keypad "A,B,C,D" to activate the relays (A for relay1, B for relay2...) and start counting 2 hours after activating the relay, imagining, I activate relay "B" and after an hour I activate relay "D", but relay "B" continues to count without resetting and relay "D" starts counting, that is, independent timers for each of them, and after this time ends, the relay turn off...
Do someone know how to help? Thanks!!!
The actual code is:
#include <Keypad.h>
#include <Password.h>
#include <Key.h>
#include <Keypad_I2C.h>
int buzzer = 11;
int noAccesled = 3;
int AccesLed = 10;
int noAcces = 1;
int passinput = 0;
long flashvarled = 0;
long flashtimeled = 300;
//Relays
int relay1 = 7;
int relay2 = 6;
int relay3 = 5;
int relay4 = 4;
/*
- tried to do this a lot of time but idk how to do, use this if that's useful
int statusrelay1 = LOW;
int statusrelay2 = LOW;
int statusrelay3 = LOW;
int statusrelay4 = LOW;
long ant_millis_relay1 = 0;
long ant_millis_relay2 = 0;
long ant_millis_relay3 = 0;
long ant_millis_relay4 = 0;
long inter_relay1 = 2000;
long inter_relay2 = 2000;
long inter_relay3 = 2000;
long inter_relay4 = 2000;
*/
//keypad
#define I2CADDR 0x20
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [ROWS] = {0, 1, 2, 3};
byte colPins [COLS] = {4, 5, 6, 7};
Keypad_I2C keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);
Password password = Password("5"); // change the access code here
void setup() {
Serial.begin(9600);
Wire .begin ();
keypad.begin (makeKeymap (keys));
Serial.begin(9600);
pinMode(relay1, OUTPUT);
digitalWrite(relay1, 0);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, 0);
pinMode(relay3, OUTPUT);
digitalWrite(relay3, 0);
pinMode(relay4, OUTPUT);
digitalWrite(relay4, 0);
pinMode(noAccesled, OUTPUT);
digitalWrite(noAccesled, HIGH);
pinMode(AccesLed, OUTPUT);
digitalWrite(AccesLed, 0);
pinMode(buzzer, OUTPUT);
}
void loop() {
char key = keypad.getKey();
if (noAcces) {
if (passinput) {
unsigned long currentvarled = millis();
if (currentvarled - flashvarled > flashtimeled) {
flashvarled = currentvarled;
digitalWrite(noAccesled, !digitalRead(noAccesled));
}
}
else {
digitalWrite(noAccesled, HIGH);
}
digitalWrite(AccesLed, 0);
}
if (key != NO_KEY) {
Serial.println(key);
password.append(key);
passinput = 1;
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
if (key == '*') {
password.reset();
passinput = 0;
noAcces = 1;
}
}
if (password.evaluate()) {
noAcces = !noAcces;
password.reset();
passinput = 0;
}
if (!noAcces) {
passinput = 0;
digitalWrite(noAccesled, LOW);
digitalWrite(AccesLed, 255);
delay(10);
switch (key) {
case 'A':
digitalWrite(relay1, !digitalRead(relay1));
break;
case 'B':
digitalWrite(relay2, !digitalRead(relay2));
break;
case 'C':
digitalWrite(relay3, !digitalRead(relay3));
break;
case 'D':
digitalWrite(relay4, !digitalRead(relay4));
break;
case '#':
digitalWrite(relay1, 0);
digitalWrite(relay2, 0);
digitalWrite(relay3, 0);
digitalWrite(relay4, 0);
break;
}
password.reset();
}
}