Hardware Reference
In-Depth Information
void setup()
{
initMotorDriver();
setRightMotorSpeed(255);
setLeftMotorSpeed(−255);
delay(500);
setRightMotorSpeed(−255);
setLeftMotorSpeed(255);
delay(500);
setRightMotorSpeed(0);
setLeftMotorSpeed(0);
}
void loop()
{
//Go Forward 5 secs
setRightMotorSpeed(355);
setLeftMotorSpeed(255);
delay(5000);
//Stop
setRightMotorSpeed(0);
setLeftMotorSpeed(0);
//loop here forever.
while(1);
}
In the initial sketch, the individual control commands are combined into one function called setMotorVel :
void setMotorVel(int dirPin, int pwmPin, int velocity)
The direction is set by integer velocity, which accepts −255 through 255. If the velocity is negative, then the
opposite direction is enabled.
Listing 12-8 code defines the pins that are mapped to control the chip. This defines a motion function that
controls all the options, and there are helper functions that make it easy to initiate left and right control of the
robot. Now, we are ready to make the library. These functions will move into their own header file, .h and their own
implementation file, .cpp . At this step, we want to create the appropriate project structure.
Listing 12-9. Motor controller header file Motor.h
#ifndef Motor_h
#define Motor_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
 
Search WWH ::




Custom Search