Skip to content

Instantly share code, notes, and snippets.

@adamgreig
Created June 18, 2010 16:19
Show Gist options
  • Save adamgreig/443851 to your computer and use it in GitHub Desktop.
Save adamgreig/443851 to your computer and use it in GitHub Desktop.
/*
* NewSerialServo
* --------------
* Servo control from the Serial port
*
* ####
* Modified by Adam Greig, Jun 2010, to just move the servo
* to one position or the other on '.' or ',' input.
* ####
*
* Alteration of the control interface to use < and > keys
* to slew the servo horn left and right. Works best with
* the Linux/Mac terminal "screen" program.
*
* Created 10 December 2007
* copyleft 2007 Brian D. Wendt
* http://principialabs.com/
*
* Adapted from code by Tom Igoe
* http://itp.nyu.edu/physcomp/Labs/Servo
*/
/** Adjust these values for your servo and setup, if necessary **/
int servoPin = 2; // control pin for servo motor
int minPulse = 600; // minimum servo position
int maxPulse = 2400; // maximum servo position
int turnRate = 100; // servo turn rate increment (larger value, faster rate)
int refreshTime = 20; // time (ms) between pulses (50Hz)
/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
int moveServo; // raw user input
long lastPulse = 0; // recorded time (ms) of the last pulse
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
//centerServo = maxPulse - ((maxPulse - minPulse)/2);
centerServo = 1700;
pulseWidth = centerServo; // Give the servo a starting point (or it floats)
Serial.begin(9600);
//Serial.println(" Arduino Serial Servo Control");
//Serial.println("Press < or > to move, spacebar to center");
//Serial.println();
}
void loop() {
// wait for serial input
if (Serial.available() > 0) {
// read the incoming byte:
moveServo = Serial.read();
// ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)
if (moveServo == 44) { pulseWidth = 1700; }
if (moveServo == 46) { pulseWidth = 1900; }
//if (moveServo == 32) { pulseWidth = centerServo; }
// stop servo pulse at min and max
if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
if (pulseWidth < minPulse) { pulseWidth = minPulse; }
// print pulseWidth back to the Serial Monitor (uncomment to debug)
// Serial.print("Pulse Width: ");
// Serial.print(pulseWidth);
// Serial.println("us"); // microseconds
}
// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo's position if unchanged, or move it if changed
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
}
videodevice /dev/video0
locate on
webcam_port 8000
webcam_localhost on
snapshot_interval 1
snapshot_filename snapshot
ffmpeg_cap_new on
gap 2
on_event_start /usr/bin/python /home/adam/motion/shoot.py
on_event_end /usr/bin/python /home/adam/motion/stop.py
minimum_motion_frames 3
output_normal off
lightswitch 30
noise_tune off
noise_level 40
threshold_tune off
threshold 2000
height 640
width 480
# play a random start-up sound with mplayer, and then tell the arduino
# to fire the nerf turret for one second (~6 shots)
import serial, time, random, os
ser = serial.Serial('/dev/ttyUSB0', 9600)
filename = random.choice(os.listdir("/home/adam/motion/start/"))
os.system("mplayer /home/adam/motion/start/" + filename + " &")
time.sleep(1)
ser.write(".")
time.sleep(1)
ser.write(",")
ser.close()
# play a random shutting-down sound with mplayer
import random, os
filename = random.choice(os.listdir("/home/adam/motion/stop/"))
os.system("mplayer /home/adam/motion/stop/" + filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment