Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Arduino sketch for serial pan/tilt
- Loading branch information
1 parent
f02ccf7
commit 65c7702
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |