Markos,
With the approach you describe I would expect that you are having problems from phase drift. The phase and frequency of the mains vary slightly from nominal, and the crystal on the mbed has a tolerance in the tens of parts per million. It doesn't take many seconds before you windup with phase errors which are a substantial fraction of the mains period and which continiously progress as time goes on.
What you might try instead is to have the task handled in the background by interrupts as follows -
1. Have two global integers - 'Delay' and 'Count'.
2. Run a 'clipped' version of the mains signal into a digital input.
Have that input fire an interrupt on both the rising and falling
edges. Attach a routine to the interrupt that just copies the
'Delay' value to the 'Count'.
3. Attach a routine to your 200 uS timer that operates as follows -
a. Set a local flag to 'False'.
b. If 'Count' is not negative then decrement 'Count', and if the
result of the decrement is negative then set the local flag to
'True'.
c. Set the digital output that fires the triac according to the
value of the local flag.
Giving the two interrupts equal priorities should prevent any 'race' conflicts over 'Count'. 'C' code experts can reduce steps 3a-c to a single assignment statement using an '&&' expression :-).
The above should generate trigger pulses 200 uS wide at the desired delay (in 200 uS steps) while tracking the mains variations (+/- 100 uS). You can run any program you want in a loop in 'main()' to vary the value of 'Delay' and thus change the triac power to the fan. For example, read a voltage from a pot and set the delay via some non-linear transformation. Another example would be to read the fan speed and adjust the delay in a feed-back control loop to acheive some desired speed.
To control the speed of an small single phase AC ventilator I try to shift Pwm pulses with respect to mains in order to fire a Triac at various phase angles. My approach is the following Sample mains using Ticker every 200_us together with time from a Timer (200 samples 40msec or two mains periods) From samples determine time when mains cross zero (0 phase angle ). Calculate a delay DT wait (DT) and program Pwmout immediately afterwards The results are not consistent Any suggestions how to control an ac motor Thank you Markos
quote