How to stop repeated motion from a servo

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)

The code I have written is as follows:

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?

Thank you for your help.

Regards

In the IDE click on Edit then Copy For Forum, that will copy your code for pasting on this forum. So just come back here and do a simple paste.

Thank you, here it is:

#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.");}
}

Much bettor now we can copy your code and try to run it.

Thank you for your assistance Jim, it is much appreciated.

So if you could somehow stop the program after this line:

Serial.println("Motion detected! Relay is now locked ON.");

that would do what you want. Do you agree?

Yes exactly

Have you ever seen this before:

while (true) { }

It will do nothing forever, so basically stops your program at that line of code.

myServo.write(0); // Rotate back to starting position (0 degrees)

Or even the servo should stop after here a single cycle to this pont

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?

Like I said the program will stop at that line. So where do you think it should be?

Let me try it out

Good way to learn

Thanks again for your help, Jim. Much appreciated

How are you powering the Uno?
I ask because powering two servos from the 5V output could be a problem if the current is too large.

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.

I will probably add an additional 5V battery through a relay for the servos and just control the relay with the UNO.

OK, will do that.

You don't need a relay. Just use the 5V battery in place of the 5V output from the Uno.