diff --git a/examples/SerialServo_using_ParsInt/SerialServo_using_ParsInt.ino b/examples/SerialServo_using_ParsInt/SerialServo_using_ParsInt.ino new file mode 100644 index 0000000..9a7888c --- /dev/null +++ b/examples/SerialServo_using_ParsInt/SerialServo_using_ParsInt.ino @@ -0,0 +1,17 @@ +#include +Servo servo1; +void setup() +{ + Serial.begin(9600); + servo1.attach(9); + servo1.write(0); + Serial.println("Enter angle between 0 to 180"); +} +void loop() +{ + if(Serial.available()>0) + { + int angle=Serial.parseInt(); + servo1.write(angle); + } +} diff --git a/examples/SerialServo_using_StrToInt/SerialServo_using_StrToInt.ino b/examples/SerialServo_using_StrToInt/SerialServo_using_StrToInt.ino new file mode 100644 index 0000000..3d18e8a --- /dev/null +++ b/examples/SerialServo_using_StrToInt/SerialServo_using_StrToInt.ino @@ -0,0 +1,28 @@ +#include +Servo servo1; +String input =""; +void setup() +{ + Serial.begin(9600); + servo1.attach(9); + servo1.write(0); + Serial.println("Enter angle between 0 to 180"); +} +void loop() +{ + //servo1.write(0); + while(Serial.available()>0) + { + int inchar = Serial.read(); + if(isDigit(inchar)) + { + input+=(char)inchar; + } + if(inchar=='\n'){ + servo1.write(input.toInt()); + delay(5000); + servo1.write(0); + input=""; + } + } +} diff --git a/examples/ServoSerial/ServoSerial.ino b/examples/ServoSerial/ServoSerial.ino new file mode 100644 index 0000000..11a5bce --- /dev/null +++ b/examples/ServoSerial/ServoSerial.ino @@ -0,0 +1,40 @@ +#include +Servo s1; +int angle =0; +void setup() { + Serial.begin(38400); + s1.attach(9); + Serial.println("Enter angle from 0 to 180"); +} +void loop() { + static char input[4]; //Declaring a string to store value + static uint8_t i=0; + char c ; // Declaring a character to store the input + int angle =0; + s1.write(angle); + if (Serial.available ()) + { + c = Serial.read (); + if ( c != '\r' && i <4 ) // assuming "Carriage Return" is chosen in the Serial monitor as the line ending character + { + input[i] = c; + i=i+1; + } + else + { + input[i] = '\0'; + i = 0; + } + //As serial monitor consider entire number digit vise so stored every digit in string called input + int pos =0; + angle = atoi(input);//Converting string to an interger value + Serial.println("angle is:"); + Serial.println(angle); + for (pos = 0; pos <= angle; pos += 1) // goes from 0 degrees to angle given in degrees in steps of 1 degree + { + s1.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } + Serial.flush(); + } +}