ebay.fr triac pid/triac s
There are various ways to do this, but here's one simple way:
First decide upon the maximum rate at which you can toggle your mechanical relay. For example, you might choose a max rate of one toggle per second. This is the rate at which the power controller thread will run (once per second).
Next choose a time interval over which you will measure the relay's average duty cycle. For example, you might choose an averaging interval of 100 seconds. Note that you would like this to be a large multiple of the max toggle rate for best accuracy. In this case, the power controller thread must "remember" the relay states that were in effect for the last 100 seconds.
Each time the power controller thread runs, it averages the previous 100 relay states (which must always be "remembered") to determine the average amount of power applied during the last 100 seconds. So for example, if the relay was turned on for 30 of the last 100 seconds then the average applied power is 30%. During each pass through the power controller thread, the average applied power is compared to the target power demanded by the PID loop thread; if applied power is less than demanded power, the relay is switched on for the next second, and if applied is greater than demanded then the relay is switched off for the next second.
Note that the values used in the above example are good for illustrating this algorithm, but you may not want to use these values in your application.