-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCytron.h
More file actions
317 lines (282 loc) · 7.88 KB
/
Cytron.h
File metadata and controls
317 lines (282 loc) · 7.88 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//TODO: Add a stop() function
class Cytron
{
public:
uint8_t dIrpin, pWmpin, lAstpwm, step1, interval; //variables for first motor
uint8_t dIrpin1, pWmpin1, lAstpwm1, step2; //variables for second motor
uint8_t dIrpin2, pWmpin2, lAstpwm2, step3; //variables for third motor
uint8_t dIrpin3, pWmpin3, lAstpwm3, step4; //variables for third motor
Cytron(uint8_t getdIrpin, uint8_t getpWmpin){ //constructor function takes direction and pwm pins for cytron
dIrpin=getdIrpin;
pWmpin=getpWmpin;
//sets direction pin as output
lAstpwm=0;
step1=5;
interval=10;
}
Cytron(uint8_t getdIrpin, uint8_t getpWmpin, uint8_t getdIrpin1, uint8_t getpWmpin1){ //constructor function takes direction and pwm pins for cytron
dIrpin=getdIrpin;
pWmpin=getpWmpin;
pWmpin1=getpWmpin1;
dIrpin1=getdIrpin1;
lAstpwm=0;
lAstpwm1=0;
step1=5;
step2=5;
interval=10;
}
Cytron(uint8_t getdIrpin, uint8_t getpWmpin, uint8_t getdIrpin1, uint8_t getpWmpin1, uint8_t getdIrpin2, uint8_t getpWmpin2){ //constructor function takes direction and pwm pins for cytron
pWmpin=getpWmpin;
dIrpin=getdIrpin;
pWmpin1=getpWmpin1;
dIrpin1=getdIrpin1;
pWmpin2=getpWmpin2;
dIrpin2=getdIrpin2;
lAstpwm=0;
lAstpwm1=0;
lAstpwm2=0;
step1=5;
step2=5;
step3=5;
interval=10;
}
Cytron(uint8_t getdIrpin, uint8_t getpWmpin, uint8_t getdIrpin1, uint8_t getpWmpin1, uint8_t getdIrpin2, uint8_t getpWmpin2, uint8_t getdIrpin3, uint8_t getpWmpin3){ //constructor function takes direction and pwm pins for cytron
pWmpin=getpWmpin;
dIrpin=getdIrpin;
pWmpin1=getpWmpin1;
dIrpin1=getdIrpin1;
pWmpin2=getpWmpin2;
dIrpin2=getdIrpin2;
pWmpin3=getpWmpin3;
dIrpin3=getdIrpin3;
lAstpwm=0;
lAstpwm1=0;
lAstpwm2=0;
lAstpwm3=0;
step1=5;
step2=5;
step3=5;
step4=5;
interval=10;
}
void direction(uint8_t dIrection){
pinMode(dIrpin,OUTPUT);
digitalWrite(dIrpin,dIrection);
}
void direction(uint8_t dIrection, uint8_t dIrection1){
pinMode(dIrpin,OUTPUT); //sets direction pin as output
pinMode(dIrpin1,OUTPUT);
digitalWrite(dIrpin,dIrection);
digitalWrite(dIrpin1,dIrection1);
}
void direction(uint8_t dIrection, uint8_t dIrection1, uint8_t dIrection2){
pinMode(dIrpin,OUTPUT); //sets direction pin as output
pinMode(dIrpin1,OUTPUT);
pinMode(dIrpin2,OUTPUT);
digitalWrite(dIrpin,dIrection);
digitalWrite(dIrpin1,dIrection1);
digitalWrite(dIrpin2,dIrection2);
}
void direction(uint8_t dIrection, uint8_t dIrection1, uint8_t dIrection2, uint8_t dIrection3){
pinMode(dIrpin,OUTPUT); //sets direction pin as output
pinMode(dIrpin1,OUTPUT);
pinMode(dIrpin2,OUTPUT);
pinMode(dIrpin3,OUTPUT);
digitalWrite(dIrpin,dIrection);
digitalWrite(dIrpin1,dIrection1);
digitalWrite(dIrpin2,dIrection2);
digitalWrite(dIrpin3,dIrection3);
}
void drive(uint8_t pWm){
if(pWm>lAstpwm){ //accelerate
for(lAstpwm=lAstpwm;lAstpwm<pWm;){ //increase the speed step by step
lAstpwm+=step1;
analogWrite(pWmpin,constrain(lAstpwm,0,pWm));
delay(interval);
}
}
else if(pWm<lAstpwm){ //decelerate
for(lAstpwm=lAstpwm;lAstpwm>pWm;){ //decrease the speed step by step
lAstpwm-=step1;
analogWrite(pWmpin,constrain(lAstpwm,pWm,255));
delay(interval);
}
}
else{ //no change
analogWrite(pWmpin,pWm);
}
}
//TWO MOTORS
void drive(uint8_t pWm, uint8_t pWm1){
if(pWm>lAstpwm&&pWm1>lAstpwm1){ //accelerate
for(lAstpwm=lAstpwm,lAstpwm1=lAstpwm1;lAstpwm<pWm&&lAstpwm1<pWm1;){ //increase the speed step by step
if (pWm>lAstpwm){
lAstpwm+=step1;
analogWrite(pWmpin,constrain(lAstpwm,0,pWm));
}
if (pWm1>lAstpwm1){
lAstpwm1+=step2;
analogWrite(pWmpin1,constrain(lAstpwm1,0,pWm1));
}
delay(interval);
}
}
else if(pWm<lAstpwm&&pWm1<lAstpwm1){ //decelerate
for(lAstpwm=lAstpwm,lAstpwm1=lAstpwm1;lAstpwm>pWm&&lAstpwm1>pWm1;){ //decrease the speed step by step
if (pWm<lAstpwm){
lAstpwm-=step1;
analogWrite(pWmpin,constrain(lAstpwm,pWm,255));
}
if (pWm1<lAstpwm1){
lAstpwm1-=step2;
analogWrite(pWmpin1,constrain(lAstpwm1,pWm1,255));
}
delay(interval);
}
}
else{ //no change
analogWrite(pWmpin,pWm);
analogWrite(pWmpin1,pWm1);
}
}
//THREE MOTORS
void drive(uint8_t pWm, uint8_t pWm1, uint8_t pWm2)
{
while(pWm==lAstpwm&&pWm1==lAstpwm1&&pWm2=lAstpwm2)
{
if(pWm>lAstpwm)
{lAstpwm+=step1;
constrain(lAstpwm,0,pWm);
}
else if(pWm<lAstpwm)
{lAstpwm-=step1;
constrain(lAstpwm,pWm,255);
}
if(pWm1>lAstpwm1)
{lAstpwm1+=step2;
constrain(lAstpwm,0,pWm);
}
else if(pWm1<lAstpwm1)
{lAstpwm1-=step2;
constrain(lAstpwm,pWm,255);
}
if(pWm2>lAstpwm2)
{lAstpwm2+=step3;
constrain(lAstpwm,0,pWm);
}
else if(pWm2<lAstpwm1)
{lAstpwm2-=step3;
constrain(lAstpwm,pWm,255);
}
analogWrite(pWmpin,lastpWm);
analogWrite(pWmpin1,lastpWm1);
analogWrite(pWmpin2,lastpWm2);
delay(interval);
}
}
//FOUR MOTORS
void drive(uint8_t pWm, uint8_t pWm1, uint8_t pWm2, uint8_t pWm3){
if(pWm>lAstpwm&&pWm1>lAstpwm1&&pWm2>lAstpwm2&&pWm3>lAstpwm3){ //accelerate
for(lAstpwm=lAstpwm,lAstpwm1=lAstpwm1,lAstpwm2=lAstpwm2,lAstpwm3=lAstpwm3;lAstpwm<pWm&&lAstpwm1<pWm1&&lAstpwm2<pWm2&&lAstpwm3<pWm3;){ //increase the speed step by step
if (pWm>lAstpwm){
lAstpwm+=step1;
analogWrite(pWmpin,constrain(lAstpwm,0,pWm));
}
if (pWm1>lAstpwm1){
lAstpwm1+=step2;
analogWrite(pWmpin1,constrain(lAstpwm1,0,pWm1));
}
if (pWm2>lAstpwm2){
lAstpwm2+=step3;
analogWrite(pWmpin2,constrain(lAstpwm2,0,pWm2));
}
if (pWm3>lAstpwm3){
lAstpwm3+=step4;
analogWrite(pWmpin3,constrain(lAstpwm3,0,pWm3));
}
delay(interval);
}
}
else if(pWm<lAstpwm&&pWm1<lAstpwm1&&pWm2<lAstpwm2&&pWm3<lAstpwm3){ //decelerate
for(lAstpwm=lAstpwm,lAstpwm1=lAstpwm1,lAstpwm2=lAstpwm2,lAstpwm3=lAstpwm3;lAstpwm>pWm&&lAstpwm1>pWm1&&lAstpwm2>pWm2&&lAstpwm3>pWm3;){ //decrease the speed step by step
if (pWm<lAstpwm){
lAstpwm-=step1;
analogWrite(pWmpin,constrain(lAstpwm,pWm,255));
}
if (pWm1<lAstpwm1){
lAstpwm1-=step2;
analogWrite(pWmpin1,constrain(lAstpwm1,pWm1,255));
}
if (pWm2<lAstpwm2){
lAstpwm2-=step3;
analogWrite(pWmpin2,constrain(lAstpwm2,pWm2,255));
}
if (pWm3<lAstpwm3){
lAstpwm3-=step4;
analogWrite(pWmpin3,constrain(lAstpwm3,pWm3,255));
}
delay(interval);
}
}
else{ //no change
analogWrite(pWmpin,pWm);
analogWrite(pWmpin1,pWm1);
analogWrite(pWmpin2,pWm2);
analogWrite(pWmpin3,pWm3);
}
}
void stop(int s)
{
switch(s)
{
case 1:
analogWrite(pWmpin,0);
break;
case 2:
analogWrite(pWmpin1,0);
break;
case 3:
analogWrite(pWmpin2,0);
break;
}
}
void stop(short int s,short int a)
{
switch(s)
{
case 1:
analogWrite(pWmpin,0);
switch(a)
{case 2:
analogWrite(pWmpin1,0);
break;
case 3:
analogWrite(pWmpin2,0);
break;
}
break;
case 2:
analogWrite(pWmpin1,0);
switch(a)
{case 1:
analogWrite(pWmpin,0);
break;
case 3:
analogWrite(pWmpin2,0);
break;
}
break;
case 3:
analogWrite(pWmpin2,0);
switch(a)
{
case 1:
analogWrite(pWmpin,0);
break;
case 2:
analogWrite(pWmpin1,0);
break;
}
}
}
};