I am aware that the servo lib can only control 12 servos.
However, if I
create 20 servo objects
detach the servos after reaching positions (I am aware they become unpowered, that is no problem for me)
and make sure that no more than 12 servos are ever attached at the same moment / make sure that there are no more than 12 servos moving at the same time
Can I than use the servo library to control all 20 servos?
For instance, I have a ServoSweep library. Every ServoSweep object has a private Servo object. The library slowly turns a servo, 1 degree per time interval. When a servo is in position, the library detaches the pin.
const int nServo = 20 ;
const int maxServo = 12 ;
ServoSweep servo[ nServo ] ; // every ServoSweep object uses one private Servo object.
void moveServos()
{
uint8_t activeServo = 0 ;
for( int i = 0 ; i < 20 ; i ++ )
{
if( servo[i].sweep() ) // sweep detaches and attaches the servo object to a pin when needed
{ // if sweep returns true, the servo is moving and thus attached
// if sweep retuns false, the servo is not moving and thus detached
if( ++ activeServo == maxServo ) return ; // if 12 servo motors are attached, the remaining ones will have to wait
}
}
}
Well for the fun of it, I am copying the content of my servoSweep library in that online software test thingy as we speak.
I think for the first test I will let all motors rotate 90 degrees in lets say 1 seconds
I will toggle the states (state being either the lower or the upper limit) of each servoSweep object with a different interval. The interval times will be doubled every time. So on a given moment more than 12 servo's have to move at the same moment.
How do I exchange that arduino mega for an uno? I clicked on the + but I am not seeing an uno anywere. I have not used wokwi before ..
When you need more than a couple of servos, then it's time to move to a PCA9685 servo board.
Two of those boards can do 32 servos, and that only takes two Arduino pins.
Leo..
Ok it seems this method does not work. motor 13 and higher will not show any movement whatsover. That is consistent with your library explanation.
I am however convinced now that it can be done.
With the following KISS code example I used a single servo object to control 2 servo's by simply detaching the first pin and attaching an other pin. I knew this would work, but I still tested it
The next thing I want to do, is to alter my servoSweep library by using a pointer to a servo object instead of the private servo object. Than I create an array of 12 servo objects and I dynamically allocate any available/detached servo object of that array to one of my 20 servoSweep objects.
It will propably take some time, before it is finished. But it sounds completely viable.
You could use an I2C based pca9685 module
I know all about that. I have also seen that you can drive 10 servo's using 2 IO pins and a decade counter, but for more than one reason (self made PCB to name one). I really want a single atmega 328 to drive 16 servo's without additional hardware like a pca9685.