10 servos, 10 potentiometers, issues

Well I have the Arduino Mega (ATmega1280)
I'm a bit of a novice, so don't expect much from me for now lol :stuck_out_tongue:

Well I am having a problem with the code that I modified from the example code in the tutorial section of using a potentiometer as an input to control a servo.

I want to verify the code before uploading it the board, but keep getting a "val_x was not declared in this scope" error.

Heres the code and the part highlighted is where it is.

#include <Servo.h>

Servo servo1; // create servo object for servo1
Servo servo2; // create servo object for servo2
Servo servo3; // create servo object for servo3
Servo servo4; // create servo object for servo4
Servo servo5; // create servo object for servo5
Servo servo6; // create servo object for servo6
Servo servo7; // create servo object for servo7
Servo servo8; // create servo object for servo8
Servo servo9; // create servo object for servo9
Servo servo10; // create servo object for servo10

int potpin0 = 0; // analog pin used to connect the potentiometer
int potpin1 = 1; // analog pin used to connect the potentiometer
int potpin2 = 2; // analog pin used to connect the potentiometer
int potpin3 = 3; // analog pin used to connect the potentiometer
int potpin4 = 4; // analog pin used to connect the potentiometer
int potpin5 = 5; // analog pin used to connect the potentiometer
int potpin6 = 6; // analog pin used to connect the potentiometer
int potpin7 = 7; // analog pin used to connect the potentiometer
int potpin8 = 8; // analog pin used to connect the potentiometer
int potpin9 = 9; // analog pin used to connect the potentiometer

int val_0; // variable to read from analog pin
int val_1; // variable to read from analog pin
int val_2; // variable to read from analog pin
int val_3; // variable to read from analog pin
int val_4; // variable to read from analog pin
int val_5; // variable to read from analog pin
int val_6; // variable to read from analog pin
int val_7; // variable to read from analog pin
int val_8; // variable to read from analog pin
int val_9; // variable to read from analog pin

void setup()
{
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo1.attach(3); // attaches the servo on pin 3 to the servo object
servo1.attach(4); // attaches the servo on pin 4 to the servo object
servo1.attach(5); // attaches the servo on pin 5 to the servo object
servo1.attach(6); // attaches the servo on pin 6 to the servo object
servo1.attach(7); // attaches the servo on pin 7 to the servo object
servo1.attach(8); // attaches the servo on pin 8 to the servo object
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo1.attach(10); // attaches the servo on pin 10 to the servo object
servo1.attach(11); // attaches the servo on pin 11 to the servo object
}

void loop()
{
val_0 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
val_0 = map(val, 0, 511, 0, 179);
servo1.write(val_0); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_1 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val_1 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo2.write(val_1); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_2 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val_2 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo3.write(val_2); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_3 = analogRead(potpin4); // reads the value of the potentiometer (value between 0 and 1023)
val_3 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo4.write(val_3); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_4 = analogRead(potpin5); // reads the value of the potentiometer (value between 0 and 1023)
val_4 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo5.write(val_4); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_5 = analogRead(potpin6); // reads the value of the potentiometer (value between 0 and 1023)
val_5 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo6.write(val_5); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_6 = analogRead(potpin7); // reads the value of the potentiometer (value between 0 and 1023)
val_6 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo7.write(val_6); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_7 = analogRead(potpin8); // reads the value of the potentiometer (value between 0 and 1023)
val_7 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo8.write(val_7); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_8 = analogRead(potpin9); // reads the value of the potentiometer (value between 0 and 1023)
val_8 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo9.write(val_8); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
val_9 = analogRead(potpin10); // reads the value of the potentiometer (value between 0 and 1023)
val_9 = map(val, 0, 511, 0, 179); // scale it to use it with the servo (value between 0 and 180)
servo10.write(val_9); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
}

I suspect that all the other similar lines will have the same errors :stuck_out_tongue:

Oh. nevermind. I just found my mistake :stuck_out_tongue: I feel stupid lol.

EDIT: well, while I have it posted, how do you think it will behave? in sequence or (somewhat) realtime? I'm waiting for my servos in the mail.

Any corrections I should make, or make the code more efficient?

how do you think it will behave?

leaving just 10ms for a servo to potentially traverse 180 degree is a big ask!

Arrays and loops would make it shorter (about half the size) and easier to read/debug/maintain.

Please use the Code (#) button when posting code.

When you actually try your program you may find that analog pins 8 and 9 don't work (actually all analog pins 8 to 15 don't work) on the Mega as there is a bug in the IDE core of version 17. It is suppose to be fixed in the next version, but there is a patch you can use to get it to work in version 17. Also version 16 works fine without the patch.

Here is the link to find the patch:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250980394

Lefty

I know this isn't very useful for your problem, but something I suggest you look into is Arrays! For sures. And for loops :slight_smile: With the arrays, you turn some of your longer code to a bit shorter! For example:

int potpin0 = 0; // analog pin used to connect the potentiometer
int potpin1 = 1; // analog pin used to connect the potentiometer
int potpin2 = 2; // analog pin used to connect the potentiometer
int potpin3 = 3; // analog pin used to connect the potentiometer
int potpin4 = 4; // analog pin used to connect the potentiometer
int potpin5 = 5; // analog pin used to connect the potentiometer
int potpin6 = 6; // analog pin used to connect the potentiometer
int potpin7 = 7; // analog pin used to connect the potentiometer
int potpin8 = 8; // analog pin used to connect the potentiometer

Can be addressed as:
int potpins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

Then, to address the pins from that:

analogRead(potpins[0]); //reads first analog pin
analogRead(potpins[8]); // reads last analog pin

And can do the same thing for all the variables you use for the values, address them the same:

int potvals[] = {0,0,0,0,0,0,0,0}; //lol think that's enough 0's

OR
int potvals[8]; // is the same as above, just doesn't set values

and then, you can use a for loop to read all of the pins at once with much less code:
for(int x = 0; x < 9; x++)
{
potvals[x] = analogRead(potpins[x])
}

;D
Went a bit off topic.. well, completely. But just some suggestions that took me a while to get used to!:slight_smile:

Thanks Lefty, but I'm doing everything from a mac and can't seem to find the wiring_analog.c let alone know what to modify it with.

Captain, I can't seem to get it to work.

I keep on getting an error at

"analogRead(potpins[8]); // reads last analog pin"

I'm afraid someone is going to have o walk me through this :stuck_out_tongue:

What kind of error? No mind-readers out here... :slight_smile:

error: expected initializer before 'analogRead' In function 'void loop()'

What do you have before the analogRead statement in question? If nothing on the same line, check that the previous line is properly terminated (has a ; at the end).

If all looks good to you, re-post your code, and we'll have a look at it.

well this is what I have now :stuck_out_tongue:

*#include <Servo.h>

Servo servo0; // create servo object for servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;

int potpins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

analogRead(potpins[0]); // reads first analog pin
analogRead(potpins[8]); // reads last analog pin

int potvals[8];

void setup()
{
servo0.attach(2); // attaches the servo on pin 2 to the servo object
servo1.attach(3); // attaches the servo on pin 3 to the servo object
servo2.attach(4); // attaches the servo on pin 4 to the servo object
servo3.attach(5); // attaches the servo on pin 5 to the servo object
servo4.attach(6); // attaches the servo on pin 6 to the servo object
servo5.attach(7); // attaches the servo on pin 7 to the servo object
servo6.attach(8); // attaches the servo on pin 8 to the servo object
servo7.attach(9); // attaches the servo on pin 9 to the servo object
servo8.attach(10); // attaches the servo on pin 10 to the servo object
servo9.attach(11); // attaches the servo on pin 11 to the servo object
}

void loop()
{
for(int x = 0; x < 9; x++)
{
potvals[x] = analogRead(potpins[x])
}
delay(18); // waits for the servo to get there
}

Might want to put a ; on the end of the analogRead line...

You're almost there! :smiley:

After int potpin[8] you forgot your ; but regardless, you still need to add some! :stuck_out_tongue:

Just using potpin[8] is just giving you an array of 9 integers. (including 0). Now, you need to set the value of each pot pin, so, instead of potpin[8] use :

byte potpins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; // set the number of the pins here, I'm assuming you're using 0 - 8. Can adjust to any number.

Now, to read the first pin, use:
analogRead(potpins[0]);//

So, above your setup, you can get rid of the two analogReads that you have, seeing as those will only run once, before the program starts, and there's no use for that! :smiley:

And also, just after that FOR loop you're using, it's just putting values you're reading into the array potvals, it's not actually doing anything with them.

?
it is there

Not the one in the for loop in the loop function.

#include <Servo.h>

Servo servo0;  // create servo object for servo
Servo servo1;  
Servo servo2;  
Servo servo3;  
Servo servo4;  
Servo servo5;  
Servo servo6;  
Servo servo7;  
Servo servo8;  
Servo servo9;  

int potpins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

//analogRead(potpins[0]);  // reads first analog pin  Not needed
//analogRead(potpins[8]);  // reads last analog pin  Not needed

int potvals[8];  // this is the same as potvals[] = {0,0,0,0,0,0,0,0};


void setup()
{
 servo0.attach(2);  // attaches the servo on pin 2 to the servo object
 servo1.attach(3);  // attaches the servo on pin 3 to the servo object
 servo2.attach(4);  // attaches the servo on pin 4 to the servo object
 servo3.attach(5);  // attaches the servo on pin 5 to the servo object
 servo4.attach(6);  // attaches the servo on pin 6 to the servo object
 servo5.attach(7);  // attaches the servo on pin 7 to the servo object
 servo6.attach(8);  // attaches the servo on pin 8 to the servo object
 servo7.attach(9);  // attaches the servo on pin 9 to the servo object
 servo8.attach(10);  // attaches the servo on pin 10 to the servo object
 servo9.attach(11);  // attaches the servo on pin 11 to the servo object
}

void loop()
{
 for(int x = 0; x < 9; x++)
 {
/*
starts at 0, and works up to 8, potvals[0] = analogRead(potpins[0]);  and potvals[1] = analogRead(potpins[1]);  etc, until it gets to 8.
*/

    potvals[x] = analogRead(potpins[x]); // forgot the ; here
 }
  delay(18);                           // waits for the servo to get there
for(int x = 0; x < 9; x++){
    Serial.println(potvals[x]); // this will show you what values you've got after reading all of them
}
}

I haven't tested, but should compile.

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

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: