# Arduino rounding problem

I have been working on a project in which I use a model aircraft transmitter and receiver to control servos. I am not plugging the servos directly into the receiver because I want to reverse/expo the servos without buying a new transmitter. The outputs from the receiver fluctuate a little bit and this causes the servo to be very jittery. Is there a way to round the microsecond output (1000-2000 microseconds) to the nearest 10? 1400 1410 1420 instead of 1401 1407 1417.

Here are some of the outputs from my receiver.

1518 1540 1521 1540 1518 1537 1515 1534 1472 1469 1515

I tried using the round() function but I think that it is only for decimals. I also was thinking of taking the last 10 outputs from the receiver and averaging them but I am unsure how to do this. Any help is much appreciated. THANKS!

Example to round to the nearest ten:

``````   int i=some_integer;
i = (i+5)/10;
i = 10*i;
``````

Thanks!

Rounding like that will give very jerky servos. The steps between each position will be noticeable. If it's close enough within +/-5 units, then you really won't notice that jitter in the output and it may even help you.

Averaging over time can be used. You can use an array of the last 10 values received, with an index pointing to the last one. When you get a new one, advance the index (wrap around to the beginning if required) and save the value in that position. To extract the averaged value, add up all the items in the array and divide by 10.

Another way of averaging without using so much storage is the Infinite Impulse Response (IIR) filter. The theory behind this can be quite complex but the simplest form is to store the previous (averaged) value. Each time you get a new reading, add 9/10 of the previous and 1/10 of the new value. You do need to be careful with rounding errors as the naieve way of doing this with integers will lose lots of precision.

The jitter may be a power supply problem. Always power servos with a separate supply (connecting the grounds) and budget one ampere per servo for as many servos as will move at one time.

Why did you cross post! this is really bad practice. PLEASE NEVER DO THAT AGAIN.

I've used a running average to smooth data

something like

``````    ave2 += altitude;
ave2 *= (1-1/aveLen);
ave3 = ave2/(aveLen-1);
``````

this was for altimeter, as you may guess..

aveLen is an integer - the number of samples over which to average.

ave2 and ave3 are floats.

regards

Allan