/
Hack_Duke_Yourinalysis.ino
95 lines (71 loc) · 1.6 KB
/
Hack_Duke_Yourinalysis.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
//Resistance Variables
const int analogPin= A0;
int raw= 0;
int Vin= 3.3;
int initialResistance;
int finalResistance;
float Vout= 0;
float R1= 1000;
float R2= 0;
float buffer= 0;
String myResistance;
//Water Level variables
const int waterPin = A1;
int rawWaterLevel = 0;
int initialWaterLevel;
int finalWaterLevel;
String myWaterLevel;
const int buttonPin = D0;
int buttonState = 0;
float densityMetric = 0;
String myDensityMetric;
String hydrationStatus;
int getResistance()
{
raw= analogRead(analogPin);
buffer= raw * Vin;
Vout= (buffer)/4096.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
R2 = R2;
return R2;
}
void setup()
{
//Sensors setup
pinMode(analogPin, INPUT);
pinMode(analogPin, INPUT);
//Button setup
pinMode(buttonPin, INPUT);
}
void loop()
{
//Button Checker
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
//initial resistance measure
initialResistance = getResistance();
//Initial Water Level measure
initialWaterLevel = analogRead(waterPin);
delay(10000);
//Final reading
finalResistance = getResistance();
//Water Level measure
finalWaterLevel = analogRead(waterPin);
float diffResistance = abs(finalResistance-initialResistance);
float diffWaterLevel = 2*abs(finalWaterLevel-initialWaterLevel);
densityMetric = diffResistance/diffWaterLevel;
myDensityMetric = String(densityMetric);
if(densityMetric > 4)
{
hydrationStatus = "You are dehydrated, try to drink more water";
}
else
{
hydrationStatus = "You are hydrated, great job!";
}
Particle.publish("Output", hydrationStatus, 60, PRIVATE);
Particle.publish("Ion Density:", myDensityMetric, 60, PRIVATE);
}
delay(150);
}