In 1984, Dave Vernier wrote the book How to Build a Better Mousetrap and 13 other Apple II Projects. The title project of this book was a somewhat whimsical mousetrap that used a photogate placed in a box to detect a mouse and then used a stepper motor to lower the door on the box to catch the mouse. Even though the stepper motor was fairly slow at lowering the door, the trap worked well to catch quite a few mice (or perhaps to catch one mouse many times).
This sketch is for the Arduino version. The stepper motor is attached to the DCU. A pulley on the stepper motor has string wrapped around it. This string goes over a pulley and holding up a vertical sliding door.Â
/*
VernierMousetrap (v 2017.07)
A Vernier Photogate should also be connnected to the second BTD 1 connector.
The sketch monitors this photogate and Controls a stepper motor (unipolar or
bipolar) via a Vernier Digital Control Unit (DCU) connected to the BTD 2
connector.
This sketch will continuously check the photogate and start the stepper
motor rotating when the photogate is blocked. External power must be supplied
to the DCU to drive the stepper motor.
This version uses the VernierLib library
See www.vernier.com/arduino for more information.
*/
#include "VernierLib.h"
VernierLib Vernier;
int photogateStatus = HIGH;
int stepDelay = 30; //step time in ms
int stepCount = 55; //number of stepCount to take
int stepDirection = 0; //stepDirection 0 =CW
int photogate = 2; //This is the input for a photogate on the BTD 1 connector
int LEDpin = 13; /// line for LED to turn on when photogate is blocked
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
}// end of setup
void loop ()
{
Serial.println(" starting loop, waiting for mouse... ");
photogateStatus = digitalRead(photogate);//low when blocked
if (photogateStatus == LOW)
{
digitalWrite(LEDpin, HIGH);// turn on LED
Serial.print(photogateStatus);
Serial.println(" Blocked ");
Vernier.DCUstep(stepCount,stepDirection, stepDelay);// use stepper motor to lower door
Vernier.DCU(0);//Turn off all lines
delay (10000); //wait 10 seconds, note that the Arduino/shield LED will stay on
digitalWrite(LEDpin, LOW);// turn off Arduino/shield LED when ready to trap again
}
else
{
Vernier.DCU(0);//Turn off all lines
digitalWrite(LEDpin, LOW);// turn off LED
}
} ;// end of loop