Skip to content

Commit

Permalink
#update docs
Browse files Browse the repository at this point in the history
#change method initmotor to begin
#update example
#add comment code
  • Loading branch information
xang555 committed May 7, 2019
1 parent aa628e5 commit 3c5b948
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 104 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added .vscode/ipch/2806fae1a891c04c/basic.ipch
Binary file not shown.
Binary file added .vscode/ipch/2806fae1a891c04c/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/5303e5fff5c00d92/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/75d55167e402af2f/mmap_address.bin
Binary file not shown.
150 changes: 101 additions & 49 deletions L298N.cpp
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



51 changes: 25 additions & 26 deletions L298N.h
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
86 changes: 84 additions & 2 deletions README.md
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)
Loading

0 comments on commit 3c5b948

Please sign in to comment.