Pulse Distribution

The distribution code was a little tricky until I realized I could use Bresenham's formula https://en.wikipedia.org/wiki/Bresenham's_line_algorithm. To see this in action for a simpler Arduino setup. There is additional discussion about the algorithm "Modified_Bresenham's_Line_Algorithm" in Arduino Accurate Pulser

This algorithm is normally used to draw a line on a screen, but at a more fundamental level it takes a continuous formula and converts it into a discrete ("pixelated") version that minimizes the overall error at each discrete point. Each individual pixel will not match the expected continuous number contributing to the overall error. But the algorithm minimizes the total of all of those error values. In short, the smaller the overall error is, the "smoother" the line looks on the screen.

The idea then was to take the total number of pulses, divide it by the number of slots and then use Bresenham's formula to minimize the total of the error values for each slot. For example, say the total number of pulses is 100. Then 1000/100 = 10.0 and so every 10th slot would have a 1 in it, the rest would have 0's. In this case it is a nice fit, but oddball numbers aren't as lucky. For example, say the total pulses is 993. Then 1000/993 = 1.000705. So most of the slots are 1, but every so often there is a 0 (for a total of 7 in the entire array).

The algorithm required some minor changes to work for my purposes, but eventually it did distribute the pulses evenly across the 1000 slots. It worked fine whether the total number was low (e.g. 1, 2, 3...) or high (e.g. ..., 997, 998, 999). I double-checked against smaller arrays (e.g. 10 elements) and it worked fine there as well. And finally I double-checked against arrays with a prime number of elements (i.e. the worst case) and it worked fine again.

- John Arrizza