Last active
September 27, 2016 08:35
-
-
Save ar1a/b4d0ca912ea88f0dc1420a1c499017f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo tilt; | |
Servo pan; | |
/* | |
* tilt_pin and pan_pin must be PWM pins! | |
* adjust values to your liking | |
* uses standard hjkl for movement | |
* "r" resets it to the middle | |
*/ | |
const int move_amt = 5; | |
const int tilt_pin = 3; | |
const int tilt_middle = 123; | |
const int tilt_low = 30; | |
const int tilt_high = 180; | |
const int pan_pin = 5; | |
const int pan_middle = 80; | |
const int pan_low = 0; | |
const int pan_high = 165; | |
int pan_current = 0; | |
int tilt_current = 0; | |
void tilt_write(int rot) | |
{ | |
if(tilt_current + rot < tilt_low || tilt_current + rot > tilt_high) return; | |
tilt_current += rot; | |
tilt.write(tilt_current); | |
} | |
void pan_write(int rot) | |
{ | |
if(pan_current + rot < pan_low || pan_current + rot > pan_high) return; | |
pan_current += rot; | |
pan.write(pan_current); | |
} | |
void setup() { | |
Serial.begin(9600); | |
// put your setup code here, to run once: | |
tilt.attach(tilt_pin); | |
tilt.write(tilt_middle); | |
tilt_current = tilt_middle; | |
pan.attach(pan_pin); | |
pan.write(pan_middle); | |
pan_current = pan_middle; | |
} | |
void loop() { | |
while(Serial.available() == 0); | |
switch(Serial.read()) { | |
case 'r': | |
setup(); | |
break; | |
case 'h': | |
pan_write(move_amt); | |
break; | |
case 'j': | |
tilt_write(-move_amt); | |
break; | |
case 'k': | |
tilt_write(move_amt); | |
break; | |
case 'l': | |
pan_write(-move_amt); | |
break; | |
} | |
delay(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment