// Use this app to find the real limits of the servo motor 0º and 180º.
// Then, use them in the attach() command for this motor.
// Make sure no weird sounds come out ot the servo, i.e. the hardware endstops must not be reached.
// https://www.woolseyworkshop.com/2023/02/15/controlling-a-servo-motor-with-an-arduino/
// https://www.hackster.io/jeremy-lindsay/calibrating-my-servos-fa27ce
// Original comments:
// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a
// to decrease) and enter to change servo position
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;
void setup()
{
myservo.attach(9, 400, 2600); //servo control pin, and range if desired
Serial.begin(9600);
Serial.println("Find the extremes for attach() with commands like: 544x for angle 0 and 2400x for angle 180.");
Serial.println();
Serial.println("serial servo incremental test code");
Serial.println("type a character (+ to increase or - to decrease)");
Serial.println("and enter to change servo position");
Serial.println("use strings like 90x or 1500x for new servo position");
Serial.println();
}
void loop()
{
// No respeta lo que dice:
// https://forum.arduino.cc/t/serial-input-basics-updated/382007/2
// porque este frena todo hasta que se termina de leer todo el puerto serie, pero para algo tan simple no creo que importe.
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if(c == '\r' || c == '\n') {}
else {
readString += c; //makes the string readString
}
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
if(readString.indexOf('x') >0) {
pos = readString.toInt();
}
if(readString =="-"){
(pos=pos-10); //use larger numbers for larger increments
if(pos<0) (pos=0); //prevent negative number
}
if (readString =="+"){
(pos=pos+10);
}
if(pos >= 400) //determine servo write method
{
Serial.println(pos);
myservo.writeMicroseconds(pos);
}
else
{
Serial.println(pos);
myservo.write(pos);
}
}
readString=""; //empty for next input
}