Skip to content

Instantly share code, notes, and snippets.

@zoutepopcorn
Last active September 10, 2019 13:07
Show Gist options
  • Save zoutepopcorn/789ac9d36832ca3809c4546de6a08cc5 to your computer and use it in GitHub Desktop.
Save zoutepopcorn/789ac9d36832ca3809c4546de6a08cc5 to your computer and use it in GitHub Desktop.
servo arduino
/* Servo - Sweep
* by BARRAGAN <http://barraganstudio.com>
* This example code is in the public domain.
*
* modified 28 May 2015 by Michael C. Miller
* modified 8 Nov 2013 by Scott Fitzgerald
*
* http://arduino.cc/en/Tutorial/Sweep
* https://github.com/esp8266/Arduino/blob/master/libraries/Servo/examples/Sweep/Sweep.ino
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup()
{
myservo.attach(2); // attaches the servo on GPIO2 to the servo object
}
void loop()
{
int pos;
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
#include <Servo.h>
Servo myservo;
String readString, servo1, servo2;
void setup() {
myservo.attach(2);
Serial.begin(115200);
Serial.println("read line "); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(3);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}
}
if (readString.length() >0) {
Serial.println(readString);
String command = readString.substring(0, 2);
servo1 = readString.substring(2, 5);
int n1 = servo1.toInt();
Serial.print("command : "); Serial.println(command);
Serial.print("input --");Serial.print(n1);Serial.println("--");
if(n1 <= 180 && n1 >= 0) {
Serial.println("COMMAND OK");
myservo.writeMicroseconds(n1);
} else {
Serial.println(" > command : 'AABBB' ");
Serial.println(" > AA \t -> Servo nr ");
Serial.println(" > BBBB -> Servo nr ");
Serial.println(" > range is 0 - 180 ");
}
readString="";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment