-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmart-Pill-Bottle.ino
341 lines (300 loc) · 8.59 KB
/
Smart-Pill-Bottle.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include "DSRTCLib.h"
//-----------------------------------------------------------------------------------------------//
//----------------------------------Set time for alarm-------------------------------------------//
//-----------------------------------------------------------------------------------------------//
// Set time for alarms in 24-hour time, omitting leading zeros and without a colon
// example: {200,1414} for 2:00AM and 2:14PM;
int times[] = {200, 1414};
//----------------------------------------------------------------------------------------------//
//---------------------------------------static code--------------------------------------------//
// Logger Setup
const int chipSelect = 10;
// RTC Setup
int ledPin = 13; // LED connected to digital pin 13
int INT_PIN = 2; // INTerrupt pin from the RTC. On Arduino Uno, this should be mapped to digital pin 2 or pin 3, which support external interrupts
int int_number = 1; // On Arduino Uno, INT0 corresponds to pin 2, and INT1 to pin 3
DS1339 RTC = DS1339();
int counter = 0;
int checklow = 0;
int checkhigh = 0;
// Button and pager motor setup
const int TOUCH_BUTTON_PIN = 4; // Input pin for touch state
const int MOTOR = 3; // Motor is used to indicate an alarm
int alarm = 0;
int pilltaken = 0;
int numtimes = sizeof(times)/sizeof(times[0]);
// Global Variables
int buttonState = LOW; // Variable for reading button
int pills = 16;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(MOTOR, OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(MOTOR, LOW);
pinMode(TOUCH_BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
Wire.begin();
// Initialize SD
Serial.print("Initializing SD card...");
// Check if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// Open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile){
dataFile.println("......................");
dataFile.println("Date, Time, Count");
dataFile.close();
}
else
{
Serial.println("Couldn't open data file");
return;
}
// Initialize RTC
RTC.start(); // ensure RTC oscillator is running, if not already
// set_time(); //**COMMENT OUT THIS LINE AFTER YOU SET THE TIME**//
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(pills);
while (1){
// Getting current date and time
String ahoraDate;
String ahoraTime;
currentTime(ahoraDate, ahoraTime);
String ahora = ahoraDate + " " + ahoraTime;
buttonState = digitalRead(TOUCH_BUTTON_PIN);
// Turning alarm on, if it is the right time.
for(int i = 0; i<numtimes; i++){
if(int(RTC.getHours()) == int(times[i]/100)){
if(int(RTC.getMinutes()) == int(times[i])-int(times[i]/100)*100){
if(alarm == 0 && pilltaken == 0){
digitalWrite(MOTOR,HIGH);
alarm = 1;
Serial.println("alarm on");
}
}
}
}
// Check if a pill was missed
for(int i = 0; i<numtimes; i++){
if(int(RTC.getHours()) == int(times[i]/100)){
if(int(RTC.getMinutes()) == int(times[i])-int(times[i]/100)*100+1){
if(pilltaken == 0){
String dataString = "";
dataString = String(ahoraDate) + ", " + String(ahoraTime) + ", " + String(counter)+ ", " + "missed pill";
// If the file is available, write to it:
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if(dataFile){
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("Couldn't access file");
}
}
pilltaken = 0;
alarm = 0;
digitalWrite(MOTOR,LOW);
}
}
}
// Displaying number of pills
LedOn(pills);
// Handle situation data, when button was pressed.
// Namely logging, resetting alarm
if (buttonState == HIGH){
checklow ++;
if(checklow==80){
checkhigh = 0;
String dataString = "";
// Reducing the number
pills = pills-1;
if(pills==0){
pills = 16;
}
// Turn off alarm:
if(alarm == 1){
digitalWrite(MOTOR,LOW);
alarm = 0;
pilltaken =1;
}
// Logging
dataString = String(ahoraDate) + ", " + String(ahoraTime) + ", " + String(counter)+ ", " + String(pills);
// If the file is available, write to it:
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if(dataFile){
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else
{
Serial.println("Couldn't access file");
}
}
}
// Resetting checklow if conditions is meet
if(buttonState == LOW){
checkhigh++;
if (checkhigh>=80){
checklow = 0;
}
}
delay(20);
counter++;
}
}
int read_int(char sep)
{
static byte c;
static int i;
i = 0;
while (1)
{
while (!Serial.available())
{;}
c = Serial.read();
if (c == sep)
{
return i;
}
if (isdigit(c))
{
i = i * 10 + c - '0';
}
else
{
Serial.print("\r\nERROR: \"");
Serial.write(c);
Serial.print("\" is not a digit\r\n");
return -1;
}
}
}
int read_int(int numbytes)
{
static byte c;
static int i;
int num = 0;
i = 0;
while (1)
{
while (!Serial.available())
{;}
c = Serial.read();
num++;
if (isdigit(c))
{
i = i * 10 + c - '0';
}
else
{
Serial.print("\r\nERROR: \"");
Serial.write(c);
Serial.print("\" is not a digit\r\n");
return -1;
}
if (num == numbytes)
{
// Serial.print("Return value is");
// Serial.println(i);
return i;
}
}
}
int read_date(int *year, int *month, int *day, int *hour, int* minute, int* second)
{
*year = read_int(4);
*month = read_int(2);
*day = read_int(' ');
*hour = read_int(':');
*minute = read_int(':');
*second = read_int(2);
return 0;
}
void currentTime(String &nowDate, String &nowTime){
RTC.readTime();
nowDate = String(int(RTC.getMonths())) + "/" + String (int(RTC.getDays())) + "/" + String(RTC.getYears()-2000);
nowTime = String(int(RTC.getHours())) + ":" + String(int(RTC.getMinutes())) + ":" + String(int(RTC.getSeconds()));
}
void LedOn(int ledNum)
{
for(int i=5;i<10;i++){
pinMode(i, INPUT);
digitalWrite(i, LOW);
};
if(ledNum<1 || ledNum>16) return;
char highpin[16]={5,6,5,7,6,7,6,8,5,8,8,7,9,7,9,8};
char lowpin[16]= {6,5,7,5,7,6,8,6,8,5,7,8,7,9,8,9};
ledNum--;
digitalWrite(highpin[ledNum],HIGH);
digitalWrite(lowpin[ledNum],LOW);
pinMode(highpin[ledNum],OUTPUT);
pinMode(lowpin[ledNum],OUTPUT);
}
void set_time()
{
Serial.println("Enter date and time (YYYYMMDD HH:MM:SS)");
int year, month, day, hour, minute, second;
int result = read_date(&year, &month, &day, &hour, &minute, &second);
if (result != 0) {
Serial.println("Date not in correct format!");
return;
}
RTC.setSeconds(second);
RTC.setMinutes(minute);
RTC.setHours(hour);
RTC.setDays(day);
RTC.setMonths(month);
RTC.setYears(year);
RTC.writeTime();
read_time();
}
void read_time()
{
Serial.print ("The current time is ");
RTC.readTime();
printTime(0);
Serial.println();
}
void printTime(byte type)
{
if(!type)
{
Serial.print(int(RTC.getMonths()));
Serial.print("/");
Serial.print(int(RTC.getDays()));
Serial.print("/");
Serial.print(RTC.getYears());
}
else
{
{
Serial.print(int(RTC.getDayOfWeek()));
Serial.print("th day of week, ");
}
{
Serial.print(int(RTC.getDays()));
Serial.print("th day of month, ");
}
}
Serial.print(" ");
Serial.print(int(RTC.getHours()));
Serial.print(":");
Serial.print(int(RTC.getMinutes()));
Serial.print(":");
Serial.print(int(RTC.getSeconds()));
}