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-missing-examples-of-Servo-library #52

Open
wants to merge 2 commits 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
55 changes: 55 additions & 0 deletions examples/MultipleServoMotors/MultipleServoMotors.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Example of attach() and detach()
by Bhargav Patel <https://github.com/Engineer1999/>

Circuit description
+-----------+--------+ +-----------+--------+
|Servo_1 | Arduino| |Servo_2 | Arduino|
|-----------+--------| |-----------+--------|
|Ground | GND | |Ground | GND |
|Power | 5V | |Power | 5V |
|Signal | D10 | |Signal | D11 |
+-----------+--------+ +-----------+--------+
*/
#include <Servo.h>

Servo myservo; // create servo object to control a servo

int angle; // variable to read the angle value of servo horn

void setup() {
Serial.begin(9600); // initializing serial communication with 9600 baud rate
myservo.attach(10); // attaches the servo on pin 10 to the servo object
if (myservo.attached()) { // check whether servo motor is attached or not
Serial.println("Servo motor attached to pin 10");
myservo.write(0); // setting angle of servo horn at 0 degree
delay(1000); // delay of 1 second
myservo.write(90); // setting angle of servo horn at 90 degree
delay(1000);
myservo.write(180); // setting angle of servo horn at 180 degree
delay(1000);
} else {
Serial.println("Servo motor attachment failed");
}
myservo.attach(11); // attaches the servo on pin 11 to the servo object
if (myservo.attached()) { // check whether servo motor is attached or not
Serial.println("Servo motor attached to pin 11");
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
} else {
Serial.println("Servo motor attachment failed");
}
myservo.detach(); // detach the servo motor
if (myservo.attached()) { // check whether servo motor is attached or not
Serial.println("Servo motor is attached");
} else {
Serial.println("Servo motor was detached");
}
}

void loop() {
}
59 changes: 59 additions & 0 deletions examples/PWMInput/PWMInput.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
PWM input example for servo motor.
by Bhargav Patel <https://github.com/Engineer1999/>

Circuit description
+-----------+--------+
|Servo | Arduino|
|-----------+--------|
|Ground | GND |
|Power | 5V |
|Signal | D10 |
+-----------+--------+

Mapping of pulse duration(width) to angle
+-----------------------------+-----------+
|Pulse duration(micro second) | Angle |
+-----------------------------+-----------+
| 500 | 0 |
| 1010 | 45 |
| 1480 | 90 |
| 2400 | 180 |
+-----------------------------+-----------+
*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int angle; // variable to read the value of the angle

void setup(){
Serial.begin(9600); // initializing serial communication with 9600 baud rate
myservo.attach(10); // attaches the servo on pin 10 to the servo object
}
void loop(){
myservo.writeMicroseconds(500); // setting PWM to 500
angle = myservo.read(); // read the value of the current angle of servo horn
Serial.print("Angle at PWM value 500: ");
Serial.println(angle); // print angle of servo horn on serial monitor
delay(2000); // delay of 1 second

myservo.writeMicroseconds(1010); // setting PWM to 1010
angle = myservo.read();
Serial.print("Angle at PWM value 1010: ");
Serial.println(angle);
delay(1000);

myservo.writeMicroseconds(1480); // setting PWM to 1480
angle = myservo.read();
Serial.print("Angle at PWM value 1480: ");
Serial.println(angle);
delay(1000);

myservo.writeMicroseconds(2400); // setting PWM to 2400
angle = myservo.read();
Serial.print("Angle at PWM value 2400: ");
Serial.println(angle);
delay(1000);
}