1int buttonVal, toggleVal, cal = 0;
2int sample = 1;
3int buzzer = 13;
4int toggle = 8; int button = 7;
5int redPin = 9; int greenPin = 10; int bluePin = 11;
6int goodPin = 5; int statusPin = 6;
7int sensorPinOne = A0;
8int sensorPinTwo = A1;
9int cellOne[2][41]; int cellTwo[2][41];
10double wavelengths[41] = {380, 390, 400, 410, 420, 430, 440, 450,
11 460, 470, 480, 490, 500, 510, 520, 530,
12 540, 550, 560, 570, 580, 590, 600, 610,
13 620, 630, 640, 650, 660, 670, 680, 690,
14 700, 710, 720, 730, 740, 750, 760, 770, 780};
15double colors[41][3] =
16 { {97, 0, 97}, {121, 0, 141}, {131, 0, 181}, {126, 0, 219}, {106, 0, 255}, {61, 0, 255}, {0, 0, 255}, {0, 70, 255},
17 {0, 123, 255}, {0, 169, 255}, {0, 213, 255}, {0, 255, 255}, {0, 255, 146}, {0, 255, 0}, {54, 255, 0}, {94, 255, 0},
18 {129, 255, 0}, {163, 255, 0}, {195, 255, 0}, {218, 255, 0}, {255, 255, 0}, {255, 223, 0}, {255, 190, 0}, {255, 155, 0},
19 {255, 119, 0}, {255, 79, 0}, {255, 33, 0}, {255, 0, 0}, {232, 0, 0}, {210, 0, 0}, {188, 0, 0}, {165, 0, 0},
20 {143, 0, 0}, {237, 0, 0}, {219, 0, 0}, {200, 0, 0}, {181, 0, 0}, {161, 0, 0}, {141, 0, 0}, {119, 0, 0}, {97, 0, 0} };
21float transmittance[2]; float absorbance[2];
22unsigned long previousMillis = 0; const long statusInterval = 300;
23
24void setup() {
25 Serial.begin(9600);
26 pinMode(buzzer, OUTPUT);
27 pinMode(toggle, INPUT); pinMode(button, INPUT);
28 pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);
29 pinMode(goodPin, OUTPUT); pinMode(statusPin, OUTPUT);
30 pinMode(sensorPinOne, INPUT); pinMode(sensorPinTwo, INPUT);
31 Serial.println("--- MiniSpec ---"); Serial.println();
32 tone(buzzer, 400, 150);
33}
34
35void loop() {
36 toggleVal = digitalRead(toggle);
37 analogWrite(goodPin, HIGH); analogWrite(statusPin, LOW);
38 setColor(0, 0, 0);
39
40 if (toggleVal == 0) {
41 if (digitalRead(button) == 1) {
42 stabalize();
43 analogWrite(goodPin, LOW);
44 for (int x=0; x<41; x++) {
45 analogWrite(statusPin, HIGH);
46 setColor(colors[x][0], colors[x][1], colors[x][2]); delay(333);
47 cellOne[0][x] = analogRead(sensorPinOne); cellTwo[0][x] = analogRead(sensorPinTwo); delay(10);
48 }
49 Serial.println("Calibration Complete"); Serial.println();
50 cal++;
51 }
52 }
53
54 if (toggleVal == 1) {
55 if (digitalRead(button) == 1 && cal >= 1) {
56 stabalize();
57 analogWrite(goodPin, LOW);
58 for (int x=0; x<41; x++) {
59 analogWrite(statusPin, HIGH);
60 setColor(colors[x][0], colors[x][1], colors[x][2]); delay(333);
61 cellOne[1][x] = analogRead(sensorPinOne); cellTwo[1][x] = analogRead(sensorPinTwo); delay(10);
62 }
63 setColor(0,0,0);
64 analysis();
65 } else if (digitalRead(button) == 1 && cal == 0) {tone(buzzer, 100, 200);}
66 }
67}
68
69void stabalize(void) {
70 setColor(255,255,255);
71 for (int y=0; y<5; y++) {
72 analogWrite(goodPin, LOW); delay(300);
73 analogWrite(goodPin, HIGH); delay(300);
74 }
75}
76
77void setColor(double red, double green, double blue) {
78 analogWrite(redPin, (int)red);
79 analogWrite(greenPin, (int)green);
80 analogWrite(bluePin, (int)blue);
81}
82
83void analysis(void) {
84 Serial.println(); Serial.print("Sample "); Serial.println(sample); Serial.println();
85 previousMillis = millis();
86 for (int x=0; x<41; x++) {
87 transmittance[0] = (float)cellOne[1][x]/(float)cellOne[0][x];
88 transmittance[1] = (float)cellTwo[1][x]/(float)cellTwo[0][x];
89 absorbance[0] = log10(1/transmittance[0]);
90 absorbance[1] = log10(1/transmittance[1]);
91 float avgAbsorbance = ((absorbance[0] + absorbance[1])/(float)2);
92 Serial.println(avgAbsorbance);
93 delay(100);
94 if (millis() - previousMillis >= statusInterval) {
95 analogWrite(statusPin, LOW);
96 }
97 if (millis() - previousMillis >= statusInterval*2) {
98 analogWrite(statusPin, HIGH);
99 previousMillis = millis();
100 }
101 }
102 sample++;
103}