Arduino - Controlling A Servo Motor Through Serial Input
Arduino Servo Motor Schematic
Arduino Servo Motor Code
[sourcecode language="c"]
#include <Servo.h>
Servo s;
char serialIn[3];
int pos = 0;
void setup(){
Serial.begin(9600);
s.attach(9);
s.write(pos);
}
void loop() {
while(Serial.available() > 0) {
if(Serial.read() == 'R') {
for (int i = 0; i < 3; serialIn[i++] = Serial.read());
pos = atoi(serialIn);
if(pos >= 0 && pos < 181) {
Serial.println(pos);
s.write(pos);
} else {
Serial.println('Value beyond servo range');
}
}
}
}
[/sourcecode]
No comments:
Post a Comment