Adding more servos

I've been trying to add more servos to this code and it doesn't work.
I have 6 servos and 6 maxbotix range finder:
Maxbotix- analog In Pin 0 to 5
Servos - Digital pin 2 to 7
And the analog pins (maxbotix) outputs the numbers to max jitter.

I got here so far- but when I get to void move and repeat everything- it goes back to moveTo- int redefinition.
Can someone please help me :wink:

#include <Servo.h>

Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int pos = 0;
int pos2 = 0;

void setup() {

Serial.begin(9600);
myservo0.attach(2); // analog 0
myservo1.attach(3); // analog 1
myservo2.attach(4); // analog 2
myservo3.attach(5); // analog 3
myservo4.attach(6); // analog 4
myservo5.attach(7); // analog 5
}

void loop() {

// servo 0 analog 0
if (analogRead(0)< 110){
Serial.print (analogRead(0));
Serial.println();
myservo0.write(170);

}
else
{
Serial.print (analogRead(0));
Serial.println();
myservo0.write(10);

}

// servo 1 analog 1

if (analogRead(1)< 110){
Serial.print (analogRead(1));
Serial.println();
myservo1.write(170);

}
else
{
Serial.print (analogRead(1));
Serial.println();
myservo1.write(10);

}

// servo 2 analog 2

if (analogRead(2)< 110){
Serial.print (analogRead(2));
Serial.println();
myservo2.write(170);

}
else
{
Serial.print (analogRead(2));
Serial.println();
myservo2.write(10);

}

// servo 3 analog 3

if (analogRead(3)< 110){
Serial.print (analogRead(3));
Serial.println();
myservo3.write(170);

}
else
{
Serial.print (analogRead(3));
Serial.println();
myservo3.write(10);

}

// servo 4 analog 4

if (analogRead(4)< 110){
Serial.print (analogRead(4));
Serial.println();
myservo4.write(170);

}
else
{
Serial.print (analogRead(4));
Serial.println();
myservo4.write(10);

}

// servo 5 analog 5

if (analogRead(5)< 110){
Serial.print (analogRead(5));
Serial.println();
myservo5.write(170);

}
else
{
Serial.print (analogRead(5));
Serial.println();
myservo5.write(10);

}
}

// move to the given position
// degreesPerStep of 4 moves 160 degrees in a little under a second
// degreesPerStep of 1 moves 160 degrees in around 3.2 seconds
void moveTo(int newPosition){
int degreesPerStep = 1; // decrease this to slow movement

int currentPos = myservo0.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 0){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myservo0.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myservo0.write( currentPos);
delay(20);
}
}
}

I also tried this- but it didnt work

/*

  • Arduino2Max
  • Send pin values from Arduino to MAX/MSP
  • Arduino2Max.pde

  • This version: .4, October 2007

  • Copyleft: use as you like
  • by Daniel Jolliffe
  • Based on a sketch and patch by Thomas Ouellet Fredericks tof.danslchamp.org

*/

#include <Servo.h>

Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;

int pos = 0;
int pos2 = 0;

int x = 0; // a place to hold pin values
//int ledpin = 13;

void setup()
{
Serial.begin(9600); // 115200 is the default Arduino Bluetooth speed
digitalWrite(13,HIGH); ///startup blink
delay(600);
digitalWrite(13,LOW);
pinMode(13,INPUT);
myservo0.attach(9); //analog 0
myservo1.attach(6); //analog 1
myservo2.attach(5); //analog 2
myservo3.attach(3); //analog 3
}

void loop()
{

if (Serial.available() > 0){ // Check serial buffer for characters

if (Serial.read() == 'r') { // If an 'r' is received then read the pins

for (int pin= 0; pin<=5; pin++){ // Read and send analog pins 0-5
x = analogRead(pin);
sendValue (x);
}
/*
for (int pin= 2; pin<=13; pin++){ // Read and send digital pins 2-13
x = digitalRead(pin);
sendValue (x);
}
*/
Serial.println(); // Send a carriage returnt to mark end of pin data.
delay (5); // add a delay to prevent crashing/overloading of the serial port

}

}
}

void sendValue (int x){ // function to send the pin value followed by a "space".
Serial.print(x);
Serial.print(32, BYTE);
if (x< 110){
moveTo(170);
}
else{
moveTo(10);
}
}

// move to the given position
// degreesPerStep of 4 moves 160 degrees in a little under a second
// degreesPerStep of 1 moves 160 degrees in around 3.2 seconds
void moveTo(int newPosition){
int degreesPerStep = 4; // decrease this to slow movement
if (x==0){
int currentPos = myservo0.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 0){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myservo0.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myservo0.write( currentPos);
delay(20);
}
}
}

else if (x==1){
int currentPos = myservo1.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 1){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myservo1.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myservo1.write( currentPos);
delay(20);
}
}
}
else if (x==2){
int currentPos = myservo2.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 1){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myservo2.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myservo2.write( currentPos);
delay(20);
}
}
}
else if (x==3){
int currentPos = myservo3.read();
int movement = newPosition - currentPos; // the number of degrees to move
if(movement < 1){
while(currentPos > newPosition){
currentPos = currentPos - degreesPerStep;
myservo3.write( currentPos);
delay(20);
}
}
else{ // movement is >0 )
while(currentPos < newPosition){
currentPos = currentPos + degreesPerStep;
myservo3.write( currentPos);
delay(20);
}
}
}
}

Thankssss, Larissa ::slight_smile:

Could you put the code in between [ code] [ /code] tags?
It makes it a lot more comfortable to go through the code...

#include <Servo.h> 

Servo myservo0; 
Servo myservo1; 
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int pos = 0;
int pos2 = 0;

void setup()  { 
 
 Serial.begin(9600); 
 myservo0.attach(2); // analog 0
  myservo1.attach(3); // analog 1
   myservo2.attach(4); // analog 2
    myservo3.attach(5); // analog 3
      myservo4.attach(6); // analog 4
         myservo5.attach(7); // analog 5
} 

void loop()  { 
 
// servo 0 analog 0
 if (analogRead(0)< 110){
   Serial.print (analogRead(0));
   Serial.println();
   myservo0.write(170);
  
 }
 else
 {
   Serial.print (analogRead(0));
   Serial.println();
   myservo0.write(10);            

 }
 
 // servo 1 analog 1
 
  if (analogRead(1)< 110){
   Serial.print (analogRead(1));
   Serial.println();
   myservo1.write(170);

 }
 else
 {
   Serial.print (analogRead(1));
   Serial.println();
   myservo1.write(10);            
 
 }
 
 // servo 2 analog 2
 
  if (analogRead(2)< 110){
   Serial.print (analogRead(2));
   Serial.println();
   myservo2.write(170);

 }
 else
 {
   Serial.print (analogRead(2));
   Serial.println();
   myservo2.write(10);            

 }
 
 // servo 3 analog 3
 
  if (analogRead(3)< 110){
   Serial.print (analogRead(3));
   Serial.println();
   myservo3.write(170);
 
 }
 else
 {
   Serial.print (analogRead(3));
   Serial.println();
   myservo3.write(10);            

 }
  
 // servo 4 analog 4
 
  if (analogRead(4)< 110){
   Serial.print (analogRead(4));
   Serial.println();
   myservo4.write(170);
 
 }
 else
 {
   Serial.print (analogRead(4));
   Serial.println();
   myservo4.write(10);            

 }
 
 // servo 5 analog 5
 
  if (analogRead(5)< 110){
   Serial.print (analogRead(5));
   Serial.println();
   myservo5.write(170);
 
 }
 else
 {
   Serial.print (analogRead(5));
   Serial.println();
   myservo5.write(10);            

 }
}

// move to the given position
// degreesPerStep of 4 moves 160 degrees in a little under a second
// degreesPerStep of 1 moves 160 degrees in around 3.2 seconds
void moveTo(int newPosition){
 int degreesPerStep =  1;  // decrease this to slow movement

 int currentPos = myservo0.read();
 int movement = newPosition - currentPos; // the  number of degrees to move
 if(movement < 0){
   while(currentPos > newPosition){
     currentPos = currentPos - degreesPerStep; 
     myservo0.write( currentPos);
     delay(20);
   }
 }
 else{ // movement is >0 )
   while(currentPos < newPosition){
     currentPos = currentPos + degreesPerStep; 
     myservo0.write( currentPos);
     delay(20);
   }
 }
}

I also tried this- but it didnt work

/*  
*  Arduino2Max
*  Send pin values from Arduino to MAX/MSP
*  
*  Arduino2Max.pde
*  ------------  
*  This version: .4, October 2007
*  ------------
*  Copyleft: use as you like
*  by Daniel Jolliffe
*  Based on a sketch and patch by Thomas Ouellet Fredericks  tof.danslchamp.org
*  
*/

#include <Servo.h> 

Servo myservo0; 
Servo myservo1; 
Servo myservo2;
Servo myservo3;

int pos = 0;
int pos2 = 0;

int x = 0;                              // a place to hold pin values
//int ledpin = 13;

void setup()
{
 Serial.begin(9600);               // 115200 is the default Arduino Bluetooth speed
 digitalWrite(13,HIGH);              ///startup blink
 delay(600);
 digitalWrite(13,LOW);
 pinMode(13,INPUT);
   myservo0.attach(9);   //analog   0
  myservo1.attach(6);     //analog  1
   myservo2.attach(5);      //analog  2
    myservo3.attach(3);        //analog 3
}



void loop()
{ 

if (Serial.available() > 0){         // Check serial buffer for characters
       
   if (Serial.read() == 'r') {       // If an 'r' is received then read the pins
   
for (int pin= 0; pin<=5; pin++){      // Read and send analog pins 0-5
   x = analogRead(pin);
   sendValue (x);
   }
/*
for (int pin= 2; pin<=13; pin++){     // Read and send digital pins 2-13
   x = digitalRead(pin);
   sendValue (x);
   }
*/ 
   Serial.println();                 // Send a carriage returnt to mark end of pin data. 
   delay (5);                        // add a delay to prevent crashing/overloading of the serial port
 
 }

}
}

void sendValue (int x){              // function to send the pin value followed by a "space". 
Serial.print(x);
Serial.print(32, BYTE); 
 if (x< 110){
   moveTo(170);          
 }
 else{
   moveTo(10);          
 }
}

// move to the given position
// degreesPerStep of 4 moves 160 degrees in a little under a second 
// degreesPerStep of 1 moves 160 degrees in around 3.2 seconds 
void moveTo(int newPosition){
 int degreesPerStep =  4;  // decrease this to slow movement
if (x==0){
 int currentPos = myservo0.read();
 int movement = newPosition - currentPos; // the  number of degrees to move
 if(movement < 0){
   while(currentPos > newPosition){
     currentPos = currentPos - degreesPerStep;  
     myservo0.write( currentPos);
     delay(20);
   }
 }
 else{ // movement is >0 )
   while(currentPos < newPosition){
     currentPos = currentPos + degreesPerStep;  
     myservo0.write( currentPos);
     delay(20);
   }
 }
}


else if (x==1){
 int currentPos = myservo1.read();
 int movement = newPosition - currentPos; // the  number of degrees to move
 if(movement < 1){
   while(currentPos > newPosition){
     currentPos = currentPos - degreesPerStep;  
     myservo1.write( currentPos);
     delay(20);
   }
 }
 else{ // movement is >0 )
   while(currentPos < newPosition){
     currentPos = currentPos + degreesPerStep;  
     myservo1.write( currentPos);
     delay(20);
   }
 }
}
else if (x==2){
 int currentPos = myservo2.read();
 int movement = newPosition - currentPos; // the  number of degrees to move
 if(movement < 1){
   while(currentPos > newPosition){
     currentPos = currentPos - degreesPerStep;  
     myservo2.write( currentPos);
     delay(20);
   }
 }
 else{ // movement is >0 )
   while(currentPos < newPosition){
     currentPos = currentPos + degreesPerStep;  
     myservo2.write( currentPos);
     delay(20);
   }
 }
}
else if (x==3){
 int currentPos = myservo3.read();
 int movement = newPosition - currentPos; // the  number of degrees to move
 if(movement < 1){
   while(currentPos > newPosition){
     currentPos = currentPos - degreesPerStep;  
     myservo3.write( currentPos);
     delay(20);
   }
 }
 else{ // movement is >0 )
   while(currentPos < newPosition){
     currentPos = currentPos + degreesPerStep;  
     myservo3.write( currentPos);
     delay(20);
   }
 }
}
}

Sorry :-[

For future reference, it is neater to just modify your original post, rather than make a new reply, which doesn't make it any neater. You can just highlight your code in the original post and click the code button.

if (x==[glow]3[/glow]){
 int currentPos = myservo[glow]3[/glow].read();

This screams "array" and "for" loop.