const byte relaisPin = 2;
const byte buttonPin = 3;
const byte ledPin = 13;
const int RELAY_ON = HIGH;
const int RELAY_OFF = LOW;
const unsigned long period = 14400000ul; // 4 hours
unsigned long lastActivation = 0;
bool relayIsActive = false;
void relayOn() {
digitalWrite(relaisPin, RELAY_ON);
digitalWrite(ledPin, HIGH);
lastActivation = millis(); // do this if you want the pulse only 4h after the last forced activation
relayIsActive = true;
}
void relayOff() {
digitalWrite(relaisPin, RELAY_OFF);
digitalWrite(ledPin, LOW);
relayIsActive = false;
}
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW);
pinMode(relaisPin, OUTPUT); digitalWrite(relaisPin, RELAY_OFF);
}
void loop() {
// forced ON ?
if (digitalRead(buttonPin) == LOW) // LOW means pressed
relayOn();
else
relayOff();
// time to pulse ?
if (!relayIsActive && (millis() - lastActivation >= period)) {
// pulse !
relayOn();
delay (200);
relayOff();
lastActivation = millis();
}
}