For this week, our class chose to wreak havoc on our fellow Fab Lab students and cause them immense trouble and pain. We decided to create a spider motion detection system that would drop a spider on your head each time you walked by.
Individual Contribution
For this group project, I helped during the brainstorming phase by keeping our group grounded and ruling out ridiculous ideas (no, we were never going to make a fist bump machine). I also assisted with debugging the code and adjusting it so the spider would drop at the correct interval.
Hanna had written the original code, but during our lab session on Thursday, we discovered a bug that was throwing everything off. We were maybe a bit too ecstatic about finding it, but we fixed it.
The issue in the code was related to defining the direction for the spool to spin, which caused it to spin poorly or not at all. We also didn’t like how long the delay was between motion detection and the spider drop. While Hanna originally wrote the code, I took on an editor role—when she was away, I had enough understanding of the program to tweak and update it for the late-night crew working to get the project running at the last minute.
This was the original not-working code.
// Basic CNC shield stepper motor example.
#include "Ultrasonic.h"
/*
Pass as a parameter the trigger and echo pin, respectively,
or only the signal pin (for sensors 3 pins), like:
Ultrasonic ultrasonic(13);
*/
Ultrasonic ultrasonic(12, 13);
int distance;
int headHeight = 24; // in inches
// motor stuff
int enablePin = 8;
int stepPin = 2;
int dirPin = 5;
int dir = LOW;
int speed = 5000; // Steps per second
int dist = 6000; // Total number of steps to move (approx 6 feet)
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(enablePin,OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
// do the code below to ensure the motor isn't trying to spin before
// we meet the conditions for deploying the spider
digitalWrite(enablePin,HIGH);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW); // counterclockwise?
}
int increment = 0;
void loop() {
distance = ultrasonic.read(INC);
Serial.print("Distance in Inches: ");
Serial.println(distance);
// make an incrementor, increment goes up for each distance less than 24, if the increment reaches 5 (on i=5),
// deploy
if (distance < headHeight) {
increment++;
Serial.println(increment);
// on the 5th read less than 24 inches, start the motor running code
if (increment == 3) {
Serial.println("deploy spider");
digitalWrite(enablePin, LOW); // start motor
digitalWrite(dirPin, LOW);
for (int i = 0; i < dist; i++) {
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
}
delay(10000);
digitalWrite(dirPin, HIGH); // to tell arduino to go clockwise
for (int i = 0; i < dist; i++) {
digitalWrite(stepPin, LOW);
delayMicroseconds(500); // a bit slower when going up
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
}
digitalWrite(enablePin, HIGH); // disable motor after move
increment = 0;
delay(10000); // delay after spider has been deployed
}
} else{
Serial.println("dont deploy spider");
}
delay(5000);
}
The New Code
This was the code that was revised to reflect the changes we wanted to make:
Updated headheight to be a more accurate distance from the victim's head from the motion sensor's location up in the ceiling.
int headHeight = 72; // in inches
Increased int dist to be larger to represent the longer string we would need if the spider would need to fall from a higher height, which was needed because we raised the location of the spider as well as increased the distance of the motion detection.
int dist = 10000;
Increased delayMicroseconds to be larger so that the string would drop at a more reasonable pace.
// Basic CNC shield stepper motor example.
#include "Ultrasonic.h"
/*
Pass as a parameter the trigger and echo pin, respectively,
or only the signal pin (for sensors 3 pins), like:
Ultrasonic ultrasonic(13);
*/
Ultrasonic ultrasonic(12, 13);
int distance;
int headHeight = 72; // in inches
// motor stuff
int enablePin = 8;
int stepPin = 2;
int dirPin = 5;
int dir = LOW;
int speed = 5000; // Steps per second
int dist = 10000; // Total number of steps to move (approx 6 feet)
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(enablePin,OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
// do the code below to ensure the motor isn't trying to spin before
// we meet the conditions for deploying the spider
digitalWrite(enablePin,HIGH);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW); // counterclockwise?
}
int increment = 0;
void loop() {
distance = ultrasonic.read(INC);
Serial.print("Distance in Inches: ");
Serial.println(distance);
// make an incrementor, increment goes up for each distance less than 24, if the increment reaches 5 (on i=5),
// deploy
if (distance < headHeight) {
increment++;
Serial.println(increment);
// on the 5th read less than 24 inches, start the motor running code
if (increment == 3) {
Serial.println("deploy spider");
digitalWrite(enablePin, LOW); // start motor
digitalWrite(dirPin, LOW);
for (int i = 0; i < dist; i++) {
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
digitalWrite(stepPin, HIGH);
delayMicroseconds(200);
}
delay(10000);
digitalWrite(dirPin, HIGH); // to tell arduino to go clockwise
for (int i = 0; i < dist; i++) {
digitalWrite(stepPin, LOW);
delayMicroseconds(500); // a bit slower when going up
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
}
digitalWrite(enablePin, HIGH); // disable motor after move
increment = 0;
delay(10000); // delay after spider has been deployed
}
} else{
Serial.println("dont deploy spider");
}
delay(5000);
}