Components and supplies
plush cloth for decoration
battery 1.5V
SG90 Micro-servo motor
Jumper wires (generic)
Arduino UNO
pulse sensor/heartrate sensor
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
open source code
arduino
1#define USE_ARDUINO_INTERRUPTS true 2 3#include <Servo.h> 4#include <PulseSensorPlayground.h> 5 6// Pulse Sensor 7const int PulseWire = A0; 8int Threshold = 550; 9 10// AdjustPulseSensor 11int count=0; 12int test_heartrate[10]; 13int u=0; 14int IdealAverage=70; 15int BPM_current=0; 16int BPM_former=0; 17 18// Servo 19int pos = 0; 20int minus=0; 21int pos_9=minus-pos; 22 23 24// ultrasonic 25const int TrigPin=3; 26const int EchoPin=4; 27float distance; 28boolean distance_judge=false; 29int count2=0; 30int count3=0; 31 32// buzzer 33int buzzer=12; 34 35//LED 36const int redPin = A1; // 红色引脚 37const int greenPin = A3; // 绿色引脚 38const int bluePin = A5; // 蓝色引脚 39 40// Initialize 41Servo servo_9; 42Servo servo_10; 43PulseSensorPlayground pulseSensor; 44 45 46void setup() { 47 // Open Monitor 48 Serial.begin(9600); 49 50 // PulseSensor 51 pulseSensor.analogInput(PulseWire); 52 pulseSensor.setThreshold(Threshold); 53 54 if (pulseSensor.begin()) { 55 Serial.println("Pulse Sensor Start!");\\ 56 } 57 58 // ultrasonic 59 pinMode(TrigPin,OUTPUT); 60 pinMode(EchoPin,INPUT); 61 62 // buzzer 63 pinMode(buzzer,OUTPUT); 64 65 // Servo 66 servo_9.attach(9, 500,2500); 67 servo_10.attach(10, 500,2500); 68 69 servo_9.write(0); 70 servo_10.write(30); 71 72} 73 74void loop() { 75 76 // Heartrate 77 BPM_current= AdjustPulseSensor(); 78 79 if(BPM_current==0){ 80 BPM_current=BPM_former; 81 } 82 83 if(count>10 && Ultrasonic()){ 84 if(BPM_current<80){ 85 sweep2(); 86 show(0); 87 }else{ 88 if(BPM_current<110){ 89 sweep1(); 90 show(1); 91 }else{ 92 swing(); 93 show(2); 94 } 95 } 96 } 97 98 BPM_former=BPM_current; 99} 100 101 102// Heartrate 103int AdjustPulseSensor(){ 104 105 int BPM=pulseSensor.getBeatsPerMinute(); //PulseSensorPlayground自带函数 106 107 if(pulseSensor.sawStartOfBeat() && BPM<130 && BPM>50){ 108 count+=1; //计数器 109 if(count<=5){ 110 /* 111 * 经过多次试验观测得出,前5次测量数据(count 1~5)非常不稳定,建议摒弃 112 */ 113 Serial.print("Adjusting……"); 114 Serial.println(count); 115 Serial.println("-------------------------------"); 116 }else{ 117 if(count<=10){ 118 /* 119 * 前5到前10次数据(count 06~10)作为测试数据,以得出本次测量的平均心率 120 */ 121 test_heartrate[count-6]=BPM; 122 Serial.println("Testing……"); 123 Serial.print("Test BPM:"); 124 Serial.println(BPM); 125 Serial.println("-------------------------------"); 126 }else{ 127 if(count==11){ 128 /* 129 * 从第11次开始测量,数据可以正式使用 130 */ 131 for(int i=0;i<5;i++){ 132 u+=test_heartrate[i]; 133 } 134 u=u/5; 135 Serial.print("Test finished!Your average heartrate is:"); 136 Serial.println(u); 137 Serial.print("Your BPM:"); 138 Serial.println(BPM-u+IdealAverage); 139 Serial.println("-------------------------------"); 140 }else{ 141 Serial.print("Your BPM:"); 142 Serial.println(BPM-u+IdealAverage); 143 Serial.println("-------------------------------"); 144 } 145 } 146 } 147 }else{ 148 return 0; 149 } 150 151 delay(20); 152 153 return BPM-u+IdealAverage; 154} 155 156//ultrasonic 157boolean Ultrasonic(){ 158 159 // ultrasonic activation 160 digitalWrite(TrigPin,LOW); 161 delayMicroseconds(2); 162 digitalWrite(TrigPin,HIGH); 163 delayMicroseconds(2); 164 digitalWrite(TrigPin,LOW); 165 166 // distance 167 distance=pulseIn(EchoPin,HIGH)/58.00; 168 Serial.print(distance); 169 Serial.print("cm"); 170 Serial.println(); 171 172 if(distance<100){ 173 distance_judge=true; 174 }else{ 175 distance_judge=false; 176 } 177 if(distance_judge && count3<5 ){ 178 digitalWrite(buzzer, LOW);//发声 179 delay(200); 180 181 digitalWrite(buzzer, HIGH);//不发声 182 183 digitalWrite(buzzer, LOW);//发声 184 delay(200); 185 186 digitalWrite(buzzer, HIGH);//不发声 187 188 count3+=1; 189 190 servo_9.write(40); 191 servo_10.write(10); 192 show(3); 193 return false; 194 }else{ 195 return true; 196 } 197 198 return true; 199} 200 201//LED 202void show(int mode){ 203 if(mode==0){ 204 setColor(0xff, 0xff, 0xff);//白色 205 setColor(0, 0xff, 0xff); // 青色 206 } 207 if(mode==1){ 208 setColor(0xff, 0xff, 0xff);//白色 209 setColor(0, 0, 0xff); // 蓝色 210 } 211 if(mode==2){ 212 setColor(0xff, 0xff, 0xff);//白色 213 setColor(0, 0xff, 0); // 绿色 214 } 215 if(mode==3){ 216 setColor(0xff, 0xff, 0xff);//白色 217 setColor(0, 0xff, 0x88); 218 } 219} 220 221// Servo 222void sweep2(){ 223 for (pos = 0; pos <= 30; pos += 1) { 224 servo_10.write(pos); 225 servo_9.write(30-pos); 226 delay(15); 227 } 228 for (pos = 30; pos >= 0; pos -= 1) { 229 servo_10.write(pos); 230 servo_9.write(30-pos); 231 delay(15); 232 } 233} 234 235void sweep1(){ 236 for (pos = 0; pos <= 30; pos += 3) { 237 servo_9.write(pos); 238 servo_10.write(30-pos); 239 if(pos==30){ 240 delay(1000); 241 } 242 delay(15); 243 } 244 servo_9.write(0); 245 servo_10.write(30); 246} 247 248void swing(){ 249 for (pos = 30; pos <= 90; pos += 15) { 250 servo_10.write(pos); 251 delay(500); 252 } 253 for (pos = 90; pos >= 30; pos -= 15) { 254 servo_10.write(pos); 255 delay(500); 256 } 257} 258 259 260// 设置LED的颜色 261void setColor(int red, int green, int blue) 262{ 263 #ifdef COMMON_ANODE 264 red = 255 - red; 265 green = 255 - green; 266 blue = 255 - blue; 267 #endif 268 269 // 设置RGB针脚的电压,有效值0-255,对应0-5V电压 270 analogWrite(redPin, red); 271 analogWrite(greenPin, green); 272 analogWrite(bluePin, blue); 273}
open source code
arduino
1#define USE_ARDUINO_INTERRUPTS true 2 3#include <Servo.h> 4#include 5 <PulseSensorPlayground.h> 6 7// Pulse Sensor 8const int PulseWire = A0; 9int 10 Threshold = 550; 11 12// AdjustPulseSensor 13int count=0; 14int test_heartrate[10]; 15int 16 u=0; 17int IdealAverage=70; 18int BPM_current=0; 19int BPM_former=0; 20 21// 22 Servo 23int pos = 0; 24int minus=0; 25int pos_9=minus-pos; 26 27 28// ultrasonic 29const 30 int TrigPin=3; 31const 32 int EchoPin=4; 33float distance; 34boolean distance_judge=false; 35int count2=0; 36int 37 count3=0; 38 39// buzzer 40int buzzer=12; 41 42//LED 43const int redPin = 44 A1; // 红色引脚 45const int greenPin = A3; // 绿色引脚 46const int bluePin = A5; // 蓝色引脚 47 48// 49 Initialize 50Servo servo_9; 51Servo servo_10; 52PulseSensorPlayground pulseSensor; 53 54 55void 56 setup() { 57 // Open Monitor 58 Serial.begin(9600); 59 60 // PulseSensor 61 62 pulseSensor.analogInput(PulseWire); 63 pulseSensor.setThreshold(Threshold); 64 65 66 if (pulseSensor.begin()) { 67 Serial.println("Pulse Sensor Start!");\\ 68 69 } 70 71 // ultrasonic 72 pinMode(TrigPin,OUTPUT); 73 pinMode(EchoPin,INPUT); 74 75 76 // buzzer 77 pinMode(buzzer,OUTPUT); 78 79 // Servo 80 servo_9.attach(9, 81 500,2500); 82 servo_10.attach(10, 500,2500); 83 84 servo_9.write(0); 85 86 servo_10.write(30); 87 88} 89 90void loop() { 91 92 // Heartrate 93 94 BPM_current= AdjustPulseSensor(); 95 96 if(BPM_current==0){ 97 BPM_current=BPM_former; 98 99 } 100 101 if(count>10 && Ultrasonic()){ 102 if(BPM_current<80){ 103 sweep2(); 104 105 show(0); 106 }else{ 107 if(BPM_current<110){ 108 sweep1(); 109 110 show(1); 111 }else{ 112 swing(); 113 show(2); 114 } 115 116 } 117 } 118 119 BPM_former=BPM_current; 120} 121 122 123// Heartrate 124int 125 AdjustPulseSensor(){ 126 127 int BPM=pulseSensor.getBeatsPerMinute(); //PulseSensorPlayground自带函数 128 129 130 if(pulseSensor.sawStartOfBeat() && BPM<130 && BPM>50){ 131 count+=1; //计数器 132 133 if(count<=5){ 134 /* 135 * 经过多次试验观测得出,前5次测量数据(count 1~5)非常不稳定,建议摒弃 136 137 */ 138 Serial.print("Adjusting……"); 139 Serial.println(count); 140 141 Serial.println("-------------------------------"); 142 }else{ 143 if(count<=10){ 144 145 /* 146 * 前5到前10次数据(count 06~10)作为测试数据,以得出本次测量的平均心率 147 */ 148 149 test_heartrate[count-6]=BPM; 150 Serial.println("Testing……"); 151 152 Serial.print("Test BPM:"); 153 Serial.println(BPM); 154 Serial.println("-------------------------------"); 155 156 }else{ 157 if(count==11){ 158 /* 159 * 从第11次开始测量,数据可以正式使用 160 161 */ 162 for(int i=0;i<5;i++){ 163 u+=test_heartrate[i]; 164 165 } 166 u=u/5; 167 Serial.print("Test finished!Your 168 average heartrate is:"); 169 Serial.println(u); 170 Serial.print("Your 171 BPM:"); 172 Serial.println(BPM-u+IdealAverage); 173 Serial.println("-------------------------------"); 174 175 }else{ 176 Serial.print("Your BPM:"); 177 Serial.println(BPM-u+IdealAverage); 178 179 Serial.println("-------------------------------"); 180 } 181 182 } 183 } 184 }else{ 185 return 0; 186 } 187 188 delay(20); 189 190 191 return BPM-u+IdealAverage; 192} 193 194//ultrasonic 195boolean Ultrasonic(){ 196 197 198 // ultrasonic activation 199 digitalWrite(TrigPin,LOW); 200 delayMicroseconds(2); 201 202 digitalWrite(TrigPin,HIGH); 203 delayMicroseconds(2); 204 digitalWrite(TrigPin,LOW); 205 206 207 // distance 208 distance=pulseIn(EchoPin,HIGH)/58.00; 209 Serial.print(distance); 210 211 Serial.print("cm"); 212 Serial.println(); 213 214 if(distance<100){ 215 distance_judge=true; 216 217 }else{ 218 distance_judge=false; 219 } 220 if(distance_judge && count3<5 221 ){ 222 digitalWrite(buzzer, LOW);//发声 223 delay(200); 224 225 digitalWrite(buzzer, 226 HIGH);//不发声 227 228 digitalWrite(buzzer, LOW);//发声 229 delay(200); 230 231 232 digitalWrite(buzzer, HIGH);//不发声 233 234 count3+=1; 235 236 servo_9.write(40); 237 238 servo_10.write(10); 239 show(3); 240 return false; 241 }else{ 242 return 243 true; 244 } 245 246 return true; 247} 248 249//LED 250void show(int mode){ 251 252 if(mode==0){ 253 setColor(0xff, 0xff, 0xff);//白色 254 setColor(0, 0xff, 0xff); 255 // 青色 256 } 257 if(mode==1){ 258 setColor(0xff, 0xff, 0xff);//白色 259 setColor(0, 260 0, 0xff); // 蓝色 261 } 262 if(mode==2){ 263 setColor(0xff, 0xff, 0xff);//白色 264 265 setColor(0, 0xff, 0); // 绿色 266 } 267 if(mode==3){ 268 setColor(0xff, 0xff, 269 0xff);//白色 270 setColor(0, 0xff, 0x88); 271 } 272} 273 274// Servo 275void 276 sweep2(){ 277 for (pos = 0; pos <= 30; pos += 1) { 278 servo_10.write(pos); 279 280 servo_9.write(30-pos); 281 delay(15); 282 } 283 for (pos = 30; pos >= 0; 284 pos -= 1) { 285 servo_10.write(pos); 286 servo_9.write(30-pos); 287 delay(15); 288 289 } 290} 291 292void sweep1(){ 293 for (pos = 0; pos <= 30; pos += 3) { 294 servo_9.write(pos); 295 296 servo_10.write(30-pos); 297 if(pos==30){ 298 delay(1000); 299 } 300 301 delay(15); 302 } 303 servo_9.write(0); 304 servo_10.write(30); 305} 306 307void 308 swing(){ 309 for (pos = 30; pos <= 90; pos += 15) { 310 servo_10.write(pos); 311 312 delay(500); 313 } 314 for (pos = 90; pos >= 30; pos -= 15) { 315 servo_10.write(pos); 316 317 delay(500); 318 } 319} 320 321 322// 设置LED的颜色 323void setColor(int red, int 324 green, int blue) 325{ 326 #ifdef COMMON_ANODE 327 red = 255 - red; 328 green 329 = 255 - green; 330 blue = 255 - blue; 331 #endif 332 333 // 设置RGB针脚的电压,有效值0-255,对应0-5V电压 334 335 analogWrite(redPin, red); 336 analogWrite(greenPin, green); 337 analogWrite(bluePin, 338 blue); 339}
Downloadable files
firtzing
firtzing
Comments
Only logged in users can leave comments
marycheungisme
0 Followers
•0 Projects
Table of contents
Intro
1
0