#include <Servo.h>
const byte servoPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const byte servoCount = sizeof servoPins / sizeof * servoPins;
const int offset[servoCount] = {0, 30, 60, 0, 30, 60, 0, 30, 60}; // offest index in the animation
Servo servos[servoCount];
const int animation[] = { // sinusoidal animation starting at 90°
90, 91, 93, 94, 96, 97, 99, 100, 102, 104, 105, 107, 108, 110, 111, 113, 114, 116,
117, 119, 120, 122, 123, 125, 126, 128, 129, 130, 132, 133, 135, 136, 137, 139, 140, 141,
142, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
162, 163, 164, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 172, 173, 174, 174, 175,
175, 176, 176, 176, 177, 177, 178, 178, 178, 178, 179, 179, 179, 179, 179, 179, 179, 179,
180, 179, 179, 179, 179, 179, 179, 179, 179, 178, 178, 178, 178, 177, 177, 176, 176, 176,
175, 175, 174, 174, 173, 172, 172, 171, 170, 170, 169, 168, 167, 167, 166, 165, 164, 163,
162, 161, 160, 159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 147, 146, 145, 144,
142, 141, 140, 139, 137, 136, 135, 133, 132, 130, 129, 128, 126, 125, 123, 122, 120, 119,
117, 116, 114, 113, 111, 110, 108, 107, 105, 104, 102, 100, 99, 97, 96, 94, 93, 91,
90, 88, 86, 85, 83, 82, 80, 79, 77, 75, 74, 72, 71, 69, 68, 66, 65, 63,
62, 60, 59, 57, 56, 54, 53, 51, 50, 49, 47, 46, 44, 43, 42, 40, 39, 38,
37, 35, 34, 33, 32, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18,
17, 16, 15, 14, 13, 12, 12, 11, 10, 9, 9, 8, 7, 7, 6, 5, 5, 4,
4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3,
4, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35,
37, 38, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 59, 60,
62, 63, 65, 66, 68, 69, 71, 72, 74, 75, 77, 79, 80, 82, 83, 85, 86, 88,
};
const size_t animationCount = sizeof animation / sizeof * animation;
void setup() {
for (byte i = 0; i < servoCount; i++) {
servos[i].write(animation[offset[i] % animationCount]); // set initial position based on offset before attaching
servos[i].attach(servoPins[i]);
}
delay(5000); // time to see the offset at start
}
void loop() {
for (int i = 0; i < 360; i++) {
for (byte s = 0; s < servoCount; s++)servos[s].write(animation[(i + offset[s]) % animationCount]);
delay(5);
}
}