Hi everyone
I am a complete noob to this so be kind. I have put together a circuit and written a piece of code for a new kind of live trap for trapping a specific kind of protected species for translocation and conservation efforts.
Here is the diagram (please note the power supply is in lieu of a 12V battery)
It all seems to work exactly as I need to which is as follows:
On motion detection the servos turn 90 degrees to allow the doors of the trap at either end to close and then torn back to the 0 degree position to lock the doors in place. A light switches on (and stays on) to indicate to researchers that the trap has been triggered.
My problem is that if the animal moves past the beam aagain the servers move again thus unlocking the doors and (although unlikely) allowing the possibility thgat the trapped animal can push open the doors and escape.
Is there a way to stop the servo motion after they have completed one full cycle, regardless of whether motion is detected again?
#include <Servo.h>
Servo myServo;
int pos = 0;
const int relayPin = 8;
const int pirPin = 5;
const int servoPin = 9;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(servoPin, OUTPUT);
pinMode(pirPin, INPUT);
myServo.attach(servoPin); // Attach the servo to the pin
myServo.write(0); // Ensure the servo starts at 0 degrees
digitalWrite(relayPin, HIGH);
digitalWrite(servoPin, HIGH);
}
void loop() {
int motion = digitalRead(pirPin); // Check for movement
if (motion == HIGH) {
myServo.write(90); // Rotate to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(0); // Rotate back to starting position (0 degrees)
digitalWrite(relayPin, LOW);
Serial.println("Motion detected! Relay is now locked ON.");}
}
I have not seen that before. Like I said this is my very first attempt at this and I have zero coding experience of any kind. Thank you Jim, I will try this. Where should this line be added?
Hi Jim, I am powering it through the jack with a 12V battery. If the servos are an issue when I test it, I will just run them through a relay as well.
By the way, this code works perfectly, thank you:
#include <Servo.h>
Servo myServo;
int pos = 0;
const int relayPin = 8;
const int pirPin = 5;
const int servoPin = 9;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(servoPin, OUTPUT);
pinMode(pirPin, INPUT);
myServo.attach(servoPin); // Attach the servo to the pin
myServo.write(0); // Ensure the servo starts at 0 degrees
digitalWrite(relayPin, LOW);
digitalWrite(servoPin, HIGH);
}
void loop() {
int motion = digitalRead(pirPin); // Check for movement
if (motion == HIGH) {
myServo.write(90); // Rotate to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(0); // Rotate back to starting position (0 degrees)
digitalWrite(relayPin, HIGH);while (true) { }
Serial.println("Motion detected! Relay is now locked ON.");}
}
If you don't want the Serial.println to execute then that is fine but I would have put it after the Serial.println
digitalWrite(relayPin, LOW);
Serial.println("Motion detected! Relay is now locked ON.");
while (true) { }
}
If you want this to work every time and all the time then you should not power the servos from the Uno 5V pin. It may work 5 times or even 100 times but it will eventually fail because the servos use too much current.
You will need a buck converter to convert the 12V to 5V for the servos.