diff --git a/examples/MultipleServoMotors/MultipleServoMotors.ino b/examples/MultipleServoMotors/MultipleServoMotors.ino new file mode 100644 index 0000000..6957cf2 --- /dev/null +++ b/examples/MultipleServoMotors/MultipleServoMotors.ino @@ -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() { +} diff --git a/examples/PWMInput/PWMInput.ino b/examples/PWMInput/PWMInput.ino new file mode 100644 index 0000000..e73ffc8 --- /dev/null +++ b/examples/PWMInput/PWMInput.ino @@ -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); +}