Like anyone who owns cats I am tired of them being in the kitchen and eating things they're not supposed to be eating. So I made a motion activated spray bottle with a servo, a PIR sensor, Elegoo Uno knock off, and a mini breadboard. Nothing fancy just spraying when its activated. I have it plugged in with an AC adapter that I had lying around and it works great. My cats have definitely been avoiding the kitchen more with it in there but, it does not stay active for very long. I set it up tested it with myself for range and sensitivity and it covers the area it needs to cover. When I came back about 30-45 minutes later I was able to walk in with no activation. I checked wiring and nothing came loose so I hit the reset button and it started working again. I did more tests and it worked fine but then it happened again in about an hour and a half.(it worked at 30 minutes bc someone got curious) The time delay and range on the PIR are both set as low as they go. I tested the loop and activation with a println in the serial log so I didn't get more soaked and it seemed to be functioning fine. I'm not sure what I'm doing wrong or if its just my hardware being a knock off and everything is from amazon. I got most of the code help from https://arduinogetstarted.com/tutorials/arduino-motion-sensor
#include <Servo.h>
Servo spray;
const int PIN_TO_SENSOR = 2; // the pin that OUTPUT pin of sensor is connected to
int pinStateCurrent = LOW; // current state of pin
int pinStatePrevious = LOW; // previous state of pin
int restpos= 0;
int activepos= 75;
int delaybetween= 500; //delay between sprays
int delayreset= 800; //delay between active and rest pos
void setup() {
Serial.begin(9600); // initialize serial
pinMode(PIN_TO_SENSOR, INPUT);
spray.attach(3);
spray.write(restpos);
}
void loop() {
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(PIN_TO_SENSOR); // read new state
if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {
Serial.println("DETECTED!");
spray.write(restpos);
delay(delayreset);
spray.write(activepos);
delay(delaybetween);
spray.write(restpos);
delay(delayreset);
spray.write(activepos);
delay(delaybetween);
spray.write(restpos);
delay(delayreset);
spray.write(activepos);
delay(delaybetween);
spray.write(restpos);
}
else
if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
spray.write(restpos);
}
delay(2000);
}
TYIA


