Arduino nano, Motion sensor and Relay with transformerless power supply
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
564 B

const int lightDelay = 90000;
const byte movePin = 3;
const byte relayPin = 4;
long lastmoveMillis = 0;
void setup() {
pinMode(movePin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
}
void loop(){
if (digitalRead(movePin) == 1) lastmoveMillis = millis();
delay(100);
if (lastmoveMillis > 0) activateRelay();
}
void activateRelay() {
if ((millis() - lastmoveMillis) < lightDelay) digitalWrite(relayPin, LOW);
if ((millis() - lastmoveMillis) > lightDelay) {
digitalWrite(relayPin, HIGH);
lastmoveMillis = 0;
}
}