subject really says it all. i have an arduino uno, and i need to run 5 servos with one potentiometer each (using arduino knob). One works fine, but when i try to add a second knob to the sketch, it wont work, saying "so and so previously defined"- for example, "error: redefinition of 'int potpin". when i add the second knob, i change the name of the servo, change the pins, change everything that should be changed, yet obviously im still doing something wrong.
seeing as how i am still lost, i am going to go ahead and say that i have no idea what im doing. im sure its possible to load multiple sketches onto my board- but how, i cant figure out. i feel like ive tried everything... also, when i have two knob sketches both open in tabs, and i hit verify, i sitll get the "redefinition of___" error.
wow, thank you a lot zoomkat, that works. i am incredibly new to arduino, so thank you both for your help. i do believe i have it now, thanks!
Just to clarify on the part you crossed out above.... you can't have more than one sketch in the Arduino board, and that's why you have to have lines like these "duplicates" but with eg the 1's and 2's added:
did that. also, i think i fixed the "expected blah blah" error. everything seems to be working fine, but ill hold off on celebrating this time for a bit....
heres my code:
//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
int potpin1 = 0; //my pot pin
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;
int newval1, oldval1;
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;
void setup()
{
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
myservo2.attach(4);
myservo2.attach(5);
myservo2.attach(6);
Serial.println("testing dual pot servo");
}
void loop()
{
newval1 = analogRead(potpin1);
newval1 = map(newval1, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){
myservo1.write(newval1);
Serial.print("1- ");
Serial.println(newval1);
oldval1=newval1;
}
newval2 = analogRead(potpin2);
newval2 = map(newval2, 0, 1023, 0, 179);
if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){
myservo2.write(newval2);
Serial.print("2- ");
Serial.println(newval2);
oldval2=newval2;
}
delay(50);
{
}
newval3 = analogRead(potpin3);
newval3 = map(newval3, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){
myservo1.write(newval3);
Serial.print("3- ");
Serial.println(newval3);
oldval3=newval3;
}
{
}
newval4 = analogRead(potpin4);
newval4 = map(newval4, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){
myservo1.write(newval4);
Serial.print("4- ");
Serial.println(newval4);
oldval4=newval4;
}
{
}
newval5 = analogRead(potpin5);
newval5 = map(newval5, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval5 > (oldval5+2)){
myservo1.write(newval5);
Serial.print("5- ");
Serial.println(newval5);
oldval5=newval5;
}
}
all seems to be well, no errors compiling or uploading. im testing it out now, and it seems to be good as well.
Is it possible / recommanded to use arrays (of servos, of pins, of variables...) to manage multiple systems in one sketch ?
Then you can have myservo[indx].attach(potpin[indx]); and so on, heavily using for(){} loops ?
It is not going to work if you assign potpins 1 to 5 to pins 0,1,2,3,4
and then assign servos 1 to 5 to pins 2,3,4,5,6
In your code, you are trying to do two different things with pins 2,3 and 4.
Actually, this might work if the compiler distinguishes between analog and digital pins for you.
It's still not very good looking code. You can refer to the analog pins as A0, A1 etc to avoid
this sort of confusion.
yea, ive changed some values since i posted that code.
i have another question however. since this will be controlling a robot arm, i want the movements to be precise. is there a way to implement the writemicroseconds() function into this code?
NHS_SO:
yea, ive changed some values since i posted that code.
i have another question however. since this will be controlling a robot arm, i want the movements to be precise. is there a way to implement the writemicroseconds() function into this code?
You might try mapping and the us range something like below. For better control of the pot shaft rotation, consider using a large knob (plastic jar lid or bottle cap) on the shaft for turning similar to the bottom pix.