-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-stepper-v2.ino
182 lines (153 loc) · 4.9 KB
/
arduino-stepper-v2.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
const int step_pin_x = 8;
const int dir_pin_x = 9;
const int home_switch_x = 3;
const int step_pin_y = 10;
const int dir_pin_y = 11;
const int home_switch_y = 4;
const int step_pin_z = 12;
const int dir_pin_z = 13;
const int home_switch_z = 5;
const int z = 240;
int x;
int y;
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
pinMode(step_pin_x,OUTPUT);
pinMode(dir_pin_x,OUTPUT);
pinMode(home_switch_x , INPUT);
pinMode(step_pin_y,OUTPUT);
pinMode(dir_pin_y,OUTPUT);
pinMode(home_switch_y , INPUT);
pinMode(step_pin_z,OUTPUT);
pinMode(dir_pin_z,OUTPUT);
pinMode(home_switch_z , INPUT);
myservo.attach(2);
Serial.begin(9600);
setHome(home_switch_x,dir_pin_x,step_pin_x);
delay(1000);
setHome(home_switch_y,dir_pin_y,step_pin_y);
delay(1000);
setHome(home_switch_z,dir_pin_z,step_pin_z);
delay(1000);
motorStep(z*25,step_pin_z );
delay(1000);
myservo.write(0);
delay(500);
}
void motorStep( int max, int step_pin){
for(int i= 0; i < max; i++) {
digitalWrite(step_pin,HIGH);
delayMicroseconds(1000);
digitalWrite(step_pin,LOW);
delayMicroseconds(1000);
}
}
void setHome(int home_switch,int dir_pin,int step_pin){
// Start Homing procedure of Stepper Motor at startup
if ( home_switch != 5 ) {
while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000); // Delay to slow down speed of Stepper
digitalWrite(step_pin, LOW);
delayMicroseconds(1000);
}
while (!digitalRead(home_switch)) { // Do this until the switch is not activated
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000); // More delay to slow even more while moving away from switch
digitalWrite(step_pin, LOW);
delayMicroseconds(1000);
}
// do Thing A
}
else if ( home_switch == 5 ) { while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000); // Delay to slow down speed of Stepper
digitalWrite(step_pin, LOW);
delayMicroseconds(1000);
}
while (!digitalRead(home_switch)) { // Do this until the switch is not activated
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000); // More delay to slow even more while moving away from switch
digitalWrite(step_pin, LOW);
delayMicroseconds(1000);
}
// do Thing B
}
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n'); // Read the input string until a newline character
// Find the position of the comma separator
int commaPos = input.indexOf(',');
if (commaPos != -1) {
// Split the input string into two substrings
String sx= input.substring(0, commaPos);
String sy= input.substring(commaPos + 1);
// Convert the substrings to integers
x= sx.toInt();
y= sy.toInt();
}
Serial.print(x);
Serial.print('/');
Serial.print(y);
Serial.println();
motorStep(x*25,step_pin_x ); // no.of steps = x-cordinate * 25
delay(1000);
motorStep(y*25,step_pin_y ); // no.of steps = y-cordinate * 25
delay(1000);
setHome(home_switch_z,dir_pin_z,step_pin_z);
delay(1000);
myservo.write(130);
delay(500);
motorStep(z*25,step_pin_z );
delay(1000);
setHome(home_switch_x,dir_pin_x,step_pin_x);
delay(1000);
setHome(home_switch_y,dir_pin_y,step_pin_y);
delay(1000);
setHome(home_switch_z,dir_pin_z,step_pin_z);
delay(1000);
myservo.write(0);
delay(500);
// Print the input values as a single output separated by '/'
Serial.print(x);
Serial.print('/');
Serial.print(y);
Serial.println();
}
else{
x=0;
y=0;
// motorStep(x*25,step_pin_x ); // no.of steps = x-cordinate * 25
// delay(1000);
// motorStep(y*25,step_pin_y ); // no.of steps = y-cordinate * 25
// delay(1000);
//
// setHome(home_switch_z,dir_pin_z,step_pin_z);
// delay(1000);
//
// myservo.write(130);
// delay(500);
//
//
// motorStep(z*25,step_pin_z );
// delay(1000);
//
// setHome(home_switch_x,dir_pin_x,step_pin_x);
// delay(1000);
// setHome(home_switch_y,dir_pin_y,step_pin_y);
// delay(1000);
// setHome(home_switch_z,dir_pin_z,step_pin_z);
// delay(1000);
//
// myservo.write(0);
// delay(500);
// delay(20000000000);
}
}