Created
October 27, 2017 21:16
-
-
Save antonxy/24f88c10cebea90f651efb63553a4d3f to your computer and use it in GitHub Desktop.
Arduino sketch to test servos or brushless motor controllers
This file contains hidden or 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
| /* Sweep | |
| by BARRAGAN <http://barraganstudio.com> | |
| This example code is in the public domain. | |
| modified 8 Nov 2013 | |
| by Scott Fitzgerald | |
| http://www.arduino.cc/en/Tutorial/Sweep | |
| */ | |
| #include <Servo.h> | |
| Servo myservo; // create servo object to control a servo | |
| // twelve servo objects can be created on most boards | |
| int pos = 0; // variable to store the servo position | |
| // 12 is the maximum length of a decimal representation of a 32-bit integer, | |
| // including space for a leading minus sign and terminating null byte | |
| byte intBuffer[12]; | |
| String intData = ""; | |
| int delimiter = (int) '\n'; | |
| void setup() { | |
| myservo.attach(9); // attaches the servo on pin 9 to the servo object | |
| Serial.begin(9600); | |
| Serial.println("Hello"); | |
| } | |
| void loop() { | |
| while (Serial.available()) { | |
| int ch = Serial.read(); | |
| Serial.println(ch); | |
| if (ch == -1) { | |
| // Handle error | |
| } | |
| else if (ch == delimiter) { | |
| // Copy read data into a char array for use by atoi | |
| // Include room for the null terminator | |
| int intLength = intData.length() + 1; | |
| intData.toCharArray(intBuffer, intLength); | |
| // Reinitialize intData for use next time around the loop | |
| intData = ""; | |
| // Convert ASCII-encoded integer to an int | |
| pos = atoi(intBuffer); | |
| Serial.print("Got: "); | |
| Serial.println(pos); | |
| break; | |
| } | |
| else { | |
| intData += (char) ch; | |
| } | |
| } | |
| myservo.writeMicroseconds(pos); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment