Hardware Reference
In-Depth Information
}
//Stops motor
void brake ()
{
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
digitalWrite(EN, HIGH);
}
Note that at the beginning of each function the EN pin is always set low, and
then the MC1 and MC2 pins (Motor Control pins) are adjusted. When that is
done, the current flow can be reenabled. To vary the speed, just use the same
technique you did before. By using PWM, you can change the duty with which
the EN pin is toggled, thus controlling the speed. The rate variable must be
between 0 and 255. The main loop takes care of making the right rate from the
input potentiometer data.
Next, consider the main program loop:
void loop()
{
val = analogRead(POT);
//go forward
if (val > 562)
{
velocity = map(val, 563, 1023, 0, 255);
forward(velocity);
}
//go backward
else if (val < 462)
{
velocity = map(val, 461, 0, 0, 255);
reverse(velocity);
}
//brake
else
{
brake();
}
}
In the main loop, the potentiometer value is read, and the appropriate func-
tion can be called based on the potentiometer value. Recall that analog inputs
Search WWH ::




Custom Search