-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoNanoCode_v1.03.txt
362 lines (277 loc) · 10 KB
/
ArduinoNanoCode_v1.03.txt
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
/*
* Written by Jolyon Troscianko - 2022
*
*
* Released under GPL-3.0 license
*
* This code is developed for the Arduino nano, tested with Elegoo units and unbranded
* open this code in Arduino IDE to upload to the Arduino.
* use old bootloader 328p if the normal one doesn't work
*
* Project home: https://github.com/troscianko/OSpRad
* Code also inspired by this project: https://impfs.github.io/review/
* C12880MA datasheet: https://www.farnell.com/datasheets/2822646.pdf
*
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// THESE FOUR VALUES NEED TO BE CHANGED FOR YOUR INDIVIDUAL UNIT:
int unitNumber = 1; // Add a unit-specific number here. This number is looked up for applying calibration data
// To set these wheel positions, build your unit, upload this code, then use the serial monitor Baud Rate set to 115200
// write "w98" to manually contorl the shutter wheel
int posDark = 98; // angle for dark measurement
int posIrr = 146; // angle for irradiance (cosine diffuser)
int posRad = 57; // angle for radiance measurement (clear)
int servoDelay = 300; // millisecond delay for servo to move
int servoDetachDelay = 1500; // millisecond delay for servo to detach (causes feedback and noisy measurements)
int servoPin = 8;
#define TRGpin A0
#define STpin A1
#define CLKpin A2
#define VIDEOpin A3
#define nSites 288 //
uint16_t data[nSites] [2];
int dataSaveDim = 0;
int delayTime = 1;
long intTime = 100;
long prevIntTime = 100;
long maxAutoIntTime = 5000;
long maxIntTime = 60000; // maximum integration time for auto-measurement
long manIntTime = 0;
int satN = 0; // number of bands over-exposed
int satVal = 1000; // over-exposure value
int satSum = 0;
int maxVal = 0;
int prevMaxVal = 0;
int nScansMax = 50; // 65535 is the max uint16 data value, so can only deal with about 60 max
int nScansMin = 3;
long sampleTimeMax = 1000; // target sampling time for repeat scans
int nScans = 1;
int measureType = 0;
void setup(){
//myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
//Set desired pins to OUTPUT
pinMode(CLKpin, OUTPUT);
pinMode(STpin, OUTPUT);
digitalWrite(CLKpin, HIGH);
digitalWrite(STpin, LOW);
Serial.begin(115200); // Baud Rate set to 115200
while (! Serial); // Wait untilSerial is ready
//Serial.println("i = irradiance measure, r = radiance measure, n# = minimum number of scans");
readSpectrometer();
resetData();
}
void readSpectrometer(){
// Start clock cycle and set start pulse to signal start
digitalWrite(CLKpin, LOW);
delayMicroseconds(delayTime);
digitalWrite(CLKpin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(CLKpin, LOW);
digitalWrite(STpin, HIGH);
delayMicroseconds(delayTime);
unsigned long cTime = millis(); // start time
unsigned long eTime = cTime + intTime; // end time
//Sample for a period of time
// for(int i = 0; i < 15; i++){ //orig 15
while(cTime < eTime){
digitalWrite(CLKpin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(CLKpin, LOW);
delayMicroseconds(delayTime);
cTime=millis();
}
//Set STpin to low
digitalWrite(STpin, LOW);
//Sample for a period of time
for(int i = 0; i < 88; i++){ //87 aligns correctly
digitalWrite(CLKpin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(CLKpin, LOW);
delayMicroseconds(delayTime);
}
int specRead = 0;
satN = 0;
for(int i = 0; i < nSites; i++){
specRead = analogRead(VIDEOpin);
// second read shoudl stablise the multiplexer and give more accurate read
//delayMicroseconds(delayTime);
// specRead = analogRead(VIDEOpin);
data[i][dataSaveDim] += specRead;
if(specRead > satVal)
satN ++;
digitalWrite(CLKpin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(CLKpin, LOW);
delayMicroseconds(delayTime);
}
}
void resetData(){
for (int i = 0; i < nSites; i++)
data[i] [dataSaveDim] = 0;
}
void satTest(){
//satN = 0;
//maxVal = 0;
for (int i = 0; i < nSites; i++){
//if(data[i][dataSaveDim] > satVal)
// satN ++;
if(data[i][dataSaveDim] > maxVal)
maxVal = data[i][dataSaveDim];
}
}
void loop(){
String arg = Serial.readString();
if (arg != NULL){
// manually set integration time
if(arg.startsWith("t") == true){
arg.replace("t", "");
manIntTime = (long) arg.toFloat();
if(manIntTime > maxIntTime)
manIntTime = maxIntTime;
Serial.println("int. time: " + String(manIntTime) + "ms");
// change max number of scans
} else if(arg.startsWith("a") == true){
arg.replace("a", "");
nScansMax = (int) arg.toFloat();
if(nScansMax < nScansMin)
nScansMin = nScansMax;
Serial.println("max scans: " + String(nScansMax));
delay(100);
// change min number of scans
} else if(arg.startsWith("n") == true){
arg.replace("n", "");
nScansMin = (int) arg.toFloat();
if(nScansMin > nScansMax)
nScansMax = nScansMin;
Serial.println("min scans: " + String(nScansMin));
delay(100);
// manual wheel position
} else if(arg.startsWith("w") == true){ // filter wheel position
arg.replace("w", "");
int wheelAngle = (int) arg.toFloat();
myservo.attach(servoPin);
myservo.write(wheelAngle);
Serial.println("Wheel angle: " + String(wheelAngle));
delay(servoDelay);
myservo.detach();
// Spec measure
} else if(arg.startsWith("r") == true || arg.startsWith("i") == true){ // radiance
myservo.attach(servoPin);
if(arg.startsWith("r") == true){
myservo.write(posRad);
measureType = 1;
} else {
myservo.write(posIrr);
measureType = 0;
}
delay(servoDelay);
myservo.detach();
delay(servoDetachDelay);
nScans = 1;
// reset all data
dataSaveDim = 1; // 1= dark, 0=light
resetData();
dataSaveDim = 0;// must be left as 0 here for code below - temp light data
resetData();
// automatically work out integration time by increasing until saturation point, then estimate ideal value
if(manIntTime == 0){
satN = 0;
intTime = 1;
prevIntTime = 1;
maxVal = 0;
resetData(); // reset dim0 data
readSpectrometer(); // read to dim0
satTest();
if(satN == 0){ // if initial 1ms scan is over-exposed don't go any further
while(satN == 0){
prevMaxVal = maxVal;
prevIntTime = intTime;
intTime = intTime*2;
if(intTime > maxAutoIntTime) // do not go above max int time (takes ages to measure)
satN = 1;
else {
// copyData(); // save previous scan to dim1
resetData(); // reset dim0 data
readSpectrometer(); // read to dim0
satTest();
}
//Serial.println("int:" + String(intTime) + " maxVal:" + String(maxVal));
delay(10);
}
//intTime = prevIntTime; // the longest integration time that didn't saturate
resetData();
//Serial.println("prevInt:" + String(prevIntTime) + " prevMaxVal:" + String(prevMaxVal));
// ensure auto-value isn't too long
float tInt = floor(float(prevIntTime*0.9*satVal)/float(prevMaxVal));
if(tInt > maxIntTime)
intTime = maxIntTime;
else intTime = tInt;
}
} else { // initial scan with manual integration time
intTime = manIntTime;
}
nScans = floor(sampleTimeMax/intTime);
if(nScans < nScansMin)
nScans = nScansMin;
if(nScans > nScansMax)
nScans = nScansMax;
satSum = 0;
//-------------Integration time is short, collect sample data, then dark measurement---------
if(intTime < sampleTimeMax){
dataSaveDim = 1; // light data
for(int i=0; i<nScans; i++){ // repeatedly read spec (note one fewer scan than below because one scan is already done above)
readSpectrometer();
satTest();
satSum = satSum + satN;
//Serial.println("satN:" + String(satN) + " satSum:" + String(satSum));
delay(10);
}
myservo.attach(servoPin);
myservo.write(posDark);
delay(servoDelay);
myservo.detach();
delay(servoDetachDelay);
dataSaveDim = 0;// dark data
resetData();
for(int i=0; i<nScans; i++){ // repeatedly read spec
readSpectrometer();
delay(10);
}
//-------------Integration time is long, take interleaved dark measurements---------
} else {
for(int i=0; i<nScans; i++){
// light measuremeant
myservo.attach(servoPin);
if(measureType == 1){
myservo.write(posRad);
} else {
myservo.write(posIrr);
}
delay(servoDelay);
myservo.detach();
delay(servoDetachDelay);
dataSaveDim = 1; // light data
readSpectrometer(); // add light measurement
satTest();
satSum = satSum + satN;
delay(10);
myservo.attach(servoPin);
myservo.write(posDark);
delay(servoDelay);
myservo.detach();
delay(servoDetachDelay);
dataSaveDim = 0; // dark data
readSpectrometer(); // add dark measurement
}
}
Serial.print(String(unitNumber) + "," + String(measureType) + "," + String(nScans) + "," + String(intTime) + "," + String(float(satSum)/float(nScans)) );
for (int i = 0; i < nSites; i++){
Serial.print("," + String((float(data[i][1])-float(data[i][0] ))/float(nScans)));
}
Serial.print("\n");
delay(50);
}
}
delay(10);
}