Skip to content

Instantly share code, notes, and snippets.

@rfair404
Last active August 29, 2015 13:57
Show Gist options
  • Save rfair404/9613888 to your computer and use it in GitHub Desktop.
Save rfair404/9613888 to your computer and use it in GitHub Desktop.
My Leprechaun Trap Arduino Sketch
/*
* My Leprechaun capture scetch
* Mainly adopted from some existing public tutorials plus a little tinkering
* this code is public domain, enjoy!
* by Russell Fair
*/
#include <Servo.h>
Servo servo1;
Servo servo2;
int pingPin = 5;
int echoPin = 4;
int greenLight = 6;
int redLight = 12;
int yellowLight1 = 8;
int yellowLight2 = 2;
bool trapSet = false;
bool captured = false;
void setup(){
Serial.begin(9600);
servo1.attach(9);
servo2.attach(10);
pinMode(echoPin, INPUT);
pinMode(pingPin, OUTPUT);
pinMode(redLight, OUTPUT);
pinMode(greenLight, OUTPUT);
pinMode(yellowLight1, OUTPUT);
pinMode(yellowLight2, OUTPUT);
setTrap();
}
void loop(){
int distanceCM;
if (trapSet && !captured){
distanceCM = getDistance();
Serial.println(distanceCM);
if(distanceCM < 11){
capture();
} else {
shimmerLights();
}
} else {
digitalWrite(redLight, HIGH);
delay(100);
digitalWrite(redLight, LOW);
delay(10);
}
delay(1000);
}
float getDistance(){
float distanceCM;
int pulseDuration;
//get distance
pulseDuration = getPulseDuration();
//convert to CM
distanceCM = getDistanceCM(pulseDuration);
return distanceCM;
}
void switchLED(int pin, int position = 2){
switch (position){
case 0:
digitalWrite(pin, LOW);
break;
case 1:
digitalWrite(pin, HIGH);
break;
default:
digitalWrite(pin, !digitalRead(pin));
break;
}
}
void shimmerLights(){
int level1 = rand() % 255;
int level2 = rand() % 255;
int delayTime = rand() % 1000+20;
int which = rand() % 2+1;
delay(delayTime);
switch (which){
case 1:
switchLED(yellowLight1);
break;
case 2:
switchLED(yellowLight2);
break;
}
}
int getPulseDuration(){
delay(100);
int pulseDuration;
//initialize the sensor output
digitalWrite(pingPin, LOW);
delayMicroseconds(20);
digitalWrite(pingPin, HIGH);
delayMicroseconds(100);
digitalWrite(pingPin, LOW);
//gets the raw pulse duration
pulseDuration = pulseIn(echoPin, HIGH);
return pulseDuration;
}
float getDistanceCM(int pulseDuration){
float distanceCM;
float sos = 29.387;
//divide by 2 because it's a round trip
distanceCM = pulseDuration / sos / 2;
return distanceCM;
}
void setTrap(){
if(!trapSet){
servo1.write(122);
delay(1000);
servo2.write(100);
Serial.println("Setting the trap now");
trapSet = true;
for(int x = 0; x < 4; X++){
digitalWrite(greenLight, HIGH);
delay(100);
digitalWrite(greenLight, LOW);
delay(50);
}
}
}
void capture(){
if(trapSet && !captured){
Serial.println("Gotcha you sneaky Leprechaun");
flashFast();
servo2.write(30);
delay(200);
servo1.write(30);
digitalWrite(greenLight, LOW);
captured = true;
}
}
void flashFast(){
for (int x = 0; x < 3; x++){
digitalWrite(yellowLight1, LOW);
digitalWrite(yellowLight2, LOW);
digitalWrite(greenLight, LOW);
delay(100);
digitalWrite(yellowLight1, HIGH);
digitalWrite(yellowLight2, HIGH);
digitalWrite(greenLight, HIGH);
delay(75);
}
digitalWrite(yellowLight1, LOW);
digitalWrite(yellowLight2, LOW);
digitalWrite(greenLight, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment