-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitration.ino
66 lines (54 loc) · 1.6 KB
/
Titration.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
// The Titration Machine
// (c) Nick DeGroot 2018
// Created for the LOHS AP Chemistry final project.
// Hardcoded for HCl + NaOH --> H2O + NaCl, or any 1:1 acid base ratio
#include "VernierLib.h"
VernierLib Vernier;
// ACID BASE VALUES
// CHANGE THESE BASED ON EXPERIMENT
const float mlAcid = 50.0;
const float molarBase = 0.1;
const float mlPerDrop = 0.045;
const float dropOffset = 20.0;
// Global Values
float mlBase = mlPerDrop * (-1.0 * dropOffset);
float molarAcid = 0.0;
float pH = 0.0;
bool gateFlipped = false;
float highestDeriv = 0.0;
void setup() {
Vernier.autoID();
pH = Vernier.readSensor();
Serial.begin(9600);
Serial.print("Initial pH: ");
Serial.println(pH);
}
void loop() {
float sensorPh = Vernier.readSensor(); // Getting pH
int photogate = digitalRead(2); // Low when blocked
if (photogate == LOW) {
if (!gateFlipped) {
gateFlipped = true;
mlBase += mlPerDrop;
float firstDeriv = (sensorPh - pH) / mlPerDrop;
if (highestDeriv < firstDeriv) {
highestDeriv = firstDeriv;
// Calculating predicted molarity for current values
float molBase = (mlBase / 1000) * molarBase;
float molAcid = molBase;
molarAcid = molAcid / (mlAcid / 1000);
}
// Setting global values
pH = sensorPh;
// Printing update to serial and LCD
Serial.print("pH: ");
Serial.print(pH, 5); // 5 Sig Figs
Serial.print("\tmL of Base: ");
Serial.print(mlBase);
Serial.print("\tPredicted acid molarity: ");
Serial.println(molarAcid, 5); // 5 Sig Figs
}
} else {
gateFlipped = false;
}
}