Hardware Reference
In-Depth Information
#define motor1Dir 7
#define motor2Dir 8
#define motor1PWM 9
#define motor2PWM 10
#define motor1Enable 11
#define motor2Enable 12
class Motor
{
public:
Motor();
void begin();
void setLeftMotorSpeed(int velocity);
void setRightMotorSpeed(int velocity);
private:
void setMotorVel(int dirPin, int pwmPin, int velocity);
};
#endif
Here is what the implementation file looks like:
#include "Motor.h"
Motor::Motor()
{
pinMode(motor1Dir, OUTPUT);
pinMode(motor2Dir, OUTPUT);
pinMode(motor1Enable, OUTPUT);
pinMode(motor2Enable, OUTPUT);
digitalWrite(motor1Enable,HIGH);
digitalWrite(motor2Enable,HIGH);
setLeftMotorSpeed(0); // make sure the motors are stopped
setRightMotorSpeed(0);
}
void Motor::setMotorVel(int dirPin, int pwmPin, int velocity)
{
if (velocity >= 255)
{
velocity = 255;
}
if (velocity <= −255)
{
velocity = −255;
}
if (velocity == 0)
{
digitalWrite(dirPin, HIGH);
digitalWrite(pwmPin, HIGH);
}
Search WWH ::




Custom Search