-
Notifications
You must be signed in to change notification settings - Fork 263
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
noob-master147
wants to merge
7
commits into
arduino-libraries:master
Choose a base branch
from
noob-master147:callibrate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bb23457
callibrate example for Servo
noob-master147 6001753
calibrate example for servo
noob-master147 4b31d2e
changes requested
noob-master147 a928daa
bug fix for serial monitor
noob-master147 e69088d
Auto Format implement
noob-master147 8819502
bug fix for both Newline and carriage return
noob-master147 be98aac
suggestions considered
noob-master147 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* Calibrate | ||
This code allows you to calibrate your servo motor using the serial monitor | ||
Enter the angle in the serial monitor | ||
by Divyansh Khandelwal <github.com/noob-master147> | ||
This example code is in the public domain. | ||
|
||
modified 24th March 2020 | ||
by Divyansh Khandelwal | ||
*/ | ||
|
||
#include <Servo.h> | ||
Servo myservo; // create servo object to control a servo | ||
int input; // variable to read the value from the Serial Monitor | ||
int servoPin = 11; // the Arduino pin the servo is attached to | ||
|
||
|
||
void setup() { | ||
myservo.attach(servoPin); // attaches the servo on servoPin to the servo object | ||
Serial.begin(9600); | ||
Serial.println("Enter The Angle from 0 to 180"); | ||
} | ||
|
||
void loop() { | ||
if (Serial.available() > 0) { | ||
input = Serial.parseInt(); // reads the value from Serial | ||
delay(25); | ||
while (Serial.available()) { // remove the line endings | ||
Serial.read(); | ||
} | ||
myservo.write(input); // sets the servo position | ||
delay (25); | ||
Serial.print("Servo set to "); | ||
Serial.print(input); | ||
Serial.println(" degree"); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
"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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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".DONEThe reason is because serial communication is relatively slow.
Serial.parseInt()
returns as soon as it encounters a non-digit. So whenSerial.parseInt()
returns the NL has already been received, but the CR has not. The "remove the line endings" code you added removes the NL, thenSerial.available()
returns 0 so thewhile
loop exits. On the next time through theloop
function the CR is in the receive buffer, soSerial.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).