Hardware Reference
In-Depth Information
Make other simple parameters variables. Again, this makes it easy to update them.
When creating this test, I found that the PWM frequency to drive the motors needed
to be relatively low to get over the kickback shown in Figure 9-13 . I also found that
I needed to get up to about 70 percent duty cycle for my circuit to reliably start the
motors turning.
Use a simple variable such as state to keep track of the test phase. This is used in
a switch statement to jump to the code to configure for that test phase and up-
dated after configuring for the current phase in order to select the next phase. Note
that the next phase isn't entered until after a two-second delay, as specified in the
call to setTimeout() .
Perform the initial setup of all the pins.
The first time a PWM pin is used, it is configured with the update frequency. It is
important to set this just once to the right frequency, because other PWM channels
might use the same PWM controller, and attempts to reset the PWM frequency
might fail. The pinMode() function doesn't have an argument for providing the
update frequency, so use the analogWrite() function, instead. You can review
using the PWM in Recipe 4.1 .
updateMotors() is the test function for the motors and is defined after all the
setup and initialization code. The code calls this function every two seconds using
the setTimeout() JavaScript function. The first call is used to prime the loop.
The call to console.log() was initially here to observe the state transitions in
the debug console, but it was replaced with the updateLEDs() call. Using the
USER LEDs makes it possible to note the state transitions without having visibility
of the debug console. updateLEDs() is defined later.
 
 
 
 
Search WWH ::




Custom Search