Controlling 10 Servos with the Audrino MegaT1280

I have been working on a project which requires 10 servos reading from 10 analogue light sensors. I have come to realised I could only have 4 servos running on my board and each servo only rotates to 90 degrees instead of the intended 180 degrees. I will upload the code here and please advise what I may do to fix this problem. Thanks

#include <Servo.h>

//Servo myservo0; // create servo object to control a servo
//Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
Servo myservo5; // create servo object to control a servo
Servo myservo6; // create servo object to control a servo
Servo myservo7; // create servo object to control a servo
Servo myservo8; // create servo object to control a servo
Servo myservo9; // create servo object to control a servo
Servo myservo10; // create servo object to control a servo
Servo myservo11; // create servo object to control a servo
Servo myservo12; // create servo object to control a servo

//int Apin0 = 0;// analog pin used to connect the potentiometer
//int Apin1 = 1;
int Apin2 = 2;
int Apin3 = 3;
int Apin4 = 4;
int Apin5 = 5;
int Apin6 = 6;
int Apin7 = 7;
int Apin8 = 8;
int Apin9 = 9;
int Apin10 = 10;
int Apin11 = 11;
int Apin12 = 12;

//int val0 = 0; // variable to read the value from the analog pin
//int val1 = 0; // variable to read the value from the analog pin
int val2 = 0; // variable to read the value from the analog pin
int val3 = 0; // variable to read the value from the analog pin
int val4 = 0; // variable to read the value from the analog pin
int val5 = 0; // variable to read the value from the analog pin
int val6 = 0; // variable to read the value from the analog pin
int val7 = 0; // variable to read the value from the analog pin
int val8 = 0; // variable to read the value from the analog pin
int val9 = 0; // variable to read the value from the analog pin
int val10 = 0; // variable to read the value from the analog pin
int val11 = 0; // variable to read the value from the analog pin
int val12 = 0; // variable to read the value from the analog pin

int minThres = 0;
int maxThres = 800;

int prevVal;

boolean firstTime = true;

void setup()
{
Serial.begin(9600);
// myservo0.attach(0); // attaches the servo on pin 9 to the servo object
// myservo1.attach(1); // attaches the servo on pin 9 to the servo object
myservo2.attach(2); // attaches the servo on pin 9 to the servo object
myservo3.attach(3); // attaches the servo on pin 9 to the servo object
myservo4.attach(4); // attaches the servo on pin 9 to the servo object
myservo5.attach(5); // attaches the servo on pin 9 to the servo object
// myservo6.attach(6); // attaches the servo on pin 9 to the servo object
// myservo7.attach(7); // attaches the servo on pin 9 to the servo object
// myservo8.attach(8); // attaches the servo on pin 9 to the servo object
// myservo9.attach(9); // attaches the servo on pin 9 to the servo object
// myservo10.attach(10); // attaches the servo on pin 9 to the servo object
// myservo11.attach(11); // attaches the servo on pin 9 to the servo object
// myservo12.attach(12); // attaches the servo on pin 9 to the servo object
delay(200);
// if (firstTime)
// {
//
// //myservo0.write(1);
// Serial.println(“Test”);
// for (int i = 0; i < 150; i++)
// {
//
// //write the servos from 0 to 100 to test they are working
// Serial.println("Write to servo " + i);
// myservo2.write(i+1);
// myservo3.write(i+1);
// myservo4.write(i+1);
//// myservo5.write(i+1);
//// myservo9.write(i+1);
//// myservo10.write(i+1);
//// myservo11.write(i+1);
//
// /myservo1.write(i);
// myservo2.write(i);
// myservo3.write(i);
// myservo4.write(i);
// myservo5.write(i);
// myservo6.write(i);
// myservo7.write(i);
// myservo8.write(i);
// myservo9.write(i);
// myservo10.write(i);
// myservo11.write(i);
// myservo12.write(i);
/
//
//
//// val2 = analogRead(Apin2);
//// if (val2 < minThres)
//// {
//// minThres = val2;
//// }
//// if (val0 > maxThres)
//// {
//// maxThres = val2;
//// }
//// Serial.println(val2);
//// delay(100);
// }
//myservo2.write(179);
// // reads the value of the potentiometer (value between 0 and 1023)
//Serial.println(“done”);
//Serial.print("Min Threshold: ");
//Serial.println(minThres);
//Serial.print("Max Threshold: ");
//Serial.println(maxThres);
//
//delay(1000);
//firstTime = false;
//}

}

void loop()
{
// prevVal = val0;
// val0 = analogRead(Apin0);
// writeToServo(prevVal, val0, myservo0);
//
// prevVal = val1;
// val1 = analogRead(Apin1);
// writeToServo(prevVal, val1, myservo1);

prevVal = val2;
val2 = analogRead(Apin2);
writeToServo(prevVal, val2, myservo2);

prevVal = val3;
val3 = analogRead(Apin3);
writeToServo(prevVal, val3, myservo3);

prevVal = val4;
val4 = analogRead(Apin4);
writeToServo(prevVal, val4, myservo4);

prevVal = val5;
val5 = analogRead(Apin5);
writeToServo(prevVal, val5, myservo5);

prevVal = val6;
val6 = analogRead(Apin6);
writeToServo(prevVal, val6, myservo6);

prevVal = val7;
val7 = analogRead(Apin7);
writeToServo(prevVal, val7, myservo7);

prevVal = val8;
val8 = analogRead(Apin8);
writeToServo(prevVal, val8, myservo8);

prevVal = val9;
val9 = analogRead(Apin9);
writeToServo(prevVal, val9, myservo9);

prevVal = val10;
val10 = analogRead(Apin10);
writeToServo(prevVal, val10, myservo10);

prevVal = val11;
val11 = analogRead(Apin11);
writeToServo(prevVal, val11, myservo11);

prevVal = val12;
val12 = analogRead(Apin12);
writeToServo(prevVal, val12, myservo12);

}

void writeToServo(int prevVal, int val, Servo myServo)
{
Serial.println(val);

if ( (val < prevVal - 15) || (val > prevVal + 15))
{
// val = map(val, 30, 360, 30, 280); // scale it to use it with the servo (value between 0 and 180)
int servoVal = map (val, minThres, maxThres, 50, 180);
servoVal = constrain(servoVal, 0,180);
Serial.print(“val”);
Serial.print(val);
Serial.print(" mapped to servo angle: ");
Serial.println(servoVal);
myServo.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
else
{
//Serial.println(“not mapping”);
}
}

I have come to realised I could only have 4 servos running on my board and each servo only rotates to 90 degrees instead of the intended 180 degrees.

Why is this then? You shouldn’t have those restrictions.

I will upload the code here and please advise what I may do to fix this problem.

Ready and waiting for the code.

I am not sure it might be due to not enough power from the USB. I am planing to get a 6volts adaptor with 800ma. I am not sure if that would solve the problem.

This does not likely contribute to your 90 degree problem, but your code is absolutely SCREAMING for the use of arrays. :slight_smile:

Something like:

Servo MyServoArray[10];   //initialize

void setup()
{  
  for (int i=1;i<5;i++)
    MyServoArray[i].attach(i);
}

void loop
{
//references to MyServoArray(i)
}

It also looks like you are mapping your analog read values to a range from 50 to 180 and using that for servo values.
I would only expect 130 degrees or so of servo movement from this.

What values are you seeing when you serial.print servoval? Are you getting numbers that correspond to the servo angles you are expecting?

The angles I get from the reading is about 10-90 after changing to 0-180. It also mention map to servo angles 552 and sometimes only 0-90. I wonder why is that so? Thanks for the fast reply. In regards to array, it would be great if someone would be able to guide me on the array coding. I do not have any background on C++ programming.

ok I managed to get a close to 180 by modifying this to

int minThres = 200; int maxThres = 600

Managed to get it going with 5 servo motors in one board and since I have two that makes it 10 servos. Thanks