/*
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);
}
}