VNH5019 Shield Demo code

I’m new here and browsed through the posts before posting my question :wink:

The Demo code uses a speed range from -400 to 400 and subsequently maps the value into a -255 to 255 range by using a rule of three (i.e. speed setting * 51/80).

Anyone knows why it was done in that way instead of simply using -255 to 255 for the speed range?


Hello, Robert.

We use a value of 400 so we can take advantage of the increased resolution of writing directly to the Timer 1 registers that affect the duty cycle of the PWM signal generated. (With the 20 kHz PWM frequency we have configured, the duty cycle can be specified in a range from 0 to 400.) This happens if the Arduino being programmed uses either the ATmega 168, 328P,328PB, or 32U4 microcontroller. If the Arduino programmed does not have one of those aforementioned AVR microcontrollers, then the speed setting is mapped to a value between 0 and 255 so that the library can use analogWrite() to generate the PWM signal.