blob: 694f8a3ce65838fab9f8e156303530768d615cb1 (
plain)
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
|
/*
____ __________________________________ ____
/ __ \/ ____/_ __/ ____/ ____/_ __/ __ \/ __ \
/ / / / __/ / / / __/ / / / / / / / / /_/ /
/ /_/ / /___ / / / /___/ /___ / / / /_/ / _, _/
/___________/ __________/\____/ _____\__________|
/ __ )/ / / / _/ / / __ \/ _/ | / / ____/
/ __ / / / // // / / / / // // |/ / / __
/ /_/ / /_/ // // /___/ /_/ // // /| / /_/ /
/_____/\____/___/_____/_____/___/_/ |_/\____/
Ladue Horton Watkins High School Science Olympiad
Licensed under the Parity Public License
*/
#include <curveFitting.h>
using ld = long double;
const int LED_R = 8, LED_G = 10, LED_B = 12, THERM = 0; // Device component pins
const ld R_k = 10000, V_in = 5, analog_max = 1023; // Device constants
// Analog to digital conversion
ld a2d(int a) { return V_in * a / analog_max; }
int d2a(ld d) { return d * analog_max / V_in; }
// Voltage to resistance conversion
ld v2r(ld V_out) { return R_k * (V_in / V_out - 1); }
ld vol[100];
int con[100];
const int order = 2;
int coeff[order + 1];
void setup() {
Serial.begin(9600);
Serial.println("Starting calibration")
Serial.println("Place sensor in water and enter the concentration into the console")
Serial.println("When you are finished, type c to continue")
int n = 0;
while (1) {
String s = Serial.readString();
if (s == "c") break;
vol[n] = a2d(analogRead(THERM));
con[n] = toInt(s);
Serial.println(n);
Serial.println(vol[n]);
Serial.println(con[n]);
++n;
}
fitCurve(order, n, vol, con, coeff);
}
void loop() {
v = a2d(analogRead(THERM));
c = 0;
for (int i = n; i >= 0; --i) c = v*c + coeff[i];
Serial.println(c);
}
|