-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathL298N.cpp
83 lines (67 loc) · 1.83 KB
/
L298N.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
L298N.cpp - Library for L298N motor driver
Created by Yohendry Hurtado, 28 dec 2014
Released into the public domain.
*/
#include "Arduino.h"
#include "L298N.h"
struct Motor {
int in1;
int in2;
int pwn;
};
Motor motors[2];
L298N::L298N(int ena, int in1, int in2, int in3, int in4, int enb) {
pinMode (ena, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
pinMode (in3, OUTPUT);
pinMode (in4, OUTPUT);
pinMode (enb, OUTPUT);
motors[0].in1 = in1;
motors[0].in2 = in2;
motors[0].pwn = ena;
motors[1].in1 = in3;
motors[1].in2 = in4;
motors[1].pwn = enb;
}
void L298N::drive_motors(int speed) {
this->drive_motor(this->MOTOR_A,speed);
this->drive_motor(this->MOTOR_B,speed);
}
void L298N::drive_motor(int motor_index, int speed) {
analogWrite(motors[motor_index].pwn,speed);
}
void L298N::forward(int speed, int delay_time) {
this->setup_motors(HIGH,LOW,HIGH,LOW);
this->drive_motors(speed);
delay(delay_time);
}
void L298N::turn_right(int speed, int delay_time) {
this->setup_motors(LOW,HIGH,HIGH,LOW);
this->drive_motors(speed);
delay(delay_time);
}
void L298N::turn_left(int speed, int delay_time) {
this->setup_motors(HIGH,LOW,LOW,HIGH);
this->drive_motors(speed);
delay(delay_time);
}
void L298N::backward(int speed, int delay_time) {
this->setup_motors(LOW,HIGH,LOW,HIGH);
this->drive_motors(speed);
delay(delay_time);
}
void L298N::full_stop(int delay_time) {
this->setup_motors(LOW,LOW,LOW,LOW);
this->drive_motors(0);
delay(delay_time);
}
void L298N::setup_motors(int state1, int state2, int state3, int state4) {
L298N::setup_motor(this->MOTOR_A,state1,state2);
L298N::setup_motor(this->MOTOR_B,state3,state4);
}
void L298N::setup_motor(int motor_index, int state1, int state2) {
digitalWrite(motors[motor_index].in1, state1);
digitalWrite(motors[motor_index].in2, state2);
}