Go Down

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

The Question

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


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)
 [glow]val_0 = map(val, 0, 511, 0, 179);[/glow]
 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 :p

The Question

#1
Nov 07, 2009, 01:00 pm Last Edit: Nov 07, 2009, 01:11 pm by mtktm Reason: 1
Oh. nevermind. I just found my mistake :p 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?

AWOL

#2
Nov 07, 2009, 01:24 pm Last Edit: Nov 07, 2009, 02:41 pm by AWOL Reason: 1
Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

retrolefty

#3
Nov 07, 2009, 01:32 pm Last Edit: Nov 07, 2009, 01:32 pm by retrolefty Reason: 1
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

CaptainObvious

I know this isn't very useful for your problem, but something I suggest you look into is Arrays! For sures. And for loops :)  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
  • = analogRead(potpins
  • )
    }

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


The Question

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

PaulS

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

The Question

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

PaulS

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.

The Question

#9
Nov 07, 2009, 11:41 pm Last Edit: Nov 07, 2009, 11:42 pm by mtktm Reason: 1
well this is what I have now :p

*#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
  • = analogRead(potpins
  • )
     }
      delay(18);                           // waits for the servo to get there
    }

PaulS

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

CaptainObvious

You're almost there! :D

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

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

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.



PaulS

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

CaptainObvious

#14
Nov 07, 2009, 11:57 pm Last Edit: Nov 07, 2009, 11:57 pm by thoed Reason: 1
Code: [Select]
#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.

Go Up