How do I evenly distribute some time?

73 Views Asked by At

Here's my problem. I have a device (ozone generator) which produces 10g of ozone per hour, or 2.77778 milligrams per second (I think). I need to be able to control the production per hour by pulsing the machine on and off to achieve an even rate.

How do I figure out the required rate of pulsing?

So if I can figure out the required amount of seconds to produce the required amount of ozone, how do I evenly distribute those seconds over an hour?

1

There are 1 best solutions below

3
On BEST ANSWER

Let's pick an example: I want to generate $3.1$ grams of ozone per hour. This is $0.31$ of the full-time output.

What we'll do is, for each second, accumulate $0.31$ into an 'accumulator' and then when that value reaches $1$, we'll emit for a second, subtract $1$, and continue. So:

  • At the start of second $0$, it's at $0$, we add $0.31$, getting $0.31$
  • At the start of second $1$, we add $0.31$, getting $0.62$
  • At the start of second $2$, we add $0.31$, getting $0.93$
  • At the start of second $3$, we add $0.31$, getting $1.24$ -- but this is above $1$! So for second $3$, we turn on the emitter, and we subtract $1$, to get $0.24$
  • We continue in this fashion: second $4$ gets us to $0.55$, second $5$ to $0.86$, second $6$ to $1.17$, so turn on the emitter for this second, so down to $0.17$.
  • We'll emit again at seconds $9, 16, 19, 22, 25, 29, 32, 35, 38, 41, 45, 48, 51, 54$, and $58$, through the first minute.

Further reading: Bresenham's Line Algorithm, Dithering


Alternatively: since we want $0.31$ of the time to be emitting, that's $1/3.226$ of the time, so we can do "wait 2.226 seconds, emit for one second" over and over and that works too.