Go Down

Topic: 10 servos, 10 potentiometers, issues (Read 7058 times) previous topic - next topic


still getting and error

*analogRead(potpins[0]); //

*error: expected constructor, destructor, or type conversion before '(' token In function 'void loop()':

BTW I'm using 5k potentiometers, so I need to map the 0, 511 values for the potentiometer to 0, 179 of the servo position.


I'm not sure why you're getting errors.. did you remove the two analogReads like I showed? Well I commented them out..

Yup, that's the error.. make sure the two analogReads before your SETUP are commented out... or better yet, just delete them completely! Once I remove the comment lines for them, I get the same exact error.

You don't need the analogRead before Setup because.. well.. it does no good! You read all 9 sensors within about .2 seconds inside the loop anyways.


ok, now it works.

But anybody have anything for the "map" function to translate the 5k potentiometer to 180 positions on the servo?


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).
The art of getting good answers lies in asking good questions.


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).

  • = map(analogRead(potpins
  • ), 0, 1023, 0, 179);

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

  • = map(analogRead(potpins
  • ), 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:

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

    Hopefully this helps!:)


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

That way you can do:
Code: [Select]

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++){



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

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

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!:D


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). ;)


Nov 08, 2009, 01:59 am Last Edit: Nov 08, 2009, 02:07 am by mtktm Reason: 1
Ok, I don't get what Alpha wrote :p

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:
Code: [Select]
// 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


Nov 08, 2009, 02:43 am Last Edit: Nov 08, 2009, 02:43 am by jpgr87 Reason: 1
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.


Nov 08, 2009, 03:05 am Last Edit: Nov 08, 2009, 03:44 am by mtktm Reason: 1
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 :p


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:
Code: [Select]
#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  


Nov 10, 2009, 07:34 pm Last Edit: Nov 10, 2009, 07:51 pm by mtktm Reason: 1

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 :p

Go Up