forked from sudar/DCMotorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCMotorBot.cpp
More file actions
220 lines (172 loc) · 3.75 KB
/
Copy pathDCMotorBot.cpp
File metadata and controls
220 lines (172 loc) · 3.75 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
DCMotorBot - A simple library to control bots made using 2 simple DC Motors
http://hardwarefun.com/projects/dc-motor-bot
*/
/*
* Copyright 2011 Sudar Muthu (email : sudar@sudarmuthu.com)
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <sudar@sudarmuthu.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer or coffee in return - Sudar
* ----------------------------------------------------------------------------
*/
#include "DCMotorBot.h"
/**
* Full Constructor
*/
DCMotorBot::DCMotorBot(byte e1, byte e2, byte I1, byte I2, byte I3, byte I4, int delay) {
setEnablePins(e1, e2);
setControlPins(I1, I2, I3, I4);
setDelay(delay);
}
/**
* Default Constructor
*/
DCMotorBot::DCMotorBot() {
setEnablePins(1, 2);
setControlPins(3, 4, 5, 6);
setDelay(10);
setSpeed(255);
}
/**
* Set Enable pins
*/
void DCMotorBot::setEnablePins(byte e1, byte e2) {
mE1 = e1;
mE2 = e2;
pinMode(mE1, OUTPUT);
pinMode(mE2, OUTPUT);
}
/**
* Set Control pins
*/
void DCMotorBot::setControlPins(byte I1, byte I2, byte I3, byte I4) {
mI1 = I1;
mI2 = I2;
mI3 = I3;
mI4 = I4;
pinMode(mI1, OUTPUT);
pinMode(mI2, OUTPUT);
pinMode(mI3, OUTPUT);
pinMode(mI4, OUTPUT);
}
/**
* Set delay value
*/
void DCMotorBot::setDelay(int delay) {
mDelay = delay;
}
/**
* Set PWM Speed
*/
void DCMotorBot::setSpeed(int speed) {
mSpeed = speed;
}
/**
* Start the bot and move forward
*/
void DCMotorBot::start() {
moveForward();
}
/**
* Move forward
*/
void DCMotorBot::moveForward() {
analogWrite(mE1, mSpeed);
analogWrite(mE2, mSpeed);
delay(mDelay);
digitalWrite(mI1, HIGH);
digitalWrite(mI2, LOW);
digitalWrite(mI3, HIGH);
digitalWrite(mI4, LOW);
}
/**
* Move backward
*/
void DCMotorBot::moveBackward() {
analogWrite(mE1, mSpeed);
analogWrite(mE2, mSpeed);
delay(mDelay);
digitalWrite(mE1, HIGH);
digitalWrite(mE2, HIGH);
digitalWrite(mI1, LOW);
digitalWrite(mI2, HIGH);
digitalWrite(mI3, LOW);
digitalWrite(mI4, HIGH);
}
/**
* Turn Left
*/
void DCMotorBot::turnLeft() {
digitalWrite(mE1, LOW);
digitalWrite(mE2, LOW);
delay(mDelay);
digitalWrite(mE1, LOW);
digitalWrite(mE2, HIGH);
digitalWrite(mI1, HIGH);
digitalWrite(mI2, LOW);
digitalWrite(mI3, HIGH);
digitalWrite(mI4, LOW);
}
/**
* Turn Right
*/
void DCMotorBot::turnRight() {
digitalWrite(mE1, LOW);
digitalWrite(mE2, LOW);
delay(mDelay);
digitalWrite(mE1, HIGH);
digitalWrite(mE2, LOW);
digitalWrite(mI1, HIGH);
digitalWrite(mI2, LOW);
digitalWrite(mI3, HIGH);
digitalWrite(mI4, LOW);
}
/**
* Stop Bot
*/
void DCMotorBot::stop() {
digitalWrite(mE1, LOW);
digitalWrite(mE2, LOW);
}
/**
* Turn wheel left
*/
void DCMotorBot::steerLeft(){
digitalWrite(mE2, LOW);
delay(mDelay);
digitalWrite(mE1, HIGH);
digitalWrite(mI1, HIGH);
digitalWrite(mI2, LOW);
}
/**
* Turn wheel right
*/
void DCMotorBot::steerRight(){
digitalWrite(mE2, LOW);
delay(mDelay);
digitalWrite(mE1, HIGH);
digitalWrite(mI1, LOW);
digitalWrite(mI2, HIGH);
}
/**
* move forward width one drive motor
*/
void DCMotorBot::goForward() {
digitalWrite(mE1, LOW);
delay(mDelay);
analogWrite(mE2, mSpeed);
digitalWrite(mI3, LOW);
digitalWrite(mI4, HIGH);
}
/**
* move backward width one drive motor
*/
void DCMotorBot::goBackward() {
analogWrite(mE1, mSpeed);
delay(mDelay);
digitalWrite(mE2, HIGH);
digitalWrite(mI3, HIGH);
digitalWrite(mI4, LOW);
}