When i try to use the code for temperature, that is DS18B20, the heartbeat values are not displayed. Can someone help me in displaying both heartbeat and also temperature values?
#include /* I added this code */
#include
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void loop()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void loop()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.println(beatAvg);
Serial.print(ds.getTempC());
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
//
}
When i comment out the temperature code heartbeat code works correctly.
When i comment out the heartbeatcode, the temperature code works perfectly.
If i keep both. Only temperature code works.
You need to study the Analog Devices* 1-WireTMdocumentation.
1-WireTM can support very large numbers of devices - but it requires more careful driver design than the single microcontroller port pin commonly found in Arduino projects.
There's been a number of discussions on this recently; eg,
* Analog Devices acquired Maxim, who acquired Dallas Semiconductor - the originators of 1-WireTM
#include <Wire.h>
#include<OneWire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
OneWire ds(2);
byte dsAddr[8];
byte dsData[9];
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
unsigned long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
ds.reset();
ds.search(dsAddr); //get the address of DS18B20
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void loop()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
unsigned long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
{
beatAvg += rates[x];
}
beatAvg /= RATE_SIZE;
}
}
Serial.print("IR=");
Serial.print(irValue);
if (irValue < 50000)
{
Serial.println(" No finger?");
}
else
{
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.println(beatAvg);
}
ds.reset(); //bring 1-Wire into idle state
ds.select(dsAddr); //slect with DS-1 with address addr1
ds.write(0x44); //conversion command
delay(750); //data ready withinh DS18B20-1 or poll status word
//---------------------------
ds.reset();
ds.select(dsAddr); //selectimg the desired DS18B20-1
ds.write(0xBE); //Function command to read Scratchpad Memory (9Byte)
ds.read_bytes(dsData, 9); //data comes from DS and are saved into buffer data[8]
//---------------------------------
int16_t raw = (dsData[1] << 8) | dsData[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
float celsius = (float)raw / 16.0; //12-bit resolution
Serial.print("Temp from Sensor: ");
Serial.println(celsius, 2);
}
You only have one device, the DS18B20 that is connected by 1-Wire.
The MAX30102 is communicating by I2C.
I'm not sure whether this is the cause of your problem, but the DS18B20 takes more than 600ms to do its temperature conversion. It is likely that during this time the MAX30102 misses heartbeats.
reading the temperatures of DS18B20-sensors can be done in a nonblocking way.
You will need to read how this works.
Me personal I don't use the DS18B20.h-library.
I don't know if this library supports non-blocking reading the temperatures.
With googling I found this library
which has an example-code that demonstrates the non-blocking reading
You have to call function
sensorDs18b20.update();
again and again and again (whích is done through the looping of loop)
executing the function
sensorDs18b20.update() is finished very quick.
Another important thing to know is Callback-function means:
the callback-function is called from inside sensorDs18b20.update()
every time a temperature-conversion inside the DS18B20-sensors has finished.
This means
sensorDs18b20.update();
is called over and over again
but only in case a temperature-conversion has really finished
the function handleIntervalElapsed() gets executed.
1. Now, at least both sensors are working together -- do you agree? You can adjust the sketch of post #5 so that you get expected responses from oxymeter sensor.
2. Can you post the output of the oxymeter sensor when it was running alone (without DS18B20 sensor) ?
3. By the by, what is the purpose of running DS18B20 temperature sensor along with oxymeter sensor?