-
Notifications
You must be signed in to change notification settings - Fork 0
/
Servo_Testing_Sketch.ino
66 lines (57 loc) · 1.96 KB
/
Servo_Testing_Sketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
String readString; //String captured from serial port
Servo myservo; // create servo object to control a servo
int n; //value to write to servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7, 500, 2500); //the pin for the servo control, and range if desired
Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
Serial.println();
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
// attach or detach servo if desired
if (readString == "d") {
myservo.detach(); //detach servo
Serial.println("servo detached");
goto bailout; //jump over writing to servo
}
if (readString == "a") {
myservo.attach(7); //reattach servo to pin 7
Serial.println("servo attached");
goto bailout;
}
n = readString.toInt(); //convert readString into a number
// auto select appropriate value
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
bailout: //reenter code loop
Serial.print("Last servo command position: ");
Serial.println(myservo.read());
Serial.println();
readString=""; //empty for next input
}
}