89 lines
2.4 KiB
C++
89 lines
2.4 KiB
C++
/*
|
|
____ __________________________________ ____
|
|
/ __ \/ ____/_ __/ ____/ ____/_ __/ __ \/ __ \
|
|
/ / / / __/ / / / __/ / / / / / / / / /_/ /
|
|
/ /_/ / /___ / / / /___/ /___ / / / /_/ / _, _/
|
|
/___________/ __________/\____/ _____\__________|
|
|
/ __ )/ / / / _/ / / __ \/ _/ | / / ____/
|
|
/ __ / / / // // / / / / // // |/ / / __
|
|
/ /_/ / /_/ // // /___/ /_/ // // /| / /_/ /
|
|
/_____/\____/___/_____/_____/___/_/ |_/\____/
|
|
|
|
Ladue Horton Watkins High School Science Olympiad
|
|
|
|
Licensed under the Parity Public License
|
|
*/
|
|
|
|
|
|
#include <curveFitting.h>
|
|
|
|
|
|
const int LED_R = 8, LED_G = 10, LED_B = 12, THERM = 0; // Device component pins
|
|
const double R_k = 10000, V_in = 5, analog_max = 1023; // Device constants
|
|
|
|
// Analog to digital conversion
|
|
double a2d(int a) { return V_in * a / analog_max; }
|
|
int d2a(double d) { return d * analog_max / V_in; }
|
|
|
|
// Voltage to resistance conversion
|
|
double v2r(double V_out) { return R_k * (V_in / V_out - 1); }
|
|
|
|
double vol[100], con[100];
|
|
|
|
const int order = 1;
|
|
double coeff[order + 1];
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Serial.setTimeout(1000000);
|
|
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.readStringUntil('\n');
|
|
if (s == "c") break;
|
|
vol[n] = a2d(analogRead(THERM));
|
|
con[n] = s.toInt();
|
|
Serial.println(n);
|
|
Serial.println(vol[n]);
|
|
Serial.println(con[n]);
|
|
++n;
|
|
}
|
|
|
|
Serial.println(fitCurve(order, n, vol, con, order + 1, coeff));
|
|
for (int i = 0; i <= order; ++i) Serial.println(coeff[i]);
|
|
Serial.println("Calibration done");
|
|
}
|
|
|
|
void loop() {
|
|
Serial.println("Testing");
|
|
|
|
double v = a2d(analogRead(THERM));
|
|
Serial.print("Voltage: ");
|
|
Serial.println(v);
|
|
|
|
double c = 0;
|
|
for (int i = 0; i <= order; ++i) c = v*c + coeff[i];
|
|
|
|
Serial.print("Concentration: ");
|
|
Serial.println(c);
|
|
|
|
if (c <= 50000) {
|
|
digitalWrite(LED_R, LOW);
|
|
digitalWrite(LED_G, LOW);
|
|
digitalWrite(LED_B, HIGH);
|
|
}
|
|
else if (c <= 15000) {
|
|
digitalWrite(LED_R, LOW);
|
|
digitalWrite(LED_G, HIGH);
|
|
digitalWrite(LED_B, LOW);
|
|
}
|
|
else {
|
|
digitalWrite(LED_R, HIGH);
|
|
digitalWrite(LED_G, LOW);
|
|
digitalWrite(LED_B, LOW);
|
|
}
|
|
}
|