Geting MAX30100 data with Arduino UNO

I try to read the SPO concentration and the pulse with MAX30100 and arduino uno. But for my project i am not allowed to use the library. I set up max, but i can not read the data. I found this code on internet but is using the I2c Library with this comandsI2C . In arduino uno I use the wire.h library.
The code i was trying to use was from here: Heart Rate Click: On EasyPIC v7. The Heart Rate click from… | by Teodor Costăchioiu | Electronza | Medium.
I managed to do what is before the line 108, but what is inside the while(1) loop has troubled me.
Does someone know how to do what is inside while(1) loop with the wire.h library? Or how to get the data? I post my code below.

#include <Wire.h>
#include <avr/io.h>

// Device address
const int adresaMAX30100 = 0x57;
byte trans_status = 255;
int write_status = 0;
unsigned int i;
int samples;
unsigned short wrp;
double var;
unsigned int HR;
unsigned int SPO2;
unsigned int FIFO_readRegister;
unsigned int FIFO_writeRegister;
unsigned int availableSample;
unsigned int samplesToRead;

/************************* Initialization Setup Function **************************/
void setup(){

// Initiate wire library and serial communication
Wire.begin();
Serial.begin(9600);

WriteI2C(adresaMAX30100, 0x09, 0x34);
MAX30100_reset();
delay(100);
WriteI2C(adresaMAX30100, 0x09, 0x32);
MAX30100_setSR ();
MAX30100_setPW ();
MAX30100_InitFIFO ();
wrp = 0;
MAX30100_wakeup() ;
MAX30100_SetSPO2() ;
var = CitireI2c(adresaMAX30100, 0x07);
Serial.println(var);

delay(10);
}

/************************* Infinite Loop Function **********************************/
void loop(){
FIFO_readRegister = CitireI2c(adresaMAX30100, 0x04 );
FIFO_writeRegister= CitireI2c(adresaMAX30100, 0x02 );
samplesToRead = FIFO_writeRegister - FIFO_readRegister;
Serial.println(samplesToRead);

 // We first check for overflows
wrp = CitireI2c ( adresaMAX30100, 0x03 );
  //if (wrp == 0x0F){ // signal overflow
 //   Serial.println("Oare esti aici?");
   // while(1); // blocking

// }
// Gets the number of samples in the FIFO
samples = MAX30100_getNumSamp();
// Then reads the samples
if (samples > 0 ){ // we have data in FIFO
for (i = 0; i < samples; i++){
// read one sample from the sensor
MAX30100_readFIFO(&HR, &SPO2);

         // Process the samples
         // One has about 14ms to do the processing
         // before the next data sample is ready

         // Here we show the gathered data on LCD
           Serial.print("HR = ");
           Serial.println(HR);
           Serial.print("SPO2 = ");
           Serial.println(SPO2);

     } // end of sample reading loop
  }    // end if statement that checks if we have data
  else { // There's no data in FIFO
         delay(30); // Wait for samples to be aquired
  }

}

/*********************** Read Temperature Sensor Function **************************/
int CitireI2c(uint8_t adresaSenzor, uint8_t adresaRegistru ){
uint8_t valoareRegistru;
// Points to device & begins transmission
Wire.beginTransmission(adresaSenzor);
// Points to temperature register to read/write data
write_status = Wire.write(adresaRegistru);
// Serial.println(write_status);
// Ends data transfer and transmits data from register
trans_status = Wire.endTransmission(false);
// Serial.println(trans_status);

// Delay to allow sufficient conversion time
// delay(10);

// Requests 2-byte temperature data from device
int n = Wire.requestFrom(adresaSenzor,1);

// Checks if data received matches the requested 2-bytes
if ( n != 1)
{
Serial.println("Error");
}
else {
// Stores each byte of data from temperature register
valoareRegistru = Wire.read();
return valoareRegistru;

}
}

/**************************** I2C Write Function ****/
double WriteI2C(int dev, int reg, int H){
// Takes in 4 variables:
// device address, register addres
// high and low bytes of data to transmit
Wire.beginTransmission(dev);
Wire.write(reg);
Wire.write(H);
Wire.endTransmission();
delay(10);
}
/
Functii MAX ********************************/
// Resets the MAX30100 IC
void MAX30100_reset(void){
unsigned short reg;
reg = CitireI2c (adresaMAX30100, 0x06);
// RESET bit is B6
// 0x40 = 01000 0000
reg = reg | 0x40; // Set reset bit to 1
WriteI2C (adresaMAX30100, 0x06, reg);
}

// Wakes up the MAX30100
// Clears bit 7 of MODE CONFIG register
void MAX30100_wakeup(void){
unsigned short reg;
reg = CitireI2c (adresaMAX30100, 0x06);
// RESET bit is B7
// 0x7F = 0111 1111
reg = reg & 0x7F; // Set SHDN bit to 0
WriteI2C (adresaMAX30100, 0x06, reg);
}

// Sets sample rate
// sample rate is bits 4:2 of register MAX30100_SPO2_CONFIG
// bitmask is 0xE3
void MAX30100_setSR (void){
unsigned short reg;
reg = CitireI2c (adresaMAX30100, 0x07);
reg = reg & 0xE3;
reg = reg | (0b000 << 2);
WriteI2C (adresaMAX30100, 0x07, reg);
}

// Sets pulse width
// sample rate is bits 1:0 of register MAX30100_SPO2_CONFIG
void MAX30100_setPW (void){
unsigned short reg;
reg = CitireI2c (adresaMAX30100, 0x07);
reg = reg & 0xFC;
reg = reg | 0b00000011;
WriteI2C (adresaMAX30100, 0x07, reg);
}

void MAX30100_InitFIFO (void){
WriteI2C (adresaMAX30100,0x03, 0x00);
WriteI2C (adresaMAX30100,0x04, 0x00);
WriteI2C (adresaMAX30100,0x05, 0x00);
}

// Wakes up the MAX30100
// Clears bit 7 of MODE CONFIG register
void MAX30100_wakeu(void){
unsigned short reg;
reg = CitireI2c (adresaMAX30100, 0x06);
// RESET bit is B7
// 0x7F = 0111 1111
reg = reg & 0x7F; // Set SHDN bit to 0
WriteI2C (adresaMAX30100,0x06, reg);
}

// Sets SPO2 rate mode
// This means MODE{2:0} = 0b011 (or 0x03 in hexadecimal)
void MAX30100_SetSPO2 (void){
unsigned short reg;
reg = CitireI2c(adresaMAX30100, 0x06);
// RESET bit is B7
// First we clear bits 2:0
reg = reg & 0xF8;
// Then we set bits 2:0 to 0x03
reg = reg | 0x03;
WriteI2C (adresaMAX30100, 0x06, reg);
}

// Gets number of samples in FIFO
int MAX30100_getNumSamp(void){
unsigned short wreg;
unsigned short rreg;
wreg = CitireI2c (adresaMAX30100, 0x02);
rreg = CitireI2c (adresaMAX30100, 0x04);
return (abs( 16 + (int)wreg - (int)rreg ) % 16);
}

// In each sample we have to read four bytes
void MAX30100_readFIFO(unsigned int *data1, unsigned int *data2){
unsigned int reg;
unsigned int readout;
unsigned int nrVal;
// Points to device & begins transmission
Wire.beginTransmission(adresaMAX30100);
// Points to temperature register to read/write data
Wire.write(0x05);
// Ends data transfer and transmits data from register
Wire.endTransmission();
// issue I2C signal repeated start

// Delay to allow sufficient conversion time
delay(10);

// Requests 2-byte temperature data from device
Wire.requestFrom(adresaMAX30100,2);
nrVal = Wire.available();

// send byte (device address + R)
readout= Wire.read(); // I2C read with ACK
reg = Wire.read();
*data2 = (readout << 8) | reg;
readout = Wire.read();
reg = Wire.read(); // I2C read with NACK (tells slave to let go)
*data1 = (readout << 8) | reg;
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.