Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calibrate example for Servo #49

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/Calibrate/Calibrate.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int input; // variable to read the value from the SerialMonitor
per1234 marked this conversation as resolved.
Show resolved Hide resolved
int servoPin = 11; // set the Pin on Arduino, servo is attached
per1234 marked this conversation as resolved.
Show resolved Hide resolved


void setup() {
myservo.attach(servoPin); // attaches the servo on servoPin to the servo object
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
input = Serial.parseInt(); // reads the value from Serial
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the line ending menu of Serial Monitor set to anything other than "No line ending", there is an issue. For example, if I enter 42 in the Serial Monitor input field, I get this output:

Servo set to 42 degree
Servo set to 0 degree

"Newline" is the default setting of this menu, so it's likely many users will encounter this problem.

The easiest solution would be to document the required line ending setting. Of course, many users won't read the directions, so they will still have the problem.

There is also the issue of the 1 s delay before the input takes effect. Not a huge issue, but it doesn't provide a very responsive feeling to the interface.

I explained the cause and described the fix for these issues here: #44 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok ill look into your suggestions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@per1234 should I mention in the document to use no line ending or should I try improvising code to work for newline?

Copy link
Contributor

@per1234 per1234 Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue still occurs with the line ending menu set to "Both NL & CR". DONE

The reason is because serial communication is relatively slow. Serial.parseInt() returns as soon as it encounters a non-digit. So when Serial.parseInt() returns the NL has already been received, but the CR has not. The "remove the line endings" code you added removes the NL, then Serial.available() returns 0 so the while loop exits. On the next time through the loop function the CR is in the receive buffer, so Serial.parseInt() returns 0 (because that's what it does when it doesn't encounter any digits).

The solution is to add a short delay before the "remove the line endings" code to allow the CR to be received, as I demonstrated in #44 (comment).

myservo.write(input); // sets the servo position
Serial.print("Servo set to ");
Serial.print(input);
Serial.println(" degree");
delay (1000);
}
}