#include <Arduino.h>
#define PIR_SENSOR_PIN 2
#define RELAY1_PIN 4
// Time in milliseconds
#define ACTIVATION_TIME 2000
#define WAIT_TIME 120000
unsigned long previousMillis = 0;
bool motionDetected = false;
byte effectState = 0;
void setup() {
Serial.begin(115200);
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(RELAY1_PIN, OUTPUT);
digitalWrite(RELAY1_PIN, LOW);
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(PIR_SENSOR_PIN) == HIGH) {
if (!motionDetected) {
motionDetected = true;
effectState = 0;
} else
motionDetected = true;
}
if (motionDetected) {
switch (effectState) {
case 0: digitalWrite(RELAY1_PIN, HIGH);
previousMillis = millis();
effectState++;
break;
case 1: if (millis() - previousMillis >= ACTIVATION_TIME) {
previousMillis = millis();
effectState++;
digitalWrite(RELAY1_PIN, LOW);
}
break;
case 2: if (millis() - previousMillis >= WAIT_TIME) {
previousMillis = millis();
effectState++;
motionDetected = false;
}
break;
}
}
}