help needed :(

hi all im new to all this i was wondering if any one could help me with some coding im using the knob example to start off with all i want to do is add another servo to this code so i can control two servos with 2 potentiometers any help would be great THANKS :-[

Just duplicate all variables used to move the servo in the example, and give the duplicated set new names.

One might do servo1 and servo2, for instance.

If your code works with one potentiometer controlling one servo, adding another potentiometer and another servo is pretty simple. You need to define the new pin and servo. Then, in loop, replicate the code to check the potentiometer sensor and drive the servo, and change the pin and servo references in copy.

If you have problems, post the code and an explanation of the problem.

hi thanks for the reply but im still having trouble heres my code… dont laugh LOL

// by Michal Rinott http://people.interaction-ivrea.it/m.rinott

#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
int potpin = 1;
int val1;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(10);
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 425, 500, 0, 180); // 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(1); // waits for the servo to get there
}
void loop()
{
val1 = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val, 425, 500, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(val); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
}

#include <Servo.h> 

Servo myservo1;  // create servo object to control a servo 
Servo myservo2;  // create servo object to control a servo 

int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;

void setup() 
{ 
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(10);
} 

void loop() 
{ 
 myservo1.write( map(analogRead(potpin1),  0, 1024, 0, 180 ); 
 delay(1);                           // waits for the servo to get there 
 myservo2.write( map(analogRead(potpin2),  0, 1024, 0, 180 ); 
 delay(1);                           // waits for the servo to get there 
}

:slight_smile:

or in other words you can only have 1 void loop function

AlphaBeta suggested that OP change the map from range to 0 to 1023, which is a good idea. But, I wonder why the original poster was using a map from range from 425 to 500. Is that the range of values you are actually seeing on the potentiometer pin?

hi alphbeta thanks for your help but im still having a problem with your code when i up load it to the board it gives me an error saying:

In function ‘void loop()’:
error: expected `)’ before ‘;’ token

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int potpin1 = 0; // analog pin used to connect the potentiometer
int potpin2 = 1;

void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10);
}

void loop()
{
myservo1.write( map(analogRead(potpin1), 0, 1024, 0, 180 );
delay(1); // waits for the servo to get there
myservo2.write( map(analogRead(potpin2), 0, 1024, 0, 180 );
delay(1); // waits for the servo to get there
}

any help would be greatly aprecited

In function 'void loop()':
error: expected `)' before ';' token

myservo1.write( map(analogRead(potpin1),  0, 1024, 0, 180 ) [color=#ff0000])[/color];

If you are using multiple pots to control multiple servos then something like this may be easier:

#include <Servo.h>

const int servoCount = 2;
const int servoPins[] = {9,10};
const int potPins[] = {0,1};

Servo myservos[servoCount]; 

void setup()
{
  for(int i=0; i < servoCount; i++)
    myservos[i].attach(servoPins[i]); 
}

void loop()
{
   for(int i=0; i < servoCount; i++){
       myservos[i].write( map(analogRead(potPins[i]), 0, 1023, 0, 180 ));
   }
   delay(15);      // Best if this delay is not less than 15 or 20 ms
}

AlphaBeta you RULE the world thanks so so much it works!!!!

Thanks a million

Thanks for providing your code. It works great with all the Servopins in place. However if I only connect one pot to one pin ( all the others see zero volts) then all servos move together as the pot is adjusted, not just the one linked to the pin. How can I fix that?

Its due to the interaction of unconnected analog input channels. You can fix it by connecting all the analog inputs channels to a pot or resistor. Or change the code to not use inputs that are not connected to anything.

BTW, If you are using code derived from post 4, it is advisable to change the value of delay in loop from 1 to 20. setting the servo value more frequently than every 20ms has no affect on the servo (the value is only read every 20ms) but interrupts are disabled and re-enabled every time you call the servo.write function.

Thanks Mem. I just wrote multiple versions of the code as you suggested, depending on the number of inputs that were connected. I am using 15ms delay (from tutorials) which seems to be working. Does it need to be increased to 20?

It will not get updated faster than every 20ms, but 15 commonly used and not a problem.