I am trying to setup a fun Halloween project building a coffin with a hinged lid and sound using relays to control motors and mp3 board to activate sound using NANO every board(thought was a classic but it installed every when I plugged it in)
Starting off with just using PIR to activate relay(using leds as test) I program what I believe to be pir activates relay for specific time then waits to reactive that relay for another predetermined time using mills.,
I am having different results using different logic but posted is my basic. What happens is I can continuously trip the sensor causing led to light every time like it isn't using a delayed reactivation time.
#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;
void setup() {
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) {
motionDetected = true;
}
if (motionDetected) {
digitalWrite(RELAY1_PIN, HIGH);
if (currentMillis - previousMillis >= ACTIVATION_TIME) {
digitalWrite(RELAY1_PIN, LOW);
previousMillis = currentMillis;
motionDetected = false;
}
}
if (currentMillis - previousMillis >= WAIT_TIME) {
previousMillis = currentMillis;
}
}
My ultimate goal is to use the Nano, 2 relays, PIR sensor, mp3 playback. If sensor sees movement relay 1 (HIGH)activates for 30 seconds, mp3 (LOW) for 2 seconds, if sensor still detects motion after 35 seconds it actives relay 2 and mp3 2 for same intervals
Then waits till time of 10 minutes has passed to activate sensor again.