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
}
}