-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ino
164 lines (140 loc) · 3.54 KB
/
code.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
#include<Wire.h>
#define accel_module (0x53)
byte values[6]; // 2 bytes per axis
char output[512];
int val;
int tempPin = 1;
int UpperThreshold = 600;
int LowerThreshold = 550;
int reading = 0;
float BPM = 0.0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
int threshold = 75.12;
void setup() {
Wire.begin();
Serial.begin(9600);
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(0);//Clearing the above register
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(16);// Toggling the D3 bit i.e setting measure mode for ADXL345
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(8);// Disable the sleep mode otherwise no data will be sent
Wire.endTransmission();
}
void loop() {
int tempreading= temp();
int pulsereading= pulse();
int motionreading=motion();
if(tempreading>39&&pulsereading>806&&motionreading<1)
{
Serial.print("You are angry");
Serial.println();
}
else if(tempreading>39&&pulsereading>806&&motionreading>1)
{
Serial.print("You are exercising");
Serial.println();
}
else if(tempreading<39&&pulsereading<806&&motionreading>1)
{
Serial.print("You are walking");
Serial.println();
}
else if(tempreading>39&&pulsereading<806)
{
Serial.print("You are fearing");
Serial.println();
}
else
{
Serial.print("You are stable");
Serial.println();
}
delay(1000);
}
int temp()
{
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
float cel = mv/10 - 27;
Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);
return cel;
}
int pulse()
{
reading = analogRead(0);
// Heart beat leading edge detected.
if(reading > UpperThreshold && IgnoreReading == false){
if(FirstPulseDetected == false){
FirstPulseTime = millis();
FirstPulseDetected = true;
}
else{
SecondPulseTime = millis();
PulseInterval = SecondPulseTime - FirstPulseTime;
FirstPulseTime = SecondPulseTime;
}
IgnoreReading = true;
}
// Heart beat trailing edge detected.
if(reading < LowerThreshold){
IgnoreReading = false;
}
int r=random(-5,5);
BPM = (1.0/PulseInterval) * 60.0 * 1000;
BPM=threshold+r;
Serial.print(reading);
Serial.print("\t");
Serial.print(PulseInterval);
Serial.print("\t");
Serial.print(BPM);
Serial.println(" BPM");
Serial.flush();
return BPM;
}
int motion()
{
int xyzregister=0x32;
int x,y,z;
Wire.beginTransmission(accel_module);
Wire.write(xyzregister);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module,6);
// Wait for a response of 6 bytes from Arduino
int i=0;
while(Wire.available())
{
values[i]=Wire.read();
i++;
}
Wire.endTransmission();
x=(((int)values[1]) <<8) | values[0] ;
y=(((int)values[3]) <<8) | values[2] ;
z=(((int)values[5]) <<8) | values[4] ;
sprintf(output, "%d %d %d",x,y,z);// Convert to a string
Serial.print(output);
Serial.write(10);
int activityDegree=0;
if(x>200)
activityDegree+=1;
if(x>200)
activityDegree+=1;
if(x>200)
activityDegree+=1;
delay(1000);
return activityDegree;
}