Skip to content
Browse files

Add Arduino sketch for serial pan/tilt

  • Loading branch information...
1 parent f02ccf7 commit 65c7702f0735a2435c3e5319affdf4f2fb99e0c9 @jeremycole committed Jan 30, 2012
Showing with 58 additions and 0 deletions.
  1. +58 −0 arduino/serial_pan_tilt/serial_pan_tilt.ino
View
58 arduino/serial_pan_tilt/serial_pan_tilt.ino
@@ -0,0 +1,58 @@
+#include <Servo.h>
+
+Servo pan, tilt;
+
+String inputString = ""; // a string to hold incoming data
+boolean stringComplete = false; // whether the string is complete
+
+void setup()
+{
+ Serial.begin(9600);
+ inputString.reserve(200);
+ pan.attach(9);
+ pan.write(90);
+ tilt.attach(10);
+ tilt.write(90);
+}
+
+void loop()
+{
+ if(stringComplete)
+ {
+ if(inputString.substring(0,1) == "H")
+ {
+ pan.write(90);
+ tilt.write(90);
+ Serial.println("Going home");
+ }
+ if(inputString.substring(0,1) == "P")
+ {
+ char pan_value = inputString.substring(1).toInt();
+ pan.write(pan_value);
+ Serial.println("Moving pan");
+ }
+ if(inputString.substring(0,1) == "T")
+ {
+ char tilt_value = inputString.substring(1).toInt();
+ tilt.write(tilt_value);
+ Serial.println("Moving tilt");
+ }
+
+ inputString = "";
+ stringComplete = false;
+ }
+}
+
+void serialEvent() {
+ while (Serial.available()) {
+ // get the new byte:
+ char inChar = (char)Serial.read();
+ // add it to the inputString:
+ inputString += inChar;
+ // if the incoming character is a newline, set a flag
+ // so the main loop can do something about it:
+ if (inChar == '\n') {
+ stringComplete = true;
+ }
+ }
+}

0 comments on commit 65c7702

Please sign in to comment.
Something went wrong with that request. Please try again.