Need help coding a loop

I'm using the Arduino and a 12-bit DAC to create waveforms to drive a laser galvanometer system.

I'm having trouble writing a loop using integer math that will spit out 10 numbers and slowly make them all tend to 2048 - the 'middle' value of the DAC and 0v in the output signal.

This loop should take about 400 iterations, and with each pass calculate 10 values to be sent to the DAC.

i.e:

Starting iteration:

0 455 910 1365 1820 2275 2730 3185 3640 4095

Ending iteration:

2048 2048 2048 2048 2048 2048 2048 2048 2048 2048

(or as close as can be eg. 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 ).

I can successfully make an 11 value sequence work, but 10 is proving to be tricky...

Anyone know how to do this?

Steve

Steve, presumably, the problem is integer rounding errors. How about doing the calculation as a long and multiplying by whatever increased precision you need, say 1000:

long step = iter * (1000 * (2048 – (long)n)) / 400 ; step = step / 1000;

does that help?

Could you post your code or at least the relevant section of it. I don't understand how it can work for 11 values but not for 10 when as far as I can tell from your description each number is independent of the other 9 (or 10). That makes me think it is likely to be a code issue rather than an algorithmic one. So please post the code and I'll be happy to help.

Charlieb

Hi Charlieb, I don't have any code to post. I am not even sure I understand what problem you are trying to solve.

I have assumed that you want to iterate 10 numbers from various starting values towards 2048 in 400 steps. If that is the case then each step is 1/400 of the difference between the starting and ending values, and if so, the issue is how to do the calculation smoothly using integer math.

If my assumption is correct, the value of a step for a number n on a given iteration is: step = iter * ((2048 – n) / 400) ;

But perhaps I am not clear on what you are trying to do.

Does it have to be actual integer math, or does it just have to produce an integer result?

Is the progression supposed to be linear or do you have in mind something else?

I’ve got a galvo and would love to figure out how to get the arduino to drive it, It’s a really old cheap galvo so I’m not worried about best quality, just would love to see the darn thing work.

Is it possible to just use the PWM outputs to drive it?