more than one potentiometer?

Ok I'm a newb to microprocessors. I want to start my first little project. I would like to control 2 servo's with 2 separate potentiometers. I've search around but I did not find any info on it. I tried to just add another "int potpin" but that didn't work.

Thanks for any help :D

Should be straight forward. What problems are you seeing? You should post your code. Also how are you providing voltage for the two servos Typically people run into problems when trying to run two or more servos using the Arduino +5vdc power, they really should be powered from an external regulated +5vdc power source.

Lefty

I tried to just add another "int potpin" but that didn't work.

In what way didn't it work? I'm guesssing it didn't compile - you can't simply have two variables of the same name at the same scope level.

Keep just one "potpin", but turn it into an array of two elements.

const int potpin [2] = {0, 1};
Servo myServo [2];

The rest is an exercise for the reader.

ahh ok thanks, once again I'm newb at this kind of stuff..I was just trying to add another potpin, but it didn't compile. I'll play around with what you posted groove. Thanks for the help :)

I was just trying to add another potpin, but it didn't compile

I think the conceptual piece of the puzzle you're missing is that in this context "potpin" is a variable, which is like a storage box for data with a label stuck on it. Since it was already declared you can't declare it again: that would be like having two storage boxes both with the same label, and you wouldn't be able to refer to them unambiguously.

@Groove's suggestion to change the "potpin" variable from a simple integer to an array of integers is an excellent way to do it, but if you're just getting started that may be too confusing. A conceptually simpler way to do it is to add a second variable (storage box) with a different label and use that to refer to the second pot, like this:

const int potpin = 0;
const int potpin2 = 1;

You will likewise need to create 2 "Servo" objects:

Servo myServo potpin;
Servo myServo2 potpin2;

Then you'll need to refer to each of them in the subsequent code.

@Groove's suggestion is technically a better way to do it but if you're just getting started with programming you should start with the basics and work up. If you're curious about arrays then you can think if them as being like an extension to my "storage boxes with labels" analogy above: an array is like a storage box with lots of compartments in it, so to reference a specific piece of data you refer to the name of the array (box) and then the element (compartment) within it.

Jon Freetronics: www.freetronics.com Practical Arduino: www.practicalarduino.com

Servo myServo potpin;

This would not be a good idea. You need a second "servopin", and you need to apply the "attach" method. Arrays really do make it easier to associate disparate objects like analogue inputs and servos, since connected items share a common array index:myServo [[glow]i[/glow]].write (analogRead (potPin [[glow]i[/glow]]), 0, 1023, 0, 180));

Arrays really do make it easier to associate disparate objects

Absolutely, I agree, but based on the way the original question was worded it sounded like the OP doesn't even know what a variable is so jumping straight to "use an array" as an answer without much explanation won't get them very far. The OP first needs to learn about what an "int" is.

Perhaps I misread the original question, but that was the impression I got.

Thanks for the help Jonathan and Groove. I really appreciate it. I finally got it working, here is the code I used.

//controlling 2 servos with 2 separate potentiometers

#include <Servo.h> 

Servo servo1; //create servo object to control a serco
Servo servo2; //create servo object to control a servo

int potpin = 0;  //analog pin 0
int potpin2 = 1; //analog pin 1
int val;        // variable to read the value from the analog pin 

void setup()
{ 
                                                             
  servo1.attach(9); // attaches servo to digital pin 9
  servo2.attach(8); // attaches servo to digital pin 8
} 
 
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)
  servo1.write(val);                // sets the servo position according to the scaled value 
  delay(15);

  val = analogRead(potpin2);       // 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)
  servo2.write(val);
  delay(15);                      // waits for the servo to get there 
}

For reference, with arrays and loops: (uncompiled, untested)

//controlling n servos with n separate potentiometers
#include <Servo.h>

#define N_SERVOS 2

Servo servo [N_SERVOS];
const int servopin [N_SERVOS] = {9, 8};
const int potpin    [N_SERVOS] = {0, 1};

void setup()
{
  for (int i = 0; i < N_SERVOS; i++) {
    servo[i].attach(servopin [i]);
  }
}

void loop()
{
  for (int i = 0; i < N_SERVOS; i++) {
    servo [i].write (map(analogRead(potpin [i]), 0, 1023, 0, 179));
  }  
  delay(15); // they both get written at virtually the same time, so only need one delay.
}