I'm trying to recreate this project with three servos rather than one. I'm an arduino novice, so I don't understand how to code very well yet. I also don't completely understand how to set up the circuit so that it will be able to contain all three servos along with the potentiometers. I'm really sorry about how little I know, but I would really appreciate your help.
here's the project:
http://arduino.cc/en/Tutorial/Knob
I know this code only programs for one servo, but I'm not sure how to alter it to support three:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
martse97:
I know this code only programs for one servo, but I'm not sure how to alter it to support three.
One word: Arrays.
#include <Servo.h>
Servo myservo[3]; // create servo object to control a servo
const int potpin[3] = {
A0, A1, A2}; // analog pins used to connect the potentiometer
const int servoPins[3] = {
3, 5, 8};
int val; // variable to read the value from the analog pin
void setup(){
for (int i=0; i<3; i++)
myservo[i].attach(servoPins[i]);
}
void loop() {
for (int i=0; i<3; i++) {
val = analogRead(potpin[i]); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo[i].write(val); // sets the servo position according to the scaled value
}
delay(15); // waits for the servo to get there
}
Below is a basic diagram of wiring a servo. Bottom is test code for two servos and two pots that includes a deadband setup to limit hunting. You should be able to add a third servo by looking how the first two are coded.
The pic attached shows how three servos can be connected if you use breadboard. In the pic, the right-hand one doesn't have a yellow wire shown, but of course it needs one. This method uses zoomcat's suggestion of powering servos from their own supply, not from the Arduino which is what you're probably doing if you followed the pic in the tutorial.
Note if you use breadboard, most of the full-size models need the gap in the middle of the power rails to be bridged with wire, as shown.
Three pots is difficult to manipulate.... I don't know what your project is, but you may find it useful to use a small joystick like this one which is just two pots at right angles. That link is my local supplier- sorry I don't know the actual manufacturer but I'm sure you can hunt that or a similar one down.
Edit.... btw if you go for John's excellent approach using arrays, you might like to read this.