Step 9Controlling servos
Example 5- controlling a servo using analog input
These two examples show how easy it is to control servos using an analog input. You can use any analog input device you want- I'll use a 10k Ohm potentiometer for the example wiring diagram. As you turn the pot (and change its value) the servo moves proportionally.
The second code example simply extends the first example to control six servos from six inputs. This kind of control comes in really handy if you want to control several servos using bend sensors attached to a glove. This would work really well for controlling an animatronic mask.
/*
* Example 5
* Servo Control
* This example uses a servos and analog input to move the servo according to the sensor input value
* Honus 2010
*/
#include "Servo.h"
Servo servo1; // creates an instance of the servo object to control a servo
int analogPin = 0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor
int servoPin = 4; // Control pin for servo motor
void setup() {
servo1.attach(servoPin); // attaches the servo on pin 9 to the servo object
}
void loop()
{
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
analogValue = map(analogValue, 0, 1023, 0, 179); // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
servo1.write(analogValue); // write the new mapped analog value to set the position of the servo
delay(15); // waits for the servo to get there
}
Example 5a- Controlling 6 servos using multiple inputs
/*
* Example 5a
* Servo Control6
* This example uses 6 servos and analog inputs to move the servos according to the sensor input values
* Honus 2010
*/
#include
Servo servoMotor1; // creates an instance of the servo object to control a servo
Servo servoMotor2;
Servo servoMotor3;
Servo servoMotor4;
Servo servoMotor5;
Servo servoMotor6;
int analogPin1 = 0; // the analog pin that the sensor is on
int analogPin2 = 1;
int analogPin3 = 2;
int analogPin4 = 3;
int analogPin5 = 4;
int analogPin6 = 5;
int analogValue1 = 0; // the value returned from the analog sensor
int analogValue2 = 0;
int analogValue3 = 0;
int analogValue4 = 0;
int analogValue5 = 0;
int analogValue6 = 0;
int servoPin1 = 4; // Control pin for servo motor
int servoPin2 = 5;
int servoPin3 = 6;
int servoPin4 = 7;
int servoPin5 = 8;
int servoPin6 = 9;
void setup() {
servoMotor1.attach(servoPin1); // attaches the servo on pin 4 to the servo object
servoMotor2.attach(servoPin2); // attaches the servo on pin 5 to the servo object
servoMotor3.attach(servoPin3); // attaches the servo on pin 6 to the servo object
servoMotor4.attach(servoPin4); // attaches the servo on pin 7 to the servo object
servoMotor5.attach(servoPin5); // attaches the servo on pin 8 to the servo object
servoMotor6.attach(servoPin6); // attaches the servo on pin 9 to the servo object
}
void loop()
{
analogValue1 = analogRead(analogPin1); // read the analog input (value between 0 and 1023)
analogValue1 = map(analogValue1, 0, 1023, 0, 179); // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
servoMotor1.write(analogValue1); // write the new mapped analog value to set the position of the servo
analogValue2 = analogRead(analogPin2);
analogValue2 = map(analogValue2, 0, 1023, 0, 179);
servoMotor2.write(analogValue2);
analogValue3 = analogRead(analogPin3);
analogValue3 = map(analogValue3, 0, 1023, 0, 179);
servoMotor3.write(analogValue3);
analogValue4 = analogRead(analogPin4);
analogValue4 = map(analogValue4, 0, 1023, 0, 179);
servoMotor4.write(analogValue4);
analogValue5 = analogRead(analogPin5);
analogValue5 = map(analogValue5, 0, 1023, 0, 179);
servoMotor5.write(analogValue5);
analogValue6 = analogRead(analogPin6);
analogValue6 = map(analogValue6, 0, 1023, 0, 179);
servoMotor6.write(analogValue6);
delay(15); // waits for the servo to get there
}
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|























































