I'm developing a long-term project for myself and am at a stage where I need to program 5 servo motors independently of each other. I am using a MEGA 2560 and wanting to use the pins 9-13 as they are PWM capable and I have other components attached to the other PWM pins.
My issue is that the motors do not respond when I control them using any of the pins up to 11. So 0-10 do not work when trying to control the motors, using the exact same code, only changing the pin number. (I understand that the TX and RX pins won't work due to the fact that I have enabled the Serial in my code, however this still does not explain the rest of the pins from 2-10.)
I appreciate any advice and tips, my simple testing code is below:
I believe that the PWMServo.h library has a problem com arduino Mega.
I tested your code in the simulator with the PWMServo.h library and
did not work.
But I tested it with Arduino UNO and it worked.
I replaced the library with the Servo.h library and your code worked with Mega.
Thank you, I've tested using the library you've suggested and now have 3 running on the PWMServo and 2 on the Servo. It's currently working so don't want to change anything and risk breaking something, thank you for your help!
You can compress the folder and try your changes, if they do not work you can restore from the compressed file to exactly where you were. I do this a lot. I use the format"filename(YYMMDD-HHMM).zip That way I do not have t remember which was the last one.
you should have no problems to change completely to the Servo library as suggested by @ruilviana!
In Paul Stoffregen's PWMServo library you'll find this
That library supports three pins of the MEGA (11,12,13) while Servo.h supports much more:
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.
/*
Forum: https://forum.arduino.cc/t/mega-2560-pwm-pins/1191076
Wokwi: https://wokwi.com/projects/381938780490308609
*/
#include <Servo.h>
// constant expression holding the number of servos wired to the MEGA
constexpr int noOfServos {8};
// constant expression storing the first pin number
// The wired pins are 6, 7, 8, ... therefore we will later
// attach the servos starting with firstServoPin (plus 1 for each further servo)
constexpr byte firstServoPin {6};
Servo myServo[noOfServos];
void setup() {
// set start angle before attaching the servos
writeToAllServos(0);
// function to attach all servos
for (int i= 0;i< noOfServos;i++){
myServo[i].attach(firstServoPin+i);
}
}
void loop() {
writeToAllServos(110);
delay(100);
writeToAllServos(120);
delay(1500);
}
// function to set all servos to the same angle
void writeToAllServos(int angle){
for (int i= 0;i< noOfServos;i++){
myServo[i].write(angle);
}
}