I am currently trying to connect 3 I2C Devices to an Arduino Mega, as detailed in the fritzing sketch. The Sensors are: Max86150 PPG Sensor on a breakout board (5v), MLX90614 IR Temperature Sensor (3.3V) and a SM9336 Pressure Sensor(3.3V). Essentially I can connect all devices and read off the sensors using individual sketches, but when I combine the sketches the values are not consistent and then the sketch crashes after only a few seconds.
Weirdly enough, when I combine a sketch with either the Temperature sensor and the Pressure sensor or the Temperature Sensor and PPG Sensor, it will work. But Pressure and PPG do not work together.
Does anyone have any idea what could be happening? I did have it working for a small while with some different wires, could this have anything to do with it? But even still some results I was reading from the sensor were the error values (e.g. the value you get when there is nothing connected, but it wasn't crashing). For reference I am using 30 AWG wires and strip board instead of the breadboard in the Fritzing.
//PPG
#include "max86150.h"
MAX86150 max86150Sensor;
uint16_t ppgunsigned16;
uint16_t irdata;
// Pressure Sensor
#include <Wire.h>
#define PressureSensor_Data 0x30 // Pressure Register address
#define TemperatureSensor_Data 0x2E // Pressure Temp Sensor Address
int P0,T0,P1,T1;
int SMD993Address = 0x6C; // Device Address
//Timing
#define INTERVAL 10 //10 milliseconds per sample for 100 Hz, change this for different sampling rates.
uint32_t lastMicros = 0;
// Temp
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void ppgsetup()
{
max86150Sensor.setup();
}
void PPG() {
while (1){
if(max86150Sensor.check()>0)
{
ppgunsigned16 = (uint16_t) (max86150Sensor.getFIFORed()>>2);
Serial.println(ppgunsigned16);
//ppgunsigned16 = (uint16_t) (max86150Sensor.getFIFORed()>>2);
//Serial.println(ppgunsigned16);
if (ppgunsigned16 > 7000){
for (int z = 0; z <= 500; z++){//This takes a reading every 10 milliseconds for 5000 milli seconds which is 5 seconds, 500 readings
digitalWrite(ledPin,HIGH);//turning LED on
//if (micros() - lastMicros > INTERVAL) { //This takes a reading every 100th of a second
//lastMicros = micros(); // do this first or your interval is too long!
if(max86150Sensor.check()>0)
{
ppgunsigned16 = (uint16_t) (max86150Sensor.getFIFORed()>>2);
irdata = (uint16_t) (max86150Sensor.getFIFOIR()>>2);
Serial.print(ppgunsigned16);
Serial.print(",");
Serial.println(irdata);
//Serial.print("Check PPG");Serial.println(y);
//mySerial.write(84);mySerial.print(ppgunsigned16);mySerial.print("/");
//mySerial.write(83);mySerial.print(irdata);mySerial.print("/");
}
}
digitalWrite(ledPin,LOW);//turning LED off
break;
}
}
}
}
void spirometry() {
for (int x = 0; x <= 500; x++){
Wire.beginTransmission(SMD993Address);
Wire.write(PressureSensor_Data);
Wire.write(TemperatureSensor_Data);
Wire.endTransmission();
Wire.requestFrom(SMD993Address,2);//This takes a reading every 100 milliseconds for 10000 milli seconds which is 10 seconds
//if (micros() - lastMicros > INTERVAL) { //This takes a reading every 1000th of a second
//lastMicros = micros(); // do this first or your interval is too long!
if(Wire.available()<= 2) {
P0 = Wire.read();
P1 = Wire.read();
}
int Ptotal = P0 + (P1<<8); //The high byte is transmitted second so that
Serial.println(Ptotal); // Print Ptotal to the serial monitor
//mySerial.write(81);mySerial.print(Ptotal);mySerial.print("/");// Send PTotal over BLE with flag 81, this will change as sending via bytes is more efficient
}
}
void temperature() {
for (int y = 0; y <= 10; y++){
mlx.begin();
Serial.println(mlx.readObjectTempC());
mySerial.write(82);mySerial.print(mlx.readObjectTempC());mySerial.print("/");
delay(10);
}
}
void setup() {
// BT
mySerial.begin(9600);
Serial.begin(9600);//Sets BT Serial Comms to 115200, ensure that BT is allowing for this
//LED
pinMode(ledPin, OUTPUT);
// PPG
// Initialize sensor
if (max86150Sensor.begin(Wire, I2C_SPEED_FAST) == false)
{
Serial.println("MAX86150 was not found. Please check wiring/power. ");
while (1);
}
Serial.println(max86150Sensor.readPartID());
ppgsetup();
//Pressure Sensor
//Temp
//Sleep
sleep();
}
void loop()
{
// put your main code here, to run repeatedly
PPG();
delay(10000);
spirometry();
delay(1000);
temperature();
}
spirometry()
. Please try to introduce a small delay or similar there. And why do you callmlx.begin()
11 times for reading the temperature? Wouldn't one begin in setup be enough?