-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_stepmotor10.ino
462 lines (364 loc) · 10.5 KB
/
sketch_stepmotor10.ino
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* Step Motor Control sketch(code)
We have here, a positioning controller system, with the Arduino Uno microcontroller board.
It uses the Step Motor, coded 28byj-48, the Driver, coded uln2003 and the Incremental Rotary
Encoder, coded ky-040.
Stages of the Control Process : Direct drive, Reverse drive and Recovery of Positioning, after
an eventual fault of the electrical Power Supply.
This project does not involves any Limit Switches, because it involves the use of an Incremental
Rotary Encoder.
Find all the hardware connections, in the void setup section.
Development Year : 2024
Control System Accuracy : +/- 1 (one) pulse of encoder.
Acknowledgements : Mathias Hertel (mathertel), by his Library 'RotaryEncoder' (in github.com)
Read,please, the README and the Usage sections, for the present sketch(code).
*/
// opening the libraries of the C++ programming language (built-in and imported ).
#include <EEPROM.h> // Library built-in
#include "RotaryEncoder.h" // Library imported
RotaryEncoder encoder(A2,A3);
#define abs(x) ((x)>0?(x):-(x)) // Modulo Function
// initialization of variables.
unsigned long ton;
unsigned long toff;
const unsigned long period = 20;
const float f1 = .35;
const float f3 = .4;
const int value1 = 4;
const unsigned long nmax = 500;
const unsigned long speed = 9600;
int comando = 1;
float count = 1;
int stopbutton=0;
const int buttonpin = 13;
byte deltateta = EEPROM.read(1);
byte posit = deltateta;
//int pos = 257*deltateta-32768;
byte flag1 = EEPROM.read(2);
byte flag2 = EEPROM.read(3);
byte flag3 = EEPROM.read(5);
byte posit1 = EEPROM.read(6);
byte count1 = EEPROM.read(4);
const int value2 = 4;
const int value3 = 5;
void setup() {
// put your setup code here, to run once:
// step motor phases initialization and else connections:
pinMode(2,OUTPUT); // initialize phase1 in pin2, as a digital input (IN1) of uln2003 driver.
pinMode(4,OUTPUT); // initialize phase2 in pin4, as a digital input (IN2) of uln2003 driver.
pinMode(7,OUTPUT); // initialize phase3 in pin7, as a digital input (IN3) of uln2003 driver.
pinMode(8,OUTPUT); // initialize phase4 in pin8, as a digital input (IN4) of uln2003 driver.
// connector of 28byj-48 step motor : connect in the slot, over uln2003 board.
// feed the uln2003 board and the Arduino board : + (connect to +5v), GND (connect to GrouND).
pinMode(3,OUTPUT); // initialize pwm out in pin3, for void loop running indicator (connect to the anode).
// indicator : a 5mm blue led, in second plane. Insert a 220 ohm resistor.
pinMode(buttonpin,INPUT); // initialize stop button in pin13.
// connections : connect to +5v one of two pins of the stop button;
// pin13 to another pin of the stop button and (in pull-down) from this point to GrouND through a 10Kohm resistor.
pinMode(A2,INPUT); // initialize analog input A2 to connect with DT pin of encoder.
pinMode(A3,INPUT); // initialize analog input A3 to connect with CLK pin of encoder.
// the else pins of encoder : + (connect to +5v), GND (connect to GrouND) and SW (not used).
pinMode(5,OUTPUT); // initialize pwm out in pin5 : connect to the smd green led (at left side of step motor).
pinMode(6,OUTPUT); // initialize pwm out in pin6 : connect to the smd blue led (at left side of step motor).
pinMode(9,OUTPUT); // initialize pwm out in pin9 : connect to the smd green led (at right side of step motor).
pinMode(10,OUTPUT);// initialize pwm out in pin10 : connect to the smd blue led (at right side of step motor).
// insert a resistor of 220 ohm in all the connections, for smd led's above.
Serial.begin(speed);
Serial.println("começa aqui esse run");
Serial.print("deltateta=");
Serial.println(deltateta);
Serial.print("posit=");
Serial.println(posit);
int comando = 1;
float count = 1.00;
int stopbutton=1;
if(flag2 != 1 && (posit >= 0)) {
EEPROM.write( 6, posit );
count1 = count1+1;
EEPROM.write( 4 , count1 );
unsigned long n = 1;
analogWrite(3,value1); // turns to HIGH led, void loop running indicator.
analogWrite(6,value3);
Serial.println("xxxxxxxxxxxxxxxx Direct Adj. xxxxxxxxxxxxxxxx");
while((n >= 1 && n <= nmax) && ((posit != (19-deltateta) && posit != (255-deltateta)))) {
// step motor runs in the first gear and in the counterclockwise way, para Adj.
ton = 7.00;
toff = 13.00;
// encoder's informations lecture.
static byte pos = deltateta;
encoder.tick();
byte newPos = encoder.getPosition();
// if the angular position was changed, shows the new value on the Serial Monitor.
if( pos != newPos ){
Serial.println( newPos );
pos = newPos;
posit = newPos;
}
digitalWrite(4,HIGH);
delay(ton);
digitalWrite(4,LOW);
delay(toff);
digitalWrite(2,HIGH);
delay(ton);
digitalWrite(2,LOW);
delay(toff);
digitalWrite(8,HIGH);
delay(ton);
digitalWrite(8,LOW);
delay(toff);
digitalWrite(7,HIGH);
delay(ton);
digitalWrite(7,LOW);
delay(toff);
n = n+1;
analogWrite(9,value2);
delay(1);
analogWrite(9,0);
delay(1);
}
EEPROM.write( 2 , 1 );
flag1 = EEPROM.read(2);
analogWrite(6,0);
}
if(flag1 != 1 && (posit >= 0)) {
unsigned long n = 1;
analogWrite(3,value1); // turns to HIGH led, void loop running indicator.
analogWrite(10,value3);
Serial.println("xxxxxxxxxxxxxxxx REV Adj. xxxxxxxxxxxxxxxx");
if(count1 != 0) {
while((n >= 1 && n <= nmax) && posit != (256-(18-(255-posit1))+(255-deltateta)) && posit != ((256-(19-(255-posit1+deltateta))-(19-(255-posit1))))) {
// step motor runs in the third gear and in the clockwise way, para Adj.
ton = 8.00;
toff = 12.00;
// encoder's informations lecture.
static byte pos = deltateta;
encoder.tick();
byte newPos = encoder.getPosition();
// if the angular position was changed, shows the new value on the Serial Monitor.
if( pos != newPos ){
Serial.println( newPos );
pos = newPos;
posit = newPos;
}
digitalWrite(2,HIGH);
delay(ton);
digitalWrite(2,LOW);
delay(toff);
digitalWrite(4,HIGH);
delay(ton);
digitalWrite(4,LOW);
delay(toff);
digitalWrite(7,HIGH);
delay(ton);
digitalWrite(7,LOW);
delay(toff);
digitalWrite(8,HIGH);
delay(ton);
digitalWrite(8,LOW);
delay(toff);
n = n+1;
analogWrite(5,value2);
delay(1);
analogWrite(5,0);
delay(1);
}
} else
{ while((n >= 1 && n <= nmax) && (posit != (19-deltateta) && posit != (256-(deltateta-237)))) {
// step motor runs in the third gear and in the clockwise way, para Adj.
ton = 8.00;
toff = 12.00;
// encoder's informations lecture.
static byte pos = deltateta;
encoder.tick();
byte newPos = encoder.getPosition();
// if the angular position was changed, shows the new value on the Serial Monitor.
if( pos != newPos ){
Serial.println( newPos );
pos = newPos;
posit = newPos;
}
digitalWrite(2,HIGH);
delay(ton);
digitalWrite(2,LOW);
delay(toff);
digitalWrite(4,HIGH);
delay(ton);
digitalWrite(4,LOW);
delay(toff);
digitalWrite(7,HIGH);
delay(ton);
digitalWrite(7,LOW);
delay(toff);
digitalWrite(8,HIGH);
delay(ton);
digitalWrite(8,LOW);
delay(toff);
n = n+1;
analogWrite(5,value2);
delay(1);
analogWrite(5,0);
delay(1);
}
}
EEPROM.write( 5, 1 );
flag3 = EEPROM.read(5);
analogWrite(10,0);
}
Serial.println("the encoder rotates coupled to the step motor...");
}
void loop() {
// put your main code here, to run repeatedly:
if(count >= 0.00 && comando != 0 ) {
Serial.println(count, 2);
}
if (comando == 1) {
stopbutton = 0;
unsigned long n = 1;
analogWrite(3,value1); // turns to HIGH led, void loop running indicator.
EEPROM.write( 3 , 0 );
EEPROM.write( 2 , 1 );
Serial.println("xxxxxxxxxxxxxxxx Direct Main xxxxxxxxxxxxxxxx");
while(n >= 1 && n <= nmax && flag3 != 1) { // step motor runs in the third gear and in the clockwise way.
ton = 8.00;
toff = 12.00;
if (stopbutton != 1) {
stopbutton=digitalRead(buttonpin);
}
// encoder's informations lecture.
static byte pos = 0;
encoder.tick();
byte newPos = encoder.getPosition();
// if the angular position was changed, shows the new value on the Serial Monitor.
if( pos != newPos ){
Serial.println( newPos );
pos = newPos;
posit = newPos;
}
EEPROM.write( 1 , posit );
digitalWrite(2,HIGH);
delay(ton);
digitalWrite(2,LOW);
delay(toff);
digitalWrite(4,HIGH);
delay(ton);
digitalWrite(4,LOW);
delay(toff);
digitalWrite(7,HIGH);
delay(ton);
digitalWrite(7,LOW);
delay(toff);
digitalWrite(8,HIGH);
delay(ton);
digitalWrite(8,LOW);
delay(toff);
analogWrite(5,value2);
delay(1);
analogWrite(5,0);
delay(1);
if (stopbutton != 1) {
stopbutton=digitalRead(buttonpin);
}
EEPROM.write( 1 , posit );
n = n+1;
}
n = 1;
if(count == 1.00) {
float(ton);
float(toff);
ton = 8.00;
toff = 12.00;
Serial.print("f3:");
Serial.println(f3, 2);
Serial.print("ton=");
Serial.println(ton, 2);
Serial.print("toff=");
Serial.println(toff, 2);
}
Serial.println("xxxxxxxxxxxxxxxx REV Main xxxxxxxxxxxxxxxx");
while(n >= 1 && n <= nmax) { // step motor runs in the first gear and in the counterclockwise way.
EEPROM.write( 3 , 1 );
if (stopbutton == 1) { // setting the directional flow flags.
EEPROM.write( 2 , 1 );
EEPROM.write( 4 , 0);
EEPROM.write( 5 , 0 );
} else {
EEPROM.write( 2 , 0 );
EEPROM.write( 5 , 0 );
flag3 = EEPROM.read(5);
}
unsigned long (ton);
unsigned long (toff);
ton = 7;
toff = 13;
if (stopbutton != 1) {
stopbutton=digitalRead(buttonpin);
}
// encoder's informations lecture.
static byte pos = 0;
encoder.tick();
byte newPos = encoder.getPosition();
// if the angular position was changed, shows the new value on the Serial Monitor.
if( pos != newPos ){
Serial.println( newPos );
pos = newPos;
posit = newPos;
}
EEPROM.write( 1 , posit );
digitalWrite(4,HIGH);
delay(ton);
digitalWrite(4,LOW);
delay(toff);
digitalWrite(2,HIGH);
delay(ton);
digitalWrite(2,LOW);
delay(toff);
digitalWrite(8,HIGH);
delay(ton);
digitalWrite(8,LOW);
delay(toff);
digitalWrite(7,HIGH);
delay(ton);
digitalWrite(7,LOW);
delay(toff);
analogWrite(9,value2);
delay(1);
analogWrite(9,0);
delay(1);
if (stopbutton != 1) {
stopbutton=digitalRead(buttonpin);
}
EEPROM.write( 1 , posit );
n = n+1;
}
if(count == 1.00) {
float(ton);
float(toff);
ton = 7.00;
toff = 13;
Serial.print("f1:");
Serial.println(f1, 2);
Serial.print("ton=");
Serial.println(ton, 2);
Serial.print("toff=");
Serial.println(toff, 2);
}
if (stopbutton != 1) {
int stopbutton=digitalRead(buttonpin);
}
if (stopbutton == 1) { // setting the directional flow flags.
EEPROM.write( 2 , 1 );
EEPROM.write( 4 , 0);
EEPROM.write( 5 , 0 );
} else {
EEPROM.write( 2 , 0 );
EEPROM.write( 5 , 0 );
flag3 = EEPROM.read(5);
}
if (stopbutton == 1) {
comando = 0;
} else {
comando = 1;
}
count = count+1.00;
stopbutton=0;
}
}