Hardware Reference
In-Depth Information
else if(velocity <0){ // Reverse
digitalWrite(dirPin, HIGH);
analogWrite(pwmPin, 255+velocity);
}
else if(velocity >0){ // Forward
digitalWrite(dirPin,LOW);
analogWrite(pwmPin, velocity);
}
}
void Motor::setLeftMotorSpeed(int velocity)
{
//Serial.print("Set Left: ");
//Serial.println(velocity);
setMotorVel(motor1Dir, motor1PWM, -velocity);
}
void Motor::setRightMotorSpeed(int velocity)
{
//Serial.print("Set Right: ");
//Serial.println(velocity);
setMotorVel(motor2Dir, motor2PWM, -velocity);
}
Once the implementation code is in place, it is time to work on the main sketch that will use the code. To avoid
cutting and pasting the code into every sketch, we can just write #include "Motor.h" . The following sketch example
shows the code controlling the motor and using features of the library, which is much shorter and cleaner than the
original sketch in Listing 12-8.
Listing 12-10. Motor controller main sketch
.#include "Motor.h"
Motor motor;
void setup()
{
motor.setRightMotorSpeed(255);
motor.setLeftMotorSpeed(−255);
delay(500);
motor.setRightMotorSpeed(−255);
motor.setLeftMotorSpeed(255);
delay(500);
motor.setRightMotorSpeed(0);
motor.setLeftMotorSpeed(0);
}
 
Search WWH ::




Custom Search