10 servos, 10 potentiometers, issues

The map function call looks like this:

int newValue = map(value, fromLow, fromHigh, toLow, toHigh);

fromLow and fromHigh are the range to map from (0 to 1023 in your case). toLow and toHigh are the range to map to (0 to 180 in your case).

It won't matter what kind of resistance you're using, it will end up reading the same as long as it's a voltage divider (potentiometer).

potvals[x] = map(analogRead(potpins[x]), 0, 1023, 0, 179);

That's got to be inside the loop, replace:
potvals[x] = analogRead(potpins[x]); // remove completely

with:
potvals[x] = map(analogRead(potpins[x]), 0, 1023, 0, 179);

It needs to be the same position. Also, now when you want to write the values to a servo, you can use:

servo0.Write(potvals[0]);
servo1.Write(potvals[1]); // etc.

Hopefully this helps!:slight_smile:

To really see the benefit of using arrays, I would have a Servo array as well.

That way you can do:

const byte NUMBER_OF_SERVOS = 10;

//later in code
//set all servos position equal to the potvals
for (byte i=0; i<NUMBER_OF_SERVOS; i++){
  servo[i].write(potvals[i]); 
}

:slight_smile:

Hey.... I was feeling semi-smart here, thanks for ruining it Alpha! :smiley:

I'm still learning, slowly but surely... haha, seems that helping others learn, speeds up the learning process on my end as well! :slight_smile:

But yes, arrays are something to definitely get used to, save you alot of time/typing/code/space/sanity!

There are ALOT of tutorials with C++, alot of good videos that can explain alot, in a bit more depth than most of us are willing to type out!:smiley:

Hey! We addicts have to support each other, you know.
[my sign. used to say that I was an arduinoAddict]

I just spon off your ideas, no glory in that (maybe if you ask Micr*soft). :wink:

Ok, I don't get what Alpha wrote :stuck_out_tongue:

Well, I should of probably defined what I am trying to do before anything lol

I'm trying to control 10 servos with 10 5K potentiometers each with assigned to each other and able to move independently (or physically appearing to do so).

The megaservo example looks similar to what Alpha posted, but that just moves multiple servos with one potentiometer, and assume that the code is similar to physically wiring the servos in parallel.

And anyone know how to patch the IDE 17 on a mac for the Arduino Mega bug?

EDIT: Another thing; this really confuses me (maybe I'm just thinking too much :p). All the examples I've seen with a analog input and digital output, just has the pins/objects assigned just by using the pin numbers. Where I'm confused is to how it knows if you are referring to an analog pin or a digital pin?

like in this example int the learning section:

// Controlling a servo position using a potentiometer (variable resistor) 
// 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 

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 
}

I don't know if you caught this, but your "potpins" and "potvals" variables aren't the same size. If you're trying to use 10 inputs, potpins will be 0 to 9 (which is ten numbers), and your potvals should be an array with 10 values (potvals[10]). Then, when you read, you can do for(i=0; i<10; i++). This reads values 0 through 9 (10 values). You have to remember you're starting your counting from 0 now, not from 1 like humans are used to.

To patch the library, you'll have to edit the file wiring_analog.c. Instructions on what to do are here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250970792 I don't know where the arduino lives on a mac, so you can probably just do a search for the file.

As for which pins you read, that's built into the Arduino IDE. analogRead() knows which pins are the analog pins, and digitalRead() knows which pins are digital. The Arduino guys did all of that coordination for you, so digitalRead(10) reads from the digital pin with the 10 silkscreened next to it.

jpgr87: Yes I'm pretty sure we know. We were just focusing on the errors that I was posting.

That link does nothing for me because I'm on a mac and the wiring_analog.c file isn't found just by a simple search. Its most likely in a compressed file somewhere, which I am unable to find.

EDIT: well, I found it and it appears to be already patched :stuck_out_tongue:

Before you start. are you going to be using the built in power supply for all of this? You may overload the regulator.

I already built a 5v regulator that will step down my 18v batter pack to 5v.

I'm trying to control 10 servos with 10 5K potentiometers each with assigned to each other and able to move independently (or physically appearing to do so).

The megaservo example looks similar to what Alpha posted, but that just moves multiple servos with one potentiometer

The MegaServo example reads one pot and uses that value to control all the servos. AlhpaBeta's post shows how to change it so that a different pot can be used for of each servo.

Here is the full sketch with that change:

#include <Servo.h>
#define NBR_SERVOS 10      // the number of servos
#define FIRST_SERVO_PIN 2  // this first of consecutive digital pins used for servos

Servo Servos[NBR_SERVOS] ; // an array of servo objects

void setup()
{
  // attach all 10 servos to digital pins starting from 2
  for( int i =0; i < NBR_SERVOS; i++)
    Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);
}
void loop()
{ 
int pos = 0;      // variable to store the servo position 

  for( int i =0; i <NBR_SERVOS; i++) 
  {
    pos = analogRead(i);   // read a value from pins 0 to 9
    Servos[i].write( map(pos, 0,1023,0,180)); // and write the value to the servo  
  }
  delay(15);   
}

Thanks!!

I'll try it out as soon as my board comes in. Should be tomorrow or the next day.

hope I don't burn out something :stuck_out_tongue:

I just had another question come to mind. I've read somewhere that you can use a servo as a sensor. If thats the case, then with a SDcard shield would I be able to record live readings from the servos and play them back?

I've read somewhere that you can use a servo as a sensor

Not without opening it up and doing some soldering (assuming we're talking R/C servo here) - do you have a link?

I've read somewhere that you can use a servo as a sensor.

That depends on what you want to sense. As AWOL says, if you want to get the physical position you would need to modify the servo. But if you want to play back a sequence of servo movements its much easier to either store the values (and time) that are written to the servos, or to regularly read and store the servo position using servo.read().

What is it you want to achieve.

Hi, my name is Guzz and i'm new on the arduino world. I'm having some trouble to write a code to control 4 servos with a arduino, and i dont know what can be wrong with my code, i'm not a programmer (yet). I wrote this code based on de sweep code from the arduino examples. The code compiles fine, but when i try to upload to the board it crashes. Im using the arduino duemilanove with atmega 328. The code is like this...

#include <Servo.h>

Servo myservo0; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos0 = 0; // variable to store the servo position

Servo myservo1; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos1 = 0; // variable to store the servo position

Servo myservo2; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos2 = 0; // variable to store the servo position

Servo myservo3; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos3 = 0; // variable to store the servo position

void setup()
{
myservo0.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
myservo2.attach(7); // attaches the servo on pin 7 to the servo object
myservo3.attach(5); // attaches the servo on pin 5 to the servo object
}

void loop()
{
for(pos0 = 0; pos0 < 180; pos0 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo0.write(pos0); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos0 = 180; pos0>=1; pos0-=1) // goes from 180 degrees to 0 degrees
{
myservo0.write(pos0); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos1 = 0; pos1 < 180; pos1 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos1 = 180; pos1>=1; pos1-=1) // goes from 180 degrees to 0 degrees
{
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos2 = 0; pos2 < 180; pos2 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos2 = 180; pos2>=1; pos2-=1) // goes from 180 degrees to 0 degrees
{
myservo2.write(pos0); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos3 = 0; pos3 < 180; pos3 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos3 = 180; pos3>=1; pos3-=1) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos3); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
}

If someone could please take a look and tell me what could be wrong i'd be most greatfull.

PS: I wasnt sure if i should create a new post, but since we are discussing servos and arduino here i was hoping no one would mind.

Thanks everybody

If you're powering the four servos from the Arduino, it is likely that the crash is due to the excessive current being drawn, pulling down the voltage to the processor and causing it to brown-out or reset.

Power the servos from a separate supply (4 AA batteries should do it), and remember to common the grounds.

I can't see anything obvious in your code that would cause a crash.

Please use the Code (#) button when posting code, and it is best not to hijack other threads.

Thanks, and sorry, not gonna do it again :slight_smile:

No worries.
We programmers are lazy types, so I re-wrote your sketch to make it a bit shorter: [uncompiled, untested]

#include <Servo.h> 

Servo myservo [4];  // create servo object to control four servos

int pos [4] = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo[0].attach(9);  // attaches the servo on pin 9 to a servo object 
  myservo[1].attach(8);  // attaches the servo on pin 8 to a servo object 
  myservo[2].attach(7);  // attaches the servo on pin 7 to a servo object 
  myservo[3].attach(5);  // attaches the servo on pin 5 to a servo object 
} 

void loop() 
{ 
  for (int i = 0; i < 4; ++i) {
      for(pos[i] = 0; pos[i] < 180; pos[i] += 1)  
    {                                   
       myservo[i].write(pos[i]);
       delay(5);                       // waits 5ms for the servo to reach the position 
    } 
    for(pos[i] = 180; pos[i]>=1; pos[i]-=1)     // goes from 180 degrees to 0 degrees 
    {                                
       myservo[i].write(pos[i]);              // tell servo to go to position in variable 'pos' 
       delay(5);                       // waits 5ms for the servo to reach the position 
    } 
  }    
}