Created
October 5, 2014 16:53
-
-
Save mattbrailsford/56ffd417c80abbe8a3d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "L9110.h" | |
L9110 bridge; | |
void setup() | |
{ | |
// Begin the bride, setting pins. Pins must be 4 of A0, A1, A4, A5, A6, A7, D0 and D1 | |
bridge.begin(A0,A1,D0,D1); | |
} | |
void loop() | |
{ | |
// Speed up A | |
for(int i = 0; i < 100; i += 20) | |
{ | |
bridge.setSpeed('A', i); | |
delay (2000); | |
} | |
// Slow down A | |
for(int i = 100; i > 0; i -= 20) | |
{ | |
bridge.setSpeed('A', i); | |
delay (2000); | |
} | |
// Speed up B | |
for(int i = 0; i < 100; i += 20) | |
{ | |
bridge.setSpeed('B', i); | |
delay (2000); | |
} | |
// Slow down B | |
for(int i = 100; i > 0; i -= 20) | |
{ | |
bridge.setSpeed('B', i); | |
delay (2000); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "L9110.h" | |
#include "spark_wiring.h" | |
/////////////////////////////////////////// | |
/// Public | |
////////////////////////////////////////// | |
/// Constructor | |
L9110::L9110() { } | |
/// Destructor | |
L9110::~L9110() { } | |
/// Begin | |
void L9110::begin(int fwdPinA, int bwdPinA, int fwdPinB, int bwdPinB) | |
{ | |
_fwdPinA = fwdPinA; | |
_bwdPinA = bwdPinA; | |
_fwdPinB = fwdPinB; | |
_bwdPinB = bwdPinB; | |
_initMotor(_fwdPinA, _bwdPinA); | |
_initMotor(_fwdPinB, _bwdPinB); | |
} | |
/// Set motor speed from -100 to + 100 | |
void L9110::setSpeed(char motor, int speed) | |
{ | |
if(motor == 'A') | |
{ | |
_setMotorSpeed(_fwdPinA, _bwdPinA, speed); | |
} | |
else if (motor == 'B') | |
{ | |
_setMotorSpeed(_fwdPinB, _bwdPinB, speed); | |
} | |
} | |
/// Stop a motor | |
void L9110::stop(char motor) | |
{ | |
setSpeed(motor, 0); | |
} | |
/////////////////////////////////////////// | |
/// Private | |
////////////////////////////////////////// | |
/// Initialize motor pins | |
void L9110::_initMotor(int fwdPin, int bwdPin) | |
{ | |
//Set control pins to be outputs | |
pinMode(fwdPin, OUTPUT); | |
pinMode(bwdPin, OUTPUT); | |
//set motoR to run at (0/255 = 0)% duty cycle (stopped) | |
digitalWrite(fwdPin, 0); | |
digitalWrite(bwdPin, 0); | |
} | |
/// Set motor speed from -100 to + 100 | |
void L9110::_setMotorSpeed(int fwdPin, int bwdPin, int speed) | |
{ | |
int throttle = map(abs(speed),0,100,0,255); | |
if(speed > 0) | |
{ | |
analogWrite(fwdPin, throttle); | |
digitalWrite(bwdPin, 0); | |
} | |
else if(speed < 0) | |
{ | |
digitalWrite(fwdPin, 0); | |
analogWrite(bwdPin, throttle); | |
} | |
else | |
{ | |
digitalWrite(fwdPin, 0); | |
digitalWrite(bwdPin, 0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef L9110_h | |
#define L9110_h | |
class L9110 | |
{ | |
public: | |
L9110(); | |
~L9110(); | |
void begin(int fwdPinA, int bwdPinA, int fwdPinB, int bwdPinB); | |
void setSpeed(char motor, int speed); // Set the speed of a selected motor, range: -100 to +100 | |
void stop(char motor); | |
private: | |
int _motoSpeedA; | |
int _motoSpeedB; | |
int _fwdPinA; | |
int _bwdPinA; | |
int _fwdPinB; | |
int _bwdPinB; | |
void _initMotor(int fwdPin, int bwdPin); | |
void _setMotorSpeed(int fwdPin, int bwdPin, int speed); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment