-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#change method initmotor to begin #update example #add comment code
- Loading branch information
Showing
10 changed files
with
258 additions
and
104 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,129 @@ | ||
#include "L298N.h" | ||
#ifndef LOW_PWM | ||
#define LOW_PWM 0 | ||
#ifndef LOW_PWM | ||
#define LOW_PWM 0 | ||
|
||
L298N::L298N(){/*init constructor*/}; | ||
|
||
L298N::L298N(uint8_t ena,uint8_t n1,uint8_t n2,uint8_t n3,uint8_t n4,uint8_t enb){ | ||
/** | ||
* constructor function | ||
* params map arduino pin to pin L298N module | ||
* | ||
* ena -> ENA or PWM pin motor left | ||
* n1 -> N1 | ||
* n2 -> N2 | ||
* n3 -> N3 | ||
* n4 -> N4 | ||
* enb -> ENB or PWM pin right | ||
*/ | ||
L298N::L298N(uint8_t ena, uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4, uint8_t enb) | ||
{ | ||
this->ENA = ena; | ||
this->ENB = enb; | ||
this->N1 = n1; | ||
this->N2 = n2; | ||
this->N3 = n3; | ||
this->N4 = n4; | ||
this->N4 = n4; | ||
} | ||
|
||
/** | ||
* initial arduino pin | ||
*/ | ||
void L298N::begin() | ||
{ | ||
pinMode(ENA, OUTPUT); | ||
pinMode(ENB, OUTPUT); | ||
pinMode(N1, OUTPUT); | ||
pinMode(N2, OUTPUT); | ||
pinMode(N3, OUTPUT); | ||
pinMode(N4, OUTPUT); | ||
} | ||
|
||
void L298N::initmotor(){ | ||
pinMode(ENA,OUTPUT); | ||
pinMode(ENB,OUTPUT); | ||
pinMode(N1,OUTPUT); | ||
pinMode(N2,OUTPUT); | ||
pinMode(N3,OUTPUT); | ||
pinMode(N4,OUTPUT); | ||
} | ||
|
||
void L298N::motorLeftForwardClock(uint8_t PWM){ | ||
analogWrite(ENA,PWM); | ||
digitalWrite(N1,HIGH); | ||
digitalWrite(N2,LOW); | ||
} | ||
/** | ||
* monit left (determine on README) trun right to left (Forward Clock) | ||
* by specify speed (PWM) 0 - 255 | ||
*/ | ||
void L298N::motorLeftForwardClock(uint8_t PWM) | ||
{ | ||
analogWrite(ENA, PWM); | ||
digitalWrite(N1, HIGH); | ||
digitalWrite(N2, LOW); | ||
} | ||
|
||
void L298N::motorRightForwardClock(uint8_t PWM){ | ||
analogWrite(ENB,PWM); | ||
digitalWrite(N3,HIGH); | ||
digitalWrite(N4,LOW); | ||
/** | ||
* monit right (determine on README) trun right to left (Forward Clock) | ||
* by specify speed (PWM) 0 - 255 | ||
*/ | ||
void L298N::motorRightForwardClock(uint8_t PWM) | ||
{ | ||
analogWrite(ENB, PWM); | ||
digitalWrite(N3, HIGH); | ||
digitalWrite(N4, LOW); | ||
} | ||
|
||
void L298N::motorLeftRevertClock(uint8_t PWM){ | ||
analogWrite(ENA,PWM); | ||
digitalWrite(N1,LOW); | ||
digitalWrite(N2,HIGH); | ||
/** | ||
* monit left (determine on README) trun left to right (revert Clock) | ||
* specify speed (PWM) is 0(0%) - 255(100%) | ||
*/ | ||
void L298N::motorLeftRevertClock(uint8_t PWM) | ||
{ | ||
analogWrite(ENA, PWM); | ||
digitalWrite(N1, LOW); | ||
digitalWrite(N2, HIGH); | ||
} | ||
|
||
void L298N::motorRightRevertClock(uint8_t PWM){ | ||
analogWrite(ENB,PWM); | ||
digitalWrite(N3,LOW); | ||
digitalWrite(N4,HIGH); | ||
/** | ||
* monit right (determine on README) trun left to right (revert Clock) | ||
* specify speed (PWM) is 0(0%) - 255(100%) | ||
*/ | ||
void L298N::motorRightRevertClock(uint8_t PWM) | ||
{ | ||
analogWrite(ENB, PWM); | ||
digitalWrite(N3, LOW); | ||
digitalWrite(N4, HIGH); | ||
} | ||
|
||
void L298N::motorLeftbreak(uint8_t PWM){ | ||
analogWrite(ENA,PWM); | ||
digitalWrite(N1,HIGH); | ||
digitalWrite(N2,HIGH); | ||
/** | ||
* monit left (determine on README) break | ||
* specify break speed (PWM) is 0(0%) - 255(100%) | ||
* smooth break | ||
*/ | ||
void L298N::motorLeftbreak(uint8_t PWM) | ||
{ | ||
analogWrite(ENA, PWM); | ||
digitalWrite(N1, HIGH); | ||
digitalWrite(N2, HIGH); | ||
} | ||
|
||
void L298N::motorRightbreak(uint8_t PWM){ | ||
analogWrite(ENB,PWM); | ||
digitalWrite(N3,HIGH); | ||
digitalWrite(N4,HIGH); | ||
/** | ||
* monit right (determine on README) break | ||
* specify break speed (PWM) is 0(0%) - 255(100%) | ||
* smooth break | ||
*/ | ||
void L298N::motorRightbreak(uint8_t PWM) | ||
{ | ||
analogWrite(ENB, PWM); | ||
digitalWrite(N3, HIGH); | ||
digitalWrite(N4, HIGH); | ||
} | ||
|
||
void L298N::motorLeftstop(){ | ||
analogWrite(ENA,LOW_PWM); | ||
digitalWrite(N1,LOW); | ||
digitalWrite(N2,LOW); | ||
/** | ||
* monit left (determine on README) stop | ||
*/ | ||
void L298N::motorLeftstop() | ||
{ | ||
analogWrite(ENA, LOW_PWM); | ||
digitalWrite(N1, LOW); | ||
digitalWrite(N2, LOW); | ||
} | ||
|
||
void L298N::motorRightstop(){ | ||
analogWrite(ENB,LOW_PWM); | ||
digitalWrite(N3,LOW); | ||
digitalWrite(N4,LOW); | ||
/** | ||
* monit right (determine on README) stop | ||
*/ | ||
void L298N::motorRightstop() | ||
{ | ||
analogWrite(ENB, LOW_PWM); | ||
digitalWrite(N3, LOW); | ||
digitalWrite(N4, LOW); | ||
} | ||
|
||
#endif | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
|
||
#ifndef MAX_PWM_SPEED | ||
#define MAX_PWM_SPEED 255 | ||
#ifndef MAX_PWM_SPEED | ||
#define MAX_PWM_SPEED 255 | ||
|
||
#include "Arduino.h" | ||
|
||
class L298N { | ||
|
||
|
||
public : | ||
L298N(); | ||
L298N(uint8_t ena,uint8_t n1,uint8_t n2,uint8_t n3,uint8_t n4,uint8_t enb); // init pin l298 motor driver | ||
void initmotor(); | ||
void motorLeftForwardClock(uint8_t PWM); | ||
void motorLeftRevertClock(uint8_t PWM); | ||
void motorRightForwardClock(uint8_t PWM); | ||
void motorRightRevertClock(uint8_t PWM); | ||
void motorLeftstop(); | ||
void motorRightstop(); | ||
void motorLeftbreak(uint8_t PWM); | ||
void motorRightbreak(uint8_t PWM); | ||
|
||
private : | ||
uint8_t ENA; | ||
uint8_t N1; | ||
uint8_t N2; | ||
uint8_t N3; | ||
uint8_t N4; | ||
uint8_t ENB; | ||
|
||
class L298N | ||
{ | ||
|
||
public: | ||
L298N(); | ||
L298N(uint8_t ena, uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4, uint8_t enb); // init pin l298 motor driver | ||
void begin(); | ||
void motorLeftForwardClock(uint8_t PWM); | ||
void motorLeftRevertClock(uint8_t PWM); | ||
void motorRightForwardClock(uint8_t PWM); | ||
void motorRightRevertClock(uint8_t PWM); | ||
void motorLeftstop(); | ||
void motorRightstop(); | ||
void motorLeftbreak(uint8_t PWM); | ||
void motorRightbreak(uint8_t PWM); | ||
|
||
private: | ||
uint8_t ENA; | ||
uint8_t N1; | ||
uint8_t N2; | ||
uint8_t N3; | ||
uint8_t N4; | ||
uint8_t ENB; | ||
}; | ||
|
||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,90 @@ | ||
# L298N | ||
|
||
Simple Arduino L298N Library make for easy way for control DC motor by using L298N Module. | ||
|
||
# how to use | ||
- install library to you arduino IDE (Download from github) | ||
|
||
- install library to your arduino IDE (Download from github) | ||
- let see examples | ||
|
||
# Example | ||
|
||
```Arduino | ||
#include <L298N.h> | ||
//pin Left | ||
#define A_ENA 2 | ||
#define A_N1 3 | ||
#define A_N2 4 | ||
#define A_N3 5 | ||
#define A_N4 6 | ||
#define A_ENB 7 | ||
L298N mdA(A_ENA, A_N1, A_N2, A_N3, A_N4, A_ENB); | ||
char junk; | ||
String inputString = ""; | ||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
mdA.begin(); | ||
} | ||
void loop() | ||
{ | ||
if (Serial.available()) | ||
{ | ||
while (Serial.available()) | ||
{ | ||
char inChar = (char)Serial.read(); | ||
inputString += inChar; | ||
} | ||
Serial.println(inputString); | ||
while (Serial.available() > 0) | ||
{ | ||
junk = Serial.read(); | ||
} // clear the serial buffer | ||
if (inputString == "1") | ||
{ | ||
mdA.motorLeftForwardClock(150); | ||
} | ||
else if (inputString == "2") | ||
{ | ||
mdA.motorLeftRevertClock(200); | ||
} | ||
else if (inputString == "3") | ||
{ | ||
mdA.motorRightForwardClock(255); | ||
} | ||
else if (inputString == "4") | ||
{ | ||
mdA.motorRightRevertClock(200); | ||
} | ||
else if (inputString == "5") | ||
{ | ||
mdA.motorLeftstop(); | ||
mdA.motorRightstop(); | ||
} | ||
else if (inputString == "6") | ||
{ | ||
mdA.motorLeftbreak(100); | ||
} | ||
else if (inputString == "7") | ||
{ | ||
mdA.motorRightbreak(100); | ||
} | ||
inputString = ""; | ||
} | ||
} //loop | ||
``` | ||
|
||
# determine left and right motor | ||
|
||
![l298n]( https://github.com/xang555/L298N/blob/master/img/l298edit.png ) | ||
![l298n](https://github.com/xang555/L298N/blob/master/img/l298edit.png) |
Oops, something went wrong.