Advertisement
safwan092

Untitled

Feb 14th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30105.h"
  3. #include "heartRate.h"
  4.  
  5.  
  6. MAX30105 particleSensor;
  7.  
  8. const byte RATE_SIZE = 4;
  9. byte rates[RATE_SIZE];
  10. byte rateSpot = 0;
  11. long lastBeat = 0;
  12. float beatsPerMinute;
  13. int beatAvg;
  14. float temperature;
  15. unsigned long dataMillis = 0;
  16. long irValue;
  17. long delta;
  18.  
  19. void setup() {
  20. Serial.begin(115200);
  21. delay(10000);
  22. Serial.println("11111");
  23. init_heart_rate_sensor();
  24. }
  25.  
  26. void loop() {
  27. read_heart_rate_sensor();
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34. void init_heart_rate_sensor() {
  35. Serial.println("Initializing...");
  36. // Initialize sensor
  37. if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  38. {
  39. Serial.println("MAX30105 was not found. Please check wiring/power. ");
  40. while (1);
  41. }
  42. Serial.println("Place your index finger on the sensor with steady pressure.");
  43.  
  44. particleSensor.setup(); //Configure sensor with default settings
  45. particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
  46. particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  47. particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
  48. }
  49.  
  50.  
  51.  
  52. void read_heart_rate_sensor() {
  53. irValue = particleSensor.getIR();
  54.  
  55.  
  56. if (checkForBeat(irValue) == true)
  57. {
  58. //We sensed a beat!
  59. delta = millis() - lastBeat;
  60. lastBeat = millis();
  61.  
  62. beatsPerMinute = 60 / (delta / 1000.0);
  63.  
  64. if (beatsPerMinute < 255 && beatsPerMinute > 20) {
  65. rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
  66. rateSpot %= RATE_SIZE; //Wrap variable
  67.  
  68. //Take average of readings
  69. beatAvg = 0;
  70. for (byte x = 0 ; x < RATE_SIZE ; x++)
  71. beatAvg += rates[x];
  72. beatAvg /= RATE_SIZE;
  73. }
  74. }
  75.  
  76. Serial.print("IR=");
  77. Serial.print(irValue);
  78. Serial.print(", BPM=");
  79. Serial.print(beatsPerMinute);
  80. Serial.print(", Avg BPM=");
  81. Serial.print(beatAvg);
  82. temperature = particleSensor.readTemperature();
  83. Serial.print(", temperatureC=");
  84. Serial.print(temperature, 4);
  85. if (irValue < 50000)
  86. Serial.print(" No finger?");
  87. Serial.println();
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement