0% found this document useful (0 votes)
12 views9 pages

Servo Motor Programming Guide

The document provides an overview of servo motors, including their types and basic functionality for controlling rotation angles. It includes several code examples in C++ demonstrating how to use the Servo library to rotate a servo motor at specified angles using different programming constructs like loops and functions. Additionally, it explains the behavior of the servo when given a negative angle input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Servo Motor Programming Guide

The document provides an overview of servo motors, including their types and basic functionality for controlling rotation angles. It includes several code examples in C++ demonstrating how to use the Servo library to rotate a servo motor at specified angles using different programming constructs like loops and functions. Additionally, it explains the behavior of the servo when given a negative angle input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Servo

❖ Servo motor:
✓ Give order to rotate with a specific angle.
✓ We can use it to rotate our robot.
✓ There are different types of servo like:
o Nano
o Sub-micro
o Micro
o Standard
o Large
✓ Servo have 3 gats
1- ground
2- power
3- signal (pin)
❖ make servo rotate with angle 90:
#include<Servo.h>
Servo s;
void setup()
{
[Link](13);
}
void loop()
{
[Link](90); }
✓ #include<Servo.h> => we will call servo libarary.
✓ Servo s => create an object from Servo library to
work on it.
✓ [Link](13) => to connect the servo with pin to work on,
the pin number is 13
✓ [Link](30) => give an order to make the servo rotate
with angle 30.

❖ make a function to rotate the servo with a different angle:


#include<Servo.h>
Servo s;
void setup()
{
[Link](13);
}
void loop()
{
rotate(30);
rotate(90);
}
void rotate(int angle){
[Link](angle);
delay(1000);
}
❖ make the servo rotate with 10 angle foreach 1 seconed using for
loop:
#include<Servo.h>
Servo s;
void setup()
{
[Link](13);
}
void loop()
{
for(int i = 0; i<=180 ; i+= 10){
rotate(i);
}
}
void rotate(int angle){
[Link](angle);
delay(1000);
}
❖ make the servo rotate with 10 angle foreach 1 second until reach
180 angle using for loop and return back to zero point:
#include<Servo.h>
Servo s;
void setup()
{
[Link](13);
}
void loop()
{
for(int i = 0; i<=180 ; i+= 10){
rotate(i);
}
delay(2000);
for(int i = 180; i>=0 ; i-= 10){
rotate(i);
}
}
void rotate(int angle){
[Link](angle);
delay(1000);
}
❖ make the last example but with while and do-while loops:
#include<Servo.h>
Servo s;
void setup()
{
[Link](13);
}

void loop()
{
int i = 0;
while(i<=180){
rotate(i);
i+=10;

}
delay(2000);
do{
rotate(i);
i-=10;
}while(i>=0);
}

void rotate(int ang){


[Link](ang);
delay(500);
}
❖ use a return function to make the servo rotate with 45 angle each
time until it reach 180:
#include<Servo.h>
Servo s;
int i =0;
void setup()
{
[Link](13);
}
void loop()
{
int a1 = rotate();
[Link](a1);
delay(500);
}

int rotate(){
i = i+45;
return i;
}
❖ if we use negative angle:
#include<Servo.h>
Servo s;
void setup()
{
[Link](13);
}
void loop()
{
[Link](-50);
}
✓ nothing will happen..
✓ as the program will work with it as it is a zero angle !!

You might also like