Advertisement
safwan092

Untitled

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