DCMotor.hpp

A DC motor generally requires a PWM (speed) signal and a direction/brake pin. DC motors don't require very accurate PWM signals so this class uses PWMOut to generate the PWM signal on any output pin via software and then uses one additional output pin for controlling the direction (fwd/reverse) of the motor. Check the requirements of your DC Motor Controller or see my Motor Controller Tutorial.

 

So for example:-

 

 OUT g_direction('B', 6);         // Use B6 as the direction pin to the motor driver
     PWMOUT g_pwm('B', 5, 50);  // Use B5 as the PWM signal to the motor driver. PWM signal is every 50ms.
     DCMOTOR g_motor(&g_pwm, &g_direction);  // Join the two lines above into a single DC motor

 

Now we can just use the methods in Motor.hpp on g_motor. ie SetSpeed(value). This class will then sort out the setting of the direction pin and the PWM signal for you.