changing servo pwm frequency 50Hz to 250Hz ?

I found this line in Servo.h
#define REFRESH_INTERVAL    20000

Can I change it to:
#define REFRESH_INTERVAL    4000 ?

I think 4000 are 250Hz

But my base question is will this work?

I want to control 24 servos on arduino mega 2560 and 8 servos on Uno
The frequency should be 250Hz

You really need to read this. Essentially you can create PWM on any pin at any frequency through software. There are also many Google references that will discuss this. Arduino PWM is handled by the hardware timers using the output compare pins associated with each. The base frequency for Arduino PWM is about 490Hz. This is a combination of using an 8-bit timer and a prescaler pf 64 with phase correct PWM (meaning count up, count down), so 16,000,000/64/256/2 = 488.28125.

If you increase the prescaler, you slow the freq. You can speed it up by changing the TOP value in the OCRnx register. The mega2560 has 15 hardware controlled PWM pins so to get more, you need to use software. Google 'PWM on any pin' and see what pops up.

The Servo library uses one timer for 12 pins, so with 2.4ms max output pulse the minimum frequency usable drving 12 pins is about 35Hz (1s/28.8ms).

Change the SERVOS_PER_TIMER #define to 8 instead of 12 and you'll get about 52Hz

For higher performance you'd need a library that can overlap pulses instead of doing each pin round-robin, but then you have a problem scheduling ISRs so they don't clash and cause jitter..

I think the Servo library generates the servo pulses sequentially. If you have 8 servos at maximum extension (2000 microseconds) that will be 16000 microseconds to output the pulses. I don't think that can be done in 4000 microseconds. Similarly, the MEGA uses two timers and 12 servos per timer. Twelve servos at maximum extension take 24000 microseconds. I don't know what happens when the pulse times exceed the repeat rate. I suspect the pulses start again immediately.

You could re-write the Servo library to do all pulses in parallel. Then you could easily complete all the pulses 250 times per second.

@MarkT, you’re right when you use all 12 servo’s and they are all set to max pulse. But if you use less servo’s and/or smaller pulses, the interval is set by REFRESH_INTERVAL. So the interval is max(REFRESH_INTERVAL, sum(all pulses)).

So if you want 250Hz (is your servo’s even accept that) you can max have 2 servo’s at 2ms pulses max or 4 servo’s with a 1ms pulse… Not a problem if you only move (and detach after move) a single servo (or if max pulse is <2ms, 2 servo’s) at the same time!

Thank you all very much, I think I understand now alot more