-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaily_health_checker.ino
206 lines (169 loc) · 4.85 KB
/
daily_health_checker.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Imports
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SD.h>
#include "DHT.h"
// Constants
#define DHTPIN 7
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Declare Liquid Crystal Display Object
LiquidCrystal_I2C lcd(0x3F,16,2);
const int chipSelect = 4;
File sdcard_file;
const int TouchPin=6; //touch switch attach to pin1
const int ledPin = 13; //pin13 built-in led
int TouchState=0; //store the value of touch switch
int PreviousTouchState=-1; //stores the previous touch state value
int ButtonState=0; //store the value of the button switch
boolean recordTempMode = false; //If false use button to cycle temperature. if true, take through temperature taking mode.
void setup(void)
{
// start serial port
Serial.begin(9600);
//Setup for LCD
lcd.init(); //initialize the lcd
lcd.backlight(); //turn on the backlight
//Setup for SD Card
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("Card initialized.");
sdcard_file = SD.open("data.csv", FILE_WRITE); // open data.csv in SD card
// if the file is available, write to it:
if (sdcard_file) {
sdcard_file.println("Temperature,Humidity");
sdcard_file.close();
}
else
{
Serial.println("error opening data.csv");
}
//Setup for Touch Switch
pinMode(TouchPin,INPUT);//set sensorPin as INPUT
pinMode(ledPin,OUTPUT); // set ledPin as OUTPUT
dht.begin();
recordTempMode=false; //Assume that we are not going to record temperature. Someone can cycle through the previous temps recorded on SD card using button.
PreviousTouchState=digitalRead(TouchPin);//read the value of pin6
Serial.println("Previous TouchState");
Serial.println(PreviousTouchState);
displayIntro();
}
void loop(void)
{
TouchState=digitalRead(TouchPin);//read the value of pin6
if (TouchState != PreviousTouchState)
{
PreviousTouchState = TouchState;
lcd.clear();
//If recordTempMode is true then we are in the recording process and this cannot be interrupted. This why else is DO Nothing
//If recordTempMode is false then make true to start the Record Temp process
if (recordTempMode == false)
recordTempMode = true;
}
if(recordTempMode==true)
{
recordTemp();
}
else
{
displayIntro();
}
}
void recordTemp()
{
//Turn on LED to show that Temperature will now be recorded
digitalWrite(ledPin,HIGH); //turn on the led
Serial.println("Requesting temperature and humidity...");
float h = dht.readHumidity(); //read humidity before user puts hand on sensor (takes humidity of environment)
lcd.setCursor(0,0); // set the cursor to column 15, line 0
lcd.print("Hold for 20 sec");
delay(3000);
lcd.clear();
for (int c = 19; c > 0; c--)
{
lcd.print(c);
delay(1000);
lcd.clear();
}
float t = dht.readTemperature() + 10; //add 10 degrees to calibrate
if (isnan(h) || isnan(t))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Temperature in Celsius: ");
Serial.print(t);
Serial.println(" C");
Serial.print("Humidity:");
Serial.print(h, 2);
Serial.print("%");
lcd.clear();
lcd.setCursor(0,0); // set the cursor to column 0, line 0
lcd.print("Temp:");
lcd.print(t, 2);
lcd.print(char(223));
lcd.print("C");
lcd.setCursor(0,1); // set the cursor to column 0, line 1
lcd.print("Humidity:");
lcd.print(h, 2);
lcd.print("%");
delay(5000);
lcd.clear();
writeToSD(t,h);
if (t < 36)
{
lcd.setCursor(0,0);
lcd.print("Temp is low");
lcd.setCursor(0,1);
lcd.print("Call doctor");
delay(3000);
lcd.clear();
}
else if (36 <= t < 37)
{
lcd.setCursor(0,0);
lcd.print("Temp is fine");
delay(3000);
lcd.clear();
}
else
{
lcd.setCursor(0,0);
lcd.print("Temp is high");
lcd.setCursor(0,1);
lcd.print("Call doctor");
delay(3000);
lcd.clear();
}
lcd.setCursor(0,0);
lcd.print("Data recorded");
delay(3000);
lcd.clear();
recordTempMode = false;
//lcd.setCursor(0,0);
//lcd.print("Touch to quit");
displayIntro;
}
void writeToSD(float t, float h)
{
sdcard_file = SD.open("data.csv", FILE_WRITE);
// if the file is available, write to it:
if (sdcard_file) {
sdcard_file.println(String(t) + "," + String(h));
sdcard_file.close();
}
else
{
Serial.println("error opening data.csv");
}
}
void displayIntro()
{
digitalWrite(ledPin,LOW); //turn off the led
lcd.setCursor(0,0); // set the cursor to column 0, line 0
lcd.print("Touch to begin"); // Print a message to the LCD
delay(3000); // 3 second delay
}