Mouse trap programming help

I have experience with PLC controllers using ladder-logic software/programming. This is my first solo project with Arduino. The C++ programming is confusing me a bit. The plan is to build a live trap to catch mice. The basic logic is 2 one way gates using servos and PIR motion sensors.

Board is a Arduino Nano
Inlet gate = servo #1
Outlet gate = servo #2
PIR motion sensor #1 is just inside the inlet gate
PIR motion sensor #2 is just after the outlet gate.

Default mode should be Inlet gate 0*/open and Outlet gate 90*/closed.

Scenario #1 - If a mouse enters the tunnel far enough to trip PIR#1, the inlet servo moves to 90*/close and the Outlet servo moves to 90*/open.

Scenario #2 - The mouse moves toward the outlet and trip PIR#2, the Outlet servo moves to 0*/close, and the Inlet servo moves to 0*/open.

Scenario #3 - The mouse has moved far enough down the only open tunnel, the Outlet into the containment box, that both PIR's no longer sense motion. Inlet gate 0*/open and Outlet gate 90*/closed

Scenario #4 - There is mice tripping PIR#1 and PIR #2, MUTINY!!! Inlet gate 90*/closed and Outlet gate 0*/open, and Inlet gate "shakes", cycling from 85* to 90* and back, at X-ms intervals (need help with this part the most)

This is the code I have so far. The IDE verifies it's clean, of course that doesn't mean it'll work.

#include <Servo.h>
// This code is intended for a Arduino Nano
// Servo #1 = Inlet servo = Digital Pin #9 - pos 0 = open, pos 90 = closed
// Servo #2 = Outlet servo = Digital Pin #10 - pos 0 = closed, pos 90 = open
// Servo Gnd and Vcc connected in parallel to Arduino Nano Gnd and VIN
// PIR Input #1 = Inlet Motion = Digital Pin#2
// PIR Input #2 = Outlet Motion = Digital Pin #3
// PIR Gnd and Vcc connected to Arduino Nanco Gnd and +5V ports.

Servo servoin;  // create servo object to control inlet servo
Servo servoout; // create servo object to control inlet servo
int pos = 0;    // variable to store the servo position
int pir1Pin = 2;  //the digital pin connected to the PIR1 sensor's output
int pir2Pin = 3;  //the digital pin connected to the PIR2 sensor's output 
int calibrationTime = 30; //amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)
 
void setup() {  
  Serial.begin(9600);  
  pinMode(pir1Pin, INPUT); 
  pinMode(pir2Pin, INPUT);   
  servoin.attach(9);  // Set intake servo to digital pin9
  servoout.attach(10);  // Set outlet servo to digital pin 10
  Serial.println("calibrating sensor "); //give the sensor time to calibrate
  for(int i = 0; i < calibrationTime; i++){
    Serial.print(calibrationTime - i);
    Serial.print("-");
    delay(1000);
  }
  Serial.println();
  Serial.println("done");
  delay(1000);  // additional delay timer to allow PIR to "settle"
  Serial.println("Primed");
 }  
   
 void loop() {  

// Scenario #1: Inlet Motion detected + Outlet Motion Not detected > ServoIn to 90 "closed" > Servoout to 90 "open"
    
  if(digitalRead(pir1Pin) == HIGH && digitalRead(pir2Pin) == LOW)  
  {  
    Serial.print("Inlet Motion Detected");  
    Serial.print('\n');  
    servoin.write(90);               // tell servoin to go to position 90 "closed"
    servoout.write(90);              // tell servoout to go to position 90 "open"
    delay(30);                      // waits 30ms for the servo to reach the position 
  }


// Scenario #2: Inlet Motion Not detected + Outlet Motion detected> ServoOut to 0 "closed" > ServoIn to 0 "open"
  
  if(digitalRead(pir1Pin) == LOW && digitalRead(pir2Pin) == HIGH)  
  {  
    Serial.print("Outlet Motion Detected");  
    Serial.print('\n');  
    servoout.write(0);             // tell servoout to go to position 0 "closed"
    servoin.write(0);              // tell servoin to go to position 0 "open"
    delay(30);                       // waits 30ms for the servo to reach the position
  }

// Scenario #3: Inlet Motion Not detected + Outlet Motion Not detected> ServoOut to 0 "closed" > ServoIn to 0 "open"
  
  if(digitalRead(pir1Pin) == LOW && digitalRead(pir2Pin) == LOW)  
  {  
    Serial.print("Primed");  
    Serial.print('\n');  
    servoout.write(0);             // tell servoout to go to position 0 "closed"
    servoin.write(0);              // tell servoin to go to position 0 "open"
    delay(30);                       // waits 30ms for the servo to reach the position
  }

// Scenario #4: Inlet Motion detected + Outlet Motion detected> ServoOut to 0 "closed" > ServoIn to 90 "closed"
  
  if(digitalRead(pir1Pin) == HIGH && digitalRead(pir2Pin) == HIGH)  
  {  
    Serial.print("MUTINY!!!");  
    Serial.print('\n');  
    servoout.write(0);             // tell servoout to go to position 0 "closed"
    servoin.write(90);              // tell servoin to go to position 90 "closed"
    delay(30);                       // waits 30ms for the servo to reach the position
  }
 }

I think you need something like this spiky thing to keep the mice from returning to the outlet gate.

Also, you are officially admonished to use code tags </> and the ctrl+t function of the editor when posting code. :wink:

There is a sticky post at the start of the forum on how to use the forum - includes help on posting code so others can easily read it and test it if required

I also used to to PLC's and ladder logic - what I could do with one rung and two instructions sometimes take 8 or 10 ++ lines of code and declarations here - frustrating for me

I programmed in C many years ago but it was not real time - but moving from C to Ladder was easy - moving back for real time stuff is not so easy

you have some of the good basics on the forum already - such as a clear statement of what you want to do and the posting of the code that you have so far - (yes the formatting needs to be different) - people that try to help themselves get better help than someone that shows up and wants the code written for them

you want to make it easy for others to help you

Good luck and welcome to the forum

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Tom.. :slight_smile:

apologies on putting the code in plain text.

Corrected

I plan on putting PIR#1 after a 90* elbow from the Inlet gate, so the motion sensor can't pick it up.

I'll have to figure out something to keep them from trying to rush the Outlet gate. I like that spike plate.

Thanks

Hi,
This is like what we used during many mouse plagues.
bucket-mouse-trap-with-dowel-peanut-butterr.jpg
Ours was not as technical as using a can and wire.
Just a bottle poked out over the edge of a bench, with lard and a bit of cheese or peanut butter in the end of the bottle to entice them out.

Tom... :slight_smile:

bucket-mouse-trap-with-dowel-peanut-butterr.jpg

saildude:
I also used to to PLC's and ladder logic - what I could do with one rung and two instructions sometimes take 8 or 10 ++ lines of code and declarations here - frustrating for me

I feel your pain. The good old days.

A-B timer.PNG

A-B timer.PNG

@dougp - yes one of the examples I was thinking of - and -|/|-the Done Bit turns it into a free running timer - or the single button with a one shot and with two lines of code if my memory is correct - press the button and toggle an output push to turn an output on - press again to turn it off -

Ladder is very powerful - loved the Allen Bradley 5 series - - online edits with the equipment running - one had to be careful but not so disruptive to the plant floor - but a lot of responsibility

thanks