-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterrupcion_Motores_Tuenti.ino
219 lines (187 loc) · 4.79 KB
/
Interrupcion_Motores_Tuenti.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
#include <Arduino.h>
#include "BasicStepperDriver.h"
/// PASOS DEL MOTOR SI EL DE 1.8 GRADOS SON 200
#define MOTOR_STEPS 200
// VEL MOTOR 1
#define RPM 150
#define RPM3 120
#define RPM4 80
//// VALOR DEFINIDO POR EL CONTROLADRO DEL MOTOR A PASOS
// MOTOR 1 PINES
#define MICROSTEPS 1
#define DIR 2//2
#define STEP 3//3
#define SLEEP 4//4
//MOTOR2 CORRRECTO
#define MICROSTEPS2 1
#define DIR2 5
#define STEP2 6
#define SLEEP2 7
//MOTOR3
#define MICROSTEPS3 1
#define DIR3 8 //8
#define STEP3 9 //9
#define SLEEP3 10 //10
//MOTOR4
#define MICROSTEPS4 1
#define DIR4 11 //11
#define STEP4 12 //12
#define SLEEP4 13 //13
const int sensorPin1= 20;
const int sensorPin2= 19;
const int sensorPin3= 18;
int value = 0;
//Uncomment line to use enable/disable functionality
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
bool stopMotor1 = true;
int timeMotor2 = 900;
int contTimeMotor2 = 0;
bool moveMotor3 = false;
int timeStartMotor3 = 350;
int timeMotor3 = 540;
int contTimeMotor3 = 0;
bool moveMotor4 = false;
int timeStartMotor4 = 150;
int timeMotor4 = 300;
int contTimeMotor4 = 0;
// 2-wire basic config, microstepping is hardwired on the driver
BasicStepperDriver stepper1(MOTOR_STEPS, DIR, STEP, SLEEP);
BasicStepperDriver stepper2(MOTOR_STEPS, DIR2, STEP2, SLEEP2);
BasicStepperDriver stepper3(MOTOR_STEPS, DIR3, STEP3, SLEEP3);
BasicStepperDriver stepper4(MOTOR_STEPS, DIR4, STEP4, SLEEP4);
void setup() {
Serial.begin(9600);
stepper1.begin(RPM, MICROSTEPS);
stepper2.begin(RPM, MICROSTEPS2);
stepper3.begin(RPM3, MICROSTEPS3);
stepper4.begin(RPM4, MICROSTEPS4);
pinMode(sensorPin1,INPUT);
pinMode(sensorPin2,INPUT);
pinMode(sensorPin3,INPUT);
attachInterrupt(3,sensor1,FALLING); //PIN 21
attachInterrupt(4,sensor2,FALLING); //PIN 20
attachInterrupt(5,sensor3,FALLING); //PIN 19
// if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line
stepper1.setEnableActiveState(LOW);
stepper2.setEnableActiveState(LOW);
stepper3.setEnableActiveState(LOW);
stepper4.setEnableActiveState(LOW);
Serial.println("Inicio programa");
}
void loop() {
motor1();
motor2();
motor3();
motor4();
recvWithStartEndMarkers();
serialComOptions();
delay(2);
}
void sensor1(){
//PARA MOTOR 1 INMEDIATAMENTE
value = digitalRead(sensorPin1); //lectura digital de pin
Serial.println("PARAR MOTOR 1");
stepper1.stop();
stopMotor1 = true;
}
void sensor2(){
//MUEVE MOTOR 3
value = digitalRead(sensorPin2); //lectura digital de pin
Serial.println("MOVER MOTOR 3");
moveMotor3 = true;
}
void sensor3(){
//MUEVE MOTOR 4
value = digitalRead(sensorPin3); //lectura digital de pin
Serial.println("MOVER MOTOR 4");
moveMotor4 = true;
}
void motor1(){
if(!stopMotor1){
stepper1.move(1);
}
}
void motor2(){
if(!stopMotor1 || contTimeMotor2 <= timeMotor2 ){
stepper2.move(1);
if(stopMotor1){
contTimeMotor2++;
}
}
}
void motor3(){
if(moveMotor3 && contTimeMotor3 <= timeMotor3){
if(contTimeMotor3 > timeStartMotor3){
stepper3.move(1);
}
if(contTimeMotor3 == timeMotor3){
contTimeMotor3 = 0;
moveMotor3 = false;
}else{
contTimeMotor3++;
}
}
}
void motor4(){
if(moveMotor4 && contTimeMotor4 <= timeMotor4){
if(contTimeMotor4 > timeStartMotor4){
stepper4.move(1);
}
if(contTimeMotor4 == timeMotor4){
contTimeMotor4 = 0;
moveMotor4 = false;
}else{
contTimeMotor4++;
}
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '*';
char endMarker = '*';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void serialComOptions() {
if (newData == true) {
//Serial.print("This just in ... ");
switch(receivedChars[0]){
case 'I':
Serial.println("Inicio");
stopMotor1 = false;
contTimeMotor2 = 0;
break;
case 'A':
Serial.println("Otro dato");
stepper1.rotate(360*10);
break;
default:
Serial.println("default");
break;
}
newData = false;
}
}