so, I keep having trouble with figuring out my servos. I'm trying to do a project with 8 Servos pressing buttons in a certain sequence in a loop. Servos are small metal gear ones off of amazon around 15€, (https://www.amazon.de/dp/B0BHY4H1KN?psc=1&ref=ppx_yo2ov_dt_b_product_details) so not the cheapest ones either. I'm powering them separately through USB, measured it at 5.1 V. Arduino is an Arduino Micro.
the code is simply whenever a certain button/servo is called, in a subroutine it writes the position at which it presses the button, retracts after about half a second, subroutine ends and it goes to the next button in the sequence.
movement delta is 20°, always centered close to 90°, so none reach anywhere near the 0-180° limits. there is never any struggling sound once they reach their position.
It runs fairly well until always at some point after some time one or more servos stop turning and don't start up again even when the code loops or worse go into continuous turning (which isn't called a single time in the code). it's not the same servo every time plus in the cases of continuous movement, sometimes it's clockwise, sometimes counterclockwise. also it happens always at different points in the loop. when I then power everything down and then initialize again from the top most of the time it starts up again, and the same problem occurs again after some runtime. but it has also happened that a servo died completely and had to be replaced.
movement is executed almost always one after the other, never more than 2 at the same time. I have also tried the code with and without detaching in between use which didn't make a difference.
here's the code, shortened by a lot to just one of the button pressing routines, every subroutine does exactly the same, just abbreviated it to one of them as an example, because the whole thing would be way too long for this.
#include <Servo.h>
Servo a;
int pin = 9;
int idleposition = 103;
int pressingposition = 123;
void setup() {
a.attach(pin);
a.write(idleposition);
delay(2000);
}
void loop() {
pressbutton(x);
pressanotherbutton(x);
.
.
. //as mentioned it does a long sequence of button presses
. //one after the other, and then loops the same sequence
}
void pressbutton(int r) {
for(r; r>0; r--){
a.write(pressingposition);
delay(400);
a.write(idleposition);
delay(350);
}
}
void pressanotherbutton(int r) {
//looks exactly the same as the one above
//just with the respective servo object
//and the respective positions initialized
//at the top like the mentioned ones
}
again, abbreviated for ease of reading. but functionally this is literally all any of the servos are doing.
this is the first time I've ever worked with servos so it's highly likely there's something I don't know, but it just seems weird to me how they keep failing seemingly arbitrarily. I can't find a pattern so I'd appreciate some suggestion on what to look out for, thanks.
any other information which might be of interest for figuring this out, please just ask, obviously!