Hi there!
I've got task to connect VL530X and MAX30102 sensors to my Arduino Leonardo board. Both devices are communicating via I2C.
The problem is that If i try to use them both at the same time, only one device is working correctly. Wiring should be okay because I2C scanner detects both devices.
I tried to connect both codes (for both sensors) and It seems It's not working correctly, because now on serial monitor I can see only measurments form VL530X sensor and BPM from second sensor is now showing 0.
/* LIBRARIES */
#include <Wire.h>
#include <VL53L0X.h>
#include "MAX30105.h" //MAX3010x library
#include "heartRate.h" //Heart rate calculating algorithm
/* CONSTANTS */
#define XSHUT 12
#define BUZZER 11
#define GREEN_LED 10
#define RED_LED 9
#define DIST_SENS_ADDR (0x29)
#define HEART_RATE_SENS_ADDR (0x57)
/* GLOBAL VARIABLES */
MAX30105 particleSensor; // Heart rate sensor
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;
VL53L0X sensor; // Distance sensor
int licznik = 1;
/* SETUP FUNCTION */
void setup()
{
pinMode(XSHUT, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
digitalWrite(GREEN_LED, LOW);
digitalWrite(XSHUT, HIGH);
Serial.begin(115200);
Serial.print("TEST");
Serial.println();
Wire.begin();
sensor.init();
sensor.setTimeout(500);
// Start continuous back-to-back mode (take readings as
// fast as possible). To use continuous timed mode
// instead, provide a desired inter-measurement period in
// ms (e.g. sensor.startContinuous(100)).
sensor.startContinuous();
while(!Serial); //We must wait for Teensy to come online
delay(100);
Serial.println("");
Serial.println("MAX30102");
Serial.println("");
delay(100);
// Initialize sensor
if (particleSensor.begin(Wire, I2C_SPEED_FAST) == false) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
byte ledBrightness = 70; //Options: 0=Off to 255=50mA
byte sampleAverage = 1; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
int sampleRate = 400; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 69; //Options: 69, 118, 215, 411
int adcRange = 16384; //Options: 2048, 4096, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
}
/* LOOP FUNCTION */
void loop()
{
int distance = sensor.readRangeContinuousMillimeters();
//int distance =sensor.startContinuous(100);
//distance = distance;
if(distance <= 250)
{
digitalWrite(RED_LED, LOW);
Serial.println();
Serial.println();
Serial.print("POWTORZENIE: ");
Serial.print(licznik);
Serial.println();
Serial.println();
digitalWrite(BUZZER, HIGH);
delay(10);
digitalWrite(BUZZER, LOW);
digitalWrite(GREEN_LED, HIGH);
delay(500);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
licznik++;
}
digitalWrite(BUZZER, LOW);
digitalWrite(GREEN_LED, LOW);
Serial.print("Distance: ");
Serial.print(distance);
Serial.print("mm");
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(100);
long irValue = particleSensor.getIR(); //Reading the IR value it will permit us to know if there's a finger on the sensor or not
//Also detecting a heartbeat
if(irValue > 7000){ //If a finger is detected
Serial.println("BPM");
Serial.println(beatAvg);
if (checkForBeat(irValue) == true) //If a heart beat is detected
{
Serial.println("BPM");
Serial.println(beatAvg);
tone(3,1000); //And tone the buzzer for a 100ms you can reduce it it will be better
delay(100);
noTone(3); //Deactivate the buzzer to have the effect of a "bip"
//We sensed a beat!
long delta = millis() - lastBeat; //Measure duration between two beats
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0); //Calculating the BPM
if (beatsPerMinute < 255 && beatsPerMinute > 20) //To calculate the average we strore some values (4) then do some math to calculate the average
{
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;
}
}
}
if (irValue < 7000){ //If no finger is detected it inform the user and put the average BPM to 0 or it will be stored for the next measure
beatAvg=0;
Serial.println("Please Place ");
Serial.println("your finger ");
noTone(3);
}
}
Code for BPM(MAX30102) only works perfect, but after adding VL530x code It stops working correctly.
Can you tell me what I did wrong ? Thanks alot