Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
Good info, but you should make it clear in the tutorial that this is useful for PWM that does not care too much about the base freqency and can use the whole range of 0-100% duty cycles. It is not suitable for servo control since it is not the correct base freqency and with the 8 bit timers, does not have sufficient granularity for the 5-10% range that servos require. For things like your LED dimmer and direct H-bridge motor control, it is very useful, though.
/** Interpolate between two numbers* value - the current value to be used* minVal - the minimum that 'value' can be* maxVal - the maximum that 'value' can be* minRtn - the return value if 'value = minVal'* maxRtn - the return value if 'value = maxVal'* return a value in the range minRtn to maxRtn*/int interpolate(int value, int minVal, int maxVal, int minRtn, int maxRtn){ long int lRtnRange = maxRtn - minRtn; long int lValRange = maxVal - minVal; long int lRelVal = value - minVal; lRtnRange = minRtn + ( lRtnRange * lRelVal / lValRange ); return (int)lRtnRange;}