I am doing a fairly simple project to control the headlights on my motorcycle based on 2 inputs: the high/low beam switch and a phototransistor to detect whether it is day or night. The input combinations lead to 4 possible output states, each state having different parameters for the PWM on each of 2 mosfets which switch the headlight.
The phototransistor needs a low pass filter such that it won't cause state switching at night from passing under a streetlight for example. I plan to install a capacitor such that the photocell has about a 0.1s time constant, and may also require some sort of filtering over about 1 second or more in software.
I want to be able to arbitrarily set the light level that the output state switches at, so the phototransistor needs to be read by an ADC.
I want the output state to change as fast as possible with a change in the high/low switch state, so I presume an interrupt is appropriate there.
3 of the output states are easy, consisting of some constant duty cycle set to the pwm, meaning the program can do this then quickly move on polling and filtering inputs. Unfortunately, the 4th state needs to vary the duty cycle from high (~90%) to low (~50%) at 4.66 hz. Of course this can be done with a loop and delay, but then the program is busy doing that instead of polling and filtering the photocell.
I can probably hack something together, but don't quite know enough about Arduino to do this in a decent manner. Can anybody give me some advice on the best fashion to: 1)frequently poll and filter the phototransistor? 2)alter the pwm setting at 4.66 hz in state 4? 3)a clean, fast method of selecting the output state based on 2 inputs? Something like two input bit variables, then output = 10*bit1+bit2?