-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino.ino
executable file
·305 lines (243 loc) · 8.52 KB
/
Arduino.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
#include <Servo.h>
/* IMPORTANT:
When manually presetting the wavelength (632.8nm in this case), always go up in value,
so for example, set it to 620 first, then turn it up to 632.8, for consistent backlash
(always finish on a positive rotation)
*/
// How long to let the camera write to card in milliseconds. Increase if an old camera is unable to keep up
#define CAMERA_BUFFERING_TIME 4000
/* Set camera to 6 seconds, so diode integrates within shutter beginning and ending */
#define DIODE_INTEGRATION_TIME 3750 /* Because TSL235 is terrible we must have such a long integration time */
/*********************************************
OLED display
*********************************************/
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
Adafruit_SSD1306 display(-1);
/*********************************************
Shutter presser
*********************************************/
Servo shutter_presser;
void PressShutter()
{
int angle = 0;
int max_angle = 40;
shutter_presser.write(max_angle);
delay(250);
shutter_presser.write(0);
delay(50); // CHANGE THIS BAC TO 70 and the first delay to 220 or smthng
}
/*********************************************
WAVELENGTH INFO
In 10 * nanometres
*********************************************/
#define WAVELENGTH_START_POS 6328 /* HeNe Laser calibration wavelength. Set to this wavelength manually before running everything! */
#define WAVELENGTH_START 3800 /* Will start taking measurements from this wavelength (380nm) */
#define WAVELENGTH_END 7500 /* Will keep measuring until this wavelength is reached (730nm) */
#define WAVELENGTH_STEP 20 /* Wavelength step, 2nm and 5nm are best */
#define STEPS_PER_REVOLUTION ((int32_t)200) /* For stepper */
#define WAVELENGTH_PER_REVOLUTION 250 /* 25nm is one full rotation */
/*********************************************
Filter wheel.
To hopefully improve monochromator output
*********************************************/
Servo filterwheel;
#define FilterWheelPos_CLOSED 34 /* An opaque filter consisting of foil, for getting dark frame and dark current before each measurement */
#define FilterWheelPos_NOFILTER 180 /* No filter, for wavelengths between 420 and 645nm */
#define FilterWheelPos_VIOLET_420 69 /* bandpass 380-420 (very sharp cutoffs) */
#define FilterWheelPos_RED_645 0 /* SChott RG645 for measuring wavelengths 645nm and over */
void SetFilterWheel(int FilterOption)
{
filterwheel.write(FilterOption);
/* Let the servo settle... */
delay(400);
}
/* Sets filter automatically for wavelength */
void SetFilterForWavelength(uint32_t wavelength)
{
if (wavelength >= 3800 && wavelength < 4200) {
SetFilterWheel(FilterWheelPos_VIOLET_420);
} else if (wavelength >= 6480) {
SetFilterWheel(FilterWheelPos_RED_645);
} else {
SetFilterWheel(FilterWheelPos_NOFILTER);
}
}
/*********************************************
Photodiode (TSL235)
*********************************************/
volatile uint32_t cnt = 0;
void interrupr() {
++cnt;
}
void DiodeInit()
{
pinMode(2, INPUT);
digitalWrite(2, HIGH);
}
uint32_t DiodeMeasure(uint32_t IntegrationTime)
{
cnt = 0;
attachInterrupt(0, interrupr, CHANGE);
delay(IntegrationTime);
detachInterrupt(0);
return cnt;
}
/*********************************************
Stepper motor (A4988 controller configured for microstepping)
*********************************************/
int stepPin = 12;
int dirPin = 13;
int enPin = 10;
int32_t steps = 0;
void StepperStep(int32_t numsteps)
{
/* All values between 60 and 250 seem ok, I chose this lower
speed because it seems to vibrate less than others, there's
probably a better value, I didn't spend long choosing */
const int steptime = 220;
steps += numsteps;
// Set the spinning direction
if (numsteps < 0) {
digitalWrite(dirPin, LOW);
numsteps = -numsteps;
} else {
digitalWrite(dirPin, HIGH);
}
delay(50);
// Spin the stepper motor
for (int32_t s = 0; s < numsteps * 16; s++)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(steptime);
digitalWrite(stepPin, LOW);
delayMicroseconds(steptime);
}
delay(5);
}
void StepperInit()
{
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin , OUTPUT);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite( enPin , HIGH);
}
void SetWavelength(int32_t TargetWavelength)
{
double current_wavelength = WAVELENGTH_START_POS + ((double)steps * (double)WAVELENGTH_PER_REVOLUTION) / (double)STEPS_PER_REVOLUTION;
double wl_diff = (double)TargetWavelength - current_wavelength;
double steps_required = ((double)wl_diff * (double)STEPS_PER_REVOLUTION) / (double)WAVELENGTH_PER_REVOLUTION;
int32_t steps_required_int = steps_required;
if (steps_required < 0) steps_required_int = (int32_t)(steps_required - 0.5);
else steps_required_int = (int32_t)(steps_required + 0.5);
/* Backlash correction - always finish on a positive rotation */
int32_t backlash_steps = 50;
if (steps_required < 0)
{
StepperStep(steps_required - backlash_steps);
delay(20);
StepperStep(+backlash_steps);
}
else
{
StepperStep(steps_required);
}
}
/*********************************************
* General functions
*********************************************/
// Reads diode, outputs to serial and presses the shutter
void TakeReading()
{
/* Camera reading */
PressShutter();
/* Diode reading */
uint32_t diode_value = DiodeMeasure(DIODE_INTEGRATION_TIME);
Serial.println(diode_value);
/* Allow camera to write or whatever */
delay(CAMERA_BUFFERING_TIME);
}
void setup()
{
Serial.begin(9600);
/****************** TSL235 diode ******************/
DiodeInit();
/****************** FILTER WHEEL ******************/
filterwheel.attach(5);
SetFilterWheel(FilterWheelPos_CLOSED); /* Keep mostly closed cause the lamp makes a lot of heat */
//SetFilterWheel(FilterWheelPos_RED_645);
//SetFilterWheel(FilterWheelPos_VIOLET_420);
//while(1);
/****************** SHUTTER PRESSER ******************/
shutter_presser.attach(4);
shutter_presser.write(0);
/****************** STEPPER ******************/
StepperInit();
uint32_t original_pos = WAVELENGTH_START_POS;
/****************** DISPLAY ******************/
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(2,2);
display.print("Plug in stepper motor NOW");
display.display();
/* wait (time to plug in stepper) */
delay(8000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(2,2);
display.print("Setting start wavelength");
display.display();
/*********************************************
Take actual measurements now
*********************************************/
if (WAVELENGTH_START-WAVELENGTH_STEP > 3000)
SetWavelength(WAVELENGTH_START-WAVELENGTH_STEP);
else
SetWavelength(300);
uint32_t time_start = millis();
for (int32_t wl = WAVELENGTH_START; wl <= WAVELENGTH_END; wl += WAVELENGTH_STEP)
{
/* Set wavelength */
SetWavelength(wl);
/* Dark reading */
TakeReading();
/* Main reading */
SetFilterForWavelength(wl);
TakeReading();
SetFilterWheel(FilterWheelPos_CLOSED);
uint32_t time_now = millis();
uint32_t time_per_reading = (time_now - time_start) / ((wl-WAVELENGTH_START)/WAVELENGTH_STEP + 1);
uint32_t time_left = time_per_reading * ((WAVELENGTH_END-wl)/WAVELENGTH_STEP);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(2,2);
display.print("Minutes left: ");
display.print(((double)time_left)/60000.0);
display.setCursor(2,12);
display.print("Wavelength: ");
display.print(((double)wl)/10.0);
display.display();
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(2,2);
display.print("Finished!");
display.display();
SetFilterWheel(FilterWheelPos_CLOSED);
SetWavelength(original_pos);
while (1);
}
void loop()
{
}