From 4144d4df15caff1cb6c301f5a667997666aa9811 Mon Sep 17 00:00:00 2001 From: Ace <36852564+Ace-Knight@users.noreply.github.com> Date: Fri, 20 Mar 2020 15:09:30 +0500 Subject: [PATCH 1/9] Create Input.ino --- examples/Input/Input.ino | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/Input/Input.ino diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino new file mode 100644 index 0000000..2440bf5 --- /dev/null +++ b/examples/Input/Input.ino @@ -0,0 +1,26 @@ +/* + Controlling a servo position using serial input + by Hamza Khalid https://github.com/hmzakhalid + +*/ + +#include + +Servo myservo; // create servo object to control a servo + + +int pos = 0; // initial Position of the Shaft (max is 180) + +void setup() { + myservo.attach(9); // attaches the servo on pin 9 to the servo object + Serial.println("Enter Position from 1 to 180"); +} + + +void loop() { + pos = Serial.read(); // Reading the input from the serial monitor and storing it in'pos' + if(pos > 0){ // checks if there is any input + myservo.write(pos); // tell servo to go to that position + delay(15); // waits 15ms for the servo to reach the position + } +} From aa20483b729d63d14017e23195c78e3808bc83f1 Mon Sep 17 00:00:00 2001 From: Ace <36852564+Ace-Knight@users.noreply.github.com> Date: Fri, 20 Mar 2020 15:11:08 +0500 Subject: [PATCH 2/9] Delete Input.ino --- examples/Input/Input.ino | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 examples/Input/Input.ino diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino deleted file mode 100644 index 2440bf5..0000000 --- a/examples/Input/Input.ino +++ /dev/null @@ -1,26 +0,0 @@ -/* - Controlling a servo position using serial input - by Hamza Khalid https://github.com/hmzakhalid - -*/ - -#include - -Servo myservo; // create servo object to control a servo - - -int pos = 0; // initial Position of the Shaft (max is 180) - -void setup() { - myservo.attach(9); // attaches the servo on pin 9 to the servo object - Serial.println("Enter Position from 1 to 180"); -} - - -void loop() { - pos = Serial.read(); // Reading the input from the serial monitor and storing it in'pos' - if(pos > 0){ // checks if there is any input - myservo.write(pos); // tell servo to go to that position - delay(15); // waits 15ms for the servo to reach the position - } -} From a45a57c19f119ec1f330d06a2a40a122e92272a6 Mon Sep 17 00:00:00 2001 From: Ace <36852564+Ace-Knight@users.noreply.github.com> Date: Fri, 20 Mar 2020 15:12:13 +0500 Subject: [PATCH 3/9] New Example input Controlling the shaft of servo using serial input --- examples/Input/Input.ino | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/Input/Input.ino diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino new file mode 100644 index 0000000..2440bf5 --- /dev/null +++ b/examples/Input/Input.ino @@ -0,0 +1,26 @@ +/* + Controlling a servo position using serial input + by Hamza Khalid https://github.com/hmzakhalid + +*/ + +#include + +Servo myservo; // create servo object to control a servo + + +int pos = 0; // initial Position of the Shaft (max is 180) + +void setup() { + myservo.attach(9); // attaches the servo on pin 9 to the servo object + Serial.println("Enter Position from 1 to 180"); +} + + +void loop() { + pos = Serial.read(); // Reading the input from the serial monitor and storing it in'pos' + if(pos > 0){ // checks if there is any input + myservo.write(pos); // tell servo to go to that position + delay(15); // waits 15ms for the servo to reach the position + } +} From ff5f517b970a2c89194c9b6774933980c0643878 Mon Sep 17 00:00:00 2001 From: Hamza Khalid <36852564+hmzakhalid@users.noreply.github.com> Date: Fri, 20 Mar 2020 15:18:18 +0500 Subject: [PATCH 4/9] Opened a Serial Port --- examples/Input/Input.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino index 2440bf5..7fddb28 100644 --- a/examples/Input/Input.ino +++ b/examples/Input/Input.ino @@ -13,6 +13,7 @@ int pos = 0; // initial Position of the Shaft (max is 180) void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object + Serial.begin(9600); //opens serial port, sets data rate to 9600 bps Serial.println("Enter Position from 1 to 180"); } From c413bdd7595f63fccedb8db0990c5be053d443bc Mon Sep 17 00:00:00 2001 From: Hamza Khalid <36852564+hmzakhalid@users.noreply.github.com> Date: Sat, 21 Mar 2020 10:30:11 +0500 Subject: [PATCH 5/9] Fixed the Code a bit. Added the Serial.Available Command to check incoming input --- examples/Input/Input.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino index 7fddb28..3061bca 100644 --- a/examples/Input/Input.ino +++ b/examples/Input/Input.ino @@ -19,8 +19,9 @@ void setup() { void loop() { - pos = Serial.read(); // Reading the input from the serial monitor and storing it in'pos' - if(pos > 0){ // checks if there is any input + + if(Serial.available() > 0){ // checks if there is any input on the serial monitor + pos = Serial.read(); // Reading the input from the serial monitor and storing it in'pos' myservo.write(pos); // tell servo to go to that position delay(15); // waits 15ms for the servo to reach the position } From 0a6754d533556a62d278a4a3804ca66ce5dd7c49 Mon Sep 17 00:00:00 2001 From: Hamza Khalid <36852564+hmzakhalid@users.noreply.github.com> Date: Sat, 21 Mar 2020 13:13:10 +0500 Subject: [PATCH 6/9] Fixed input stream issues. Removed random capitalization. Removed random line spaces. Fixed Input from the serial moniter. --- examples/Input/Input.ino | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino index 3061bca..be3022f 100644 --- a/examples/Input/Input.ino +++ b/examples/Input/Input.ino @@ -1,15 +1,13 @@ /* Controlling a servo position using serial input by Hamza Khalid https://github.com/hmzakhalid - */ #include Servo myservo; // create servo object to control a servo - -int pos = 0; // initial Position of the Shaft (max is 180) +long pos = 0; // initial position of the shaft (max is 180) void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object @@ -17,12 +15,9 @@ void setup() { Serial.println("Enter Position from 1 to 180"); } - void loop() { - - if(Serial.available() > 0){ // checks if there is any input on the serial monitor - pos = Serial.read(); // Reading the input from the serial monitor and storing it in'pos' + if(Serial.available() > 0){ // checks if there is any input on the serial monitor + pos = Serial.parseInt(); // Reading the input from the serial monitor and storing it in'pos' myservo.write(pos); // tell servo to go to that position - delay(15); // waits 15ms for the servo to reach the position } } From 5f94f8da4a2a36cdda8c1d9bd507375586518b22 Mon Sep 17 00:00:00 2001 From: Hamza Khalid <36852564+hmzakhalid@users.noreply.github.com> Date: Sat, 21 Mar 2020 13:14:26 +0500 Subject: [PATCH 7/9] Used correct Class name --- examples/Input/Input.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino index be3022f..57da37a 100644 --- a/examples/Input/Input.ino +++ b/examples/Input/Input.ino @@ -5,12 +5,12 @@ #include -Servo myservo; // create servo object to control a servo +Servo myservo; // create Servo object to control a servo motor long pos = 0; // initial position of the shaft (max is 180) void setup() { - myservo.attach(9); // attaches the servo on pin 9 to the servo object + myservo.attach(9); // attaches the servo on pin 9 to the Servo object Serial.begin(9600); //opens serial port, sets data rate to 9600 bps Serial.println("Enter Position from 1 to 180"); } From d70c6b39cd5fbc938786a8f14c301a3ad7263bbe Mon Sep 17 00:00:00 2001 From: Hamza Khalid <36852564+hmzakhalid@users.noreply.github.com> Date: Sat, 21 Mar 2020 13:15:35 +0500 Subject: [PATCH 8/9] Update Input.ino --- examples/Input/Input.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino index 57da37a..722655e 100644 --- a/examples/Input/Input.ino +++ b/examples/Input/Input.ino @@ -6,7 +6,6 @@ #include Servo myservo; // create Servo object to control a servo motor - long pos = 0; // initial position of the shaft (max is 180) void setup() { From 2709147bee8a047256061903f8958c0b52c878ab Mon Sep 17 00:00:00 2001 From: Hamza Khalid <36852564+hmzakhalid@users.noreply.github.com> Date: Sat, 21 Mar 2020 19:27:45 +0500 Subject: [PATCH 9/9] Dump line Endings. Fixed New Line and CR that caused to return 0 . --- examples/Input/Input.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/Input/Input.ino b/examples/Input/Input.ino index 722655e..2ba057f 100644 --- a/examples/Input/Input.ino +++ b/examples/Input/Input.ino @@ -15,8 +15,12 @@ void setup() { } void loop() { - if(Serial.available() > 0){ // checks if there is any input on the serial monitor + if(Serial.available()){ // checks if there is any input on the serial monitor pos = Serial.parseInt(); // Reading the input from the serial monitor and storing it in'pos' + delay(2); // wait for all the serial data to come in + while (Serial.available()) { // dump line endings from the received data + Serial.read(); + } myservo.write(pos); // tell servo to go to that position } }