motionsensorrelay/motionsensorrelay.ino

25 行
564 B
Arduino
Raw パーマリンク 通常表示 履歴

2019-10-25 14:32:22 +00:00
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;
}
}