-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOLED.ino
298 lines (238 loc) · 9.79 KB
/
OLED.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
/*
CosmicWatch Desktop Muon Detector Arduino Code
This code does not use the microSD card reader/writer, but does used the OLED screen.
Questions?
Spencer N. Axani
saxani@mit.edu
Requirements: Sketch->Include->Manage Libraries:
SPI, EEPROM, SD, and Wire are probably already installed.
1. Adafruit SSD1306 -- by Adafruit Version 1.0.1
2. Adafruit GFX Library -- by Adafruit Version 1.0.2
3. TimerOne -- by Jesse Tane et al. Version 1.1.0
*/
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <TimerOne.h>
#include <Wire.h>
#include <SPI.h>
#include <EEPROM.h>
const byte OLED = 1; // Turn on/off the OLED [1,0]
const int SIGNAL_THRESHOLD = 50; // Min threshold to trigger on. See calibration.pdf for conversion to mV.
const int RESET_THRESHOLD = 25;
const int LED_BRIGHTNESS = 255; // Brightness of the LED [0,255]
const long double cal[] = {-9.085681659276021e-27, 4.6790804314609205e-23, -1.0317125207013292e-19,
1.2741066484319192e-16, -9.684460759517656e-14, 4.6937937442284284e-11, -1.4553498837275352e-08,
2.8216624998078298e-06, -0.000323032620672037, 0.019538631135788468, -0.3774384056850066, 12.324891083404246};
const int cal_max = 1023;
//INTERUPT SETUP
#define TIMER_INTERVAL 1000000 // Every 1,000,000 us the timer will update the OLED readout
//OLED SETUP
#define OLED_RESET 10
Adafruit_SSD1306 display(OLED_RESET);
//initialize variables
char detector_name[40];
unsigned long time_stamp = 0L;
unsigned long measurement_deadtime = 0L;
unsigned long time_measurement = 0L; // Time stamp
unsigned long interrupt_timer = 0L; // Time stamp
int start_time = 0L; // Reference time for all the time measurements
unsigned long total_deadtime = 0L; // total measured deadtime
unsigned long waiting_t1 = 0L;
unsigned long measurement_t1;
unsigned long measurement_t2;
float sipm_voltage = 0;
long int count = 0L; // A tally of the number of muon counts observed
float last_sipm_voltage = 0;
float temperatureC;
byte waiting_for_interupt = 0;
byte SLAVE;
byte MASTER;
byte keep_pulse = 0;
void setup() {
analogReference (EXTERNAL);
ADCSRA &= ~(bit (ADPS0) | bit (ADPS1) | bit (ADPS2)); // clear prescaler bits
ADCSRA |= bit (ADPS0) | bit (ADPS1); // Set prescaler to 8
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(3, OUTPUT);
pinMode(6, INPUT);
if (digitalRead(6) == HIGH) {
SLAVE = 1;
MASTER = 0;
digitalWrite(3,HIGH);
delay(1000);}
else{
delay(10);
MASTER = 1;
SLAVE = 0;
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);}
if (OLED == 1){
display.setRotation(2); // Upside down screen (0 is right-side-up)
OpeningScreen(); // Run the splash screen on start-up
delay(2000); // Delay some time to show the logo, and keep the Pin6 HIGH for coincidence
display.setTextSize(1);}
else {delay(2000);}
digitalWrite(3,LOW);
if (MASTER == 1) {digitalWrite(6, LOW);}
Serial.println(F("##########################################################################################"));
Serial.println(F("### CosmicWatch: The Desktop Muon Detector"));
Serial.println(F("### Questions? saxani@mit.edu"));
Serial.println(F("### Comp_date Comp_time Event Ardn_time[ms] ADC[0-1023] SiPM[mV] Deadtime[ms] Temp[C] Name"));
Serial.println(F("##########################################################################################"));
get_detector_name(detector_name);
Serial.println(detector_name);
get_time();
delay(900);
start_time = millis();
Timer1.initialize(TIMER_INTERVAL); // Initialise timer 1
Timer1.attachInterrupt(timerIsr); // attach the ISR routine
}
void loop()
{
while (1){
int adc_fake = (rand()*1023);
if (/*analogRead(A0)*/ adc_fake > SIGNAL_THRESHOLD){
// Make a measurement of the pulse amplitude
int adc = adc_fake /*analogRead(A0)*/;
// If Master, send a signal to the Slave
if (MASTER == 1) {
digitalWrite(6, HIGH);
count++;
keep_pulse = 1;}
// Wait for ~8us
analogRead(A3);
// If Slave, check for signal from Master
if (SLAVE == 1){
if (digitalRead(6) == HIGH){
keep_pulse = 1;
count++;}}
// Wait for ~8us
analogRead(A3);
// If Master, stop signalling the Slave
if (MASTER == 1) {
digitalWrite(6, LOW);}
// Measure the temperature, voltage reference is currently set to 3.3V
temperatureC = (((analogRead(A3)+analogRead(A3)+analogRead(A3))/3. * (3300./1024)) - 500.)/10. ;
// Measure deadtime
measurement_deadtime = total_deadtime;
time_stamp = millis() - start_time;
// If you are within 15 miliseconds away from updating the OLED screen, we'll let if finish
if((interrupt_timer + 1000 - millis()) < 15){
waiting_t1 = millis();
waiting_for_interupt = 1;
delay(30);
waiting_for_interupt = 0;}
measurement_t1 = micros();
if (MASTER == 1) {
analogWrite(3, LED_BRIGHTNESS);
sipm_voltage = get_sipm_voltage(adc);
last_sipm_voltage = sipm_voltage;
Serial.println((String)count + " " + time_stamp+ " " + adc+ " " + sipm_voltage+ " " + measurement_deadtime+ " " + temperatureC);}
if (SLAVE == 1) {
if (keep_pulse == 1) {
analogWrite(3, LED_BRIGHTNESS);
sipm_voltage = get_sipm_voltage(adc);
last_sipm_voltage = sipm_voltage;
Serial.println((String)count + " " + time_stamp+ " " + adc+ " " + sipm_voltage + " " + measurement_deadtime+ " " + temperatureC);}}
keep_pulse = 0;
digitalWrite(3, LOW);
while(/*analogRead(A0)*/ adc_fake > RESET_THRESHOLD){continue;}
total_deadtime += (micros() - measurement_t1) / 1000.;}}
}
void timerIsr()
{
interrupts();
interrupt_timer = millis();
if (waiting_for_interupt == 1){
total_deadtime += (millis() - waiting_t1);}
waiting_for_interupt = 0;
if (OLED == 1){
get_time();}
}
void get_time()
{
unsigned long int OLED_t1 = micros();
float count_average = 0;
float count_std = 0;
if (count > 0.) {
count_average = count / ((interrupt_timer - start_time - total_deadtime) / 1000.);
count_std = sqrt(count) / ((interrupt_timer - start_time - total_deadtime) / 1000.);}
else {
count_average = 0;
count_std = 0;}
display.setCursor(0, 0);
display.clearDisplay();
display.print(F("Total Count: "));
display.println(count);
display.print(F("Uptime: "));
int minutes = ((interrupt_timer - start_time) / 1000 / 60) % 60;
int seconds = ((interrupt_timer - start_time) / 1000) % 60;
char min_char[4];
char sec_char[4];
sprintf(min_char, "%02d", minutes);
sprintf(sec_char, "%02d", seconds);
display.println((String) ((interrupt_timer - start_time) / 1000 / 3600) + ":" + min_char + ":" + sec_char);
if (count == 0) {
display.println("Hi, I'm "+(String)detector_name);
}
//if (MASTER == 1) {display.println(F("::--- MASTER ---::"));}
//if (SLAVE == 1) {display.println(F("::--- SLAVE ---::"));}}
else{
if (last_sipm_voltage > 180){
display.print(F("===---- WOW! ----==="));}
else{
if (MASTER == 1) {display.print(F("M"));}
if (SLAVE == 1) {display.print(F("S"));}
for (int i = 1; i <= (last_sipm_voltage + 10) / 10; i++) {display.print(F("-"));}}
display.println(F(""));}
char tmp_average[4];
char tmp_std[4];
int decimals = 2;
if (count_average < 10) {decimals = 3;}
dtostrf(count_average, 1, decimals, tmp_average);
dtostrf(count_std, 1, decimals, tmp_std);
display.print(F("Rate: "));
display.print((String)tmp_average);
display.print(F("+/-"));
display.println((String)tmp_std);
display.display();
total_deadtime += (micros() - OLED_t1 +73)/1000.;
}
void OpeningScreen(void)
{
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(8, 0);
display.clearDisplay();
display.print(F("Cosmic \n Watch"));
display.display();
display.setTextSize(1);
display.clearDisplay();
}
// This function converts the measured ADC value to a SiPM voltage via the calibration array
float get_sipm_voltage(float adc_value)
{
float voltage = 0;
for (int i = 0; i < (sizeof(cal)/sizeof(float)); i++) {
voltage += cal[i] * pow(adc_value,(sizeof(cal)/sizeof(float)-i-1));
}
return voltage;
}
// This function reads the EEPROM to get the detector ID
boolean get_detector_name(char* det_name)
{
byte ch; // byte read from eeprom
int bytesRead = 0; // number of bytes read so far
ch = EEPROM.read(bytesRead); // read next byte from eeprom
det_name[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
while ( (ch != 0x00) && (bytesRead < 40) && ((bytesRead) <= 511) )
{
ch = EEPROM.read(bytesRead);
det_name[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
}
if ((ch != 0x00) && (bytesRead >= 1)) {det_name[bytesRead - 1] = 0;}
return true;
}