Devices & Components
Arduino Uno Rev3
SparkFun XBee Explorer USB
SparkFun XBee Shield
Xbee Pro S1
Corona DS238 MG Digital Servos
Hardware & Tools
Soldering iron (generic)
3D Printer (generic)
Software & Tools
Arduino IDE
Project description
Code
Arduino Servo Control from XBee serial receive
arduino
Flashed to Arduino Uno
1 2#include <Servo.h> 3Servo yawservo; // create servo object to control a servo 4Servo pitchservo; // create servo object to control a servo 5 6String inputString = ""; // a string to hold incoming data 7boolean yawComplete = false; // whether the string is complete 8boolean pitchComplete = false; // whether the string is complete 9 10void setup() { 11 // initialize serial: 12 Serial.begin(9600); // reserve 200 bytes for the inputString: 13 inputString.reserve(200); 14 yawservo.attach(9); // attaches the servo on pin 9 to the servo object 15 pitchservo.attach(10); // attaches the servo on pin 9 to the servo object 16} 17 18void loop() { 19 serialEvent(); //call the function // print the string when a newline arrives: 20 if (pitchComplete) { 21 pitchservo.write(inputString.toInt()); // sets the servo position according to the scaled value 22 delay(1); 23 inputString = ""; 24 pitchComplete = false; 25 } 26 27 if (yawComplete) { 28 yawservo.write(inputString.toInt()); // sets the servo position according to the scaled value 29 delay(1); 30 inputString = ""; 31 yawComplete = false; 32 } 33} 34 35void serialEvent() { 36 while (Serial.available()) { 37 // get the new byte: 38 char inChar = (char)Serial.read(); 39 if (inChar == 'x') 40 { 41 pitchComplete = true; 42 break; 43 } 44 if (inChar == 'y') 45 { 46 yawComplete = true; 47 break; 48 } 49 inputString += inChar; 50 } 51} 52 53 54
Comments
Only logged in users can leave comments