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

Added examples for servo library #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions examples/SerialServo_using_ParsInt/SerialServo_using_ParsInt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <Servo.h>
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);
}
}
28 changes: 28 additions & 0 deletions examples/SerialServo_using_StrToInt/SerialServo_using_StrToInt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <Servo.h>
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="";
}
}
}
40 changes: 40 additions & 0 deletions examples/ServoSerial/ServoSerial.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <Servo.h>
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();
}
}