-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacroRail.ino
More file actions
249 lines (217 loc) · 4.21 KB
/
MacroRail.ino
File metadata and controls
249 lines (217 loc) · 4.21 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
#define BLYNK_PRINT SwSerial
#define BLYNK_MSG_LIMIT 0
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "32HTfa1ZP7Cw4sNYdYoCvixtjXBKw5YC"; /*
defines pins numbers
*/
const int stepPin = 3;
const int dirPin = 4;
const int shutterPin =5;
bool moveForward = false;
bool moveBackward = false;
const double pitch = 2; // pitch in mm
const double stepAngle = 1.8; // angle per step
double distance = 0;
double numImages = 0;
bool runIt = false;
bool autoMode = false;
double startStep = 0;
double endStep = 0;
double currentStep = 0;
void terminalout(String out)
{
Blynk.virtualWrite(V2, out + "\n");
}
void setup()
{
SwSerial.begin(9600);
/*
Sets the two pins as Outputs
*/
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(shutterPin,OUTPUT);
Serial.begin(9600);
Blynk.begin(Serial, auth);
}
void takeImage()
{
// stabilise period
delay(1000);
digitalWrite(shutterPin, HIGH);
delay(500);
digitalWrite(shutterPin, LOW);
// TODO : do shutter things
}
void loop()
{
Blynk.run();
if (moveForward)
{
move(true);
}
if (moveBackward)
{
move(false);
}
if (runIt)
{
runIt = false; // stop it running more than once
runProcess();
}
}
void move(bool forwards)
{
if (forwards)
digitalWrite(dirPin, HIGH);
else
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delay(2);
digitalWrite(stepPin, LOW);
delay(2);
if (forwards)
currentStep++;
else
currentStep--;
}
void runProcess()
{
terminalout("Running process");
if (autoMode)
{
terminalout("Auto mode");
// check that start is before end
if (startStep < endStep)
{
// we are ok to go
double numberOfStepsTotal = endStep - startStep;
double numberOfStepsPerShot = numberOfStepsTotal / numImages;
// move the carriage to the start if it's not already there
terminalout("Start Step = " + String(startStep,0));
terminalout("End Step = " + String(endStep,0));
terminalout("Number of images = " + String(numImages,0));
while (currentStep > startStep)
{
move(false);
}
for (int i = 0; i < numImages; i++)
{
terminalout("Taking image " + String(i) + " at location " + String(currentStep,0));
takeImage();
terminalout("Image taken");
for (int j = 0; j < numberOfStepsPerShot; j++)
{
move(true);
}
terminalout("Moved to next image");
}
terminalout("finished");
}
}
else
{
terminalout("Manual Mode");
// move forward
digitalWrite(dirPin, HIGH);
// calculate amount to move per step
double distanceToMove = distance / numImages;
// so need to move "distance to move" numImages times
// convert distance to move to steps
double eachstep = pitch / (360.0 / stepAngle);
double stepsPerDistance = distanceToMove / eachstep;
for (int i = 0; i < numImages; i++)
{
takeImage();
for (int j = 0; j < stepsPerDistance; j++)
{
move(true);
}
}
}
}
BLYNK_CONNECTED()
{
Blynk.syncAll();
}
/*
move forward
*/
BLYNK_WRITE(V0)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
moveForward = true;
}
else
{
moveForward = false;
}
}
// move backwards
BLYNK_WRITE(V1)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
moveBackward = true;
}
else
{
moveBackward = false;
}
}
// distance
BLYNK_WRITE(V3)
{
distance = param.asInt();
}
// num images
BLYNK_WRITE(V4)
{
numImages = param.asInt();
}
// go button on image and distance
BLYNK_WRITE(V5)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
runIt = true;
terminalout("Runit\n");
}
}
// manual or auto
BLYNK_WRITE(V6)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
autoMode = true;
}
else
autoMode = false;
}
// set start
BLYNK_WRITE(V7)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
startStep = currentStep;
}
}
// set end
BLYNK_WRITE(V8)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
endStep = currentStep;
}
}