Skip to content

Using the FastPWM object

willBoyd8 edited this page Mar 31, 2017 · 3 revisions

With a fastPWM object created, we can now output stuff with PWM.

To set a new output, use

void fastPWM::setVal(byte val)

val is the new output for the PWM. Note that unless the input and output ranges are the same, the val must be converted from input range (ex, 0-100) to output range (ex, 0-200). This can be done by hand, but you could also use

int fastPWM::mapValue(int value)

mapValue takes an input range number and spits out an output range number, so updating the object is as easy as calling

int newValue = demoObject.mapValue(50);
demoObject.setVal(newValue);

When setVal() is called, the value is checked against the lowWarning and highWarning values given during initialization. If the new value is smaller than lowWarning, fastPWM sets state to 1 and initialized to false. If the new value is bigger than highWarning, it sets the state to 3 and initialized to true. If the new value is somewhere in between, fastPWM sets state to 2. Finally, the OCR is updated using

void fastPWM::updateOutputCompareRegisterZero()

updateOutputCompareRegisterZero() first checks state. If state is 1, fastPWM automagically sets the output PWM to 0. If it is 3, fastPWM sets the output to Arduino's HIGH, or 100%. If state is 2, it initializes the object if it hasn't already been initialized with pwmInit() and sets the value of the output to the new value.

Clone this wiki locally