1

I have two sensors data in packet but now I want to send these two sensors data into single packet in which these two sensors data are gathered. I mean I want to have a packet in which I have oxygen, heart rate and pedometer values. Can anyone suggest me how to do it. Code for both sensors are given below. I have already try a lot of ways but all in vain. I will be very thankful for your suggestion and time.

For Sensor 1

#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"


MAX30105 particleSensor;


#define MAX_BRIGHTNESS 255

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
//Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
//To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100];  //red LED sensor data
#else
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100];  //red LED sensor data
#endif

int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
uint16_t checkSum = 0;

byte pulseLED = 11; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read

byte ledBrightness = 60; //Options: 0=Off to 255=50mA
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384

void setup()
{
  
}

void loop()
{
  Serial.begin(9600);
  wait_for_Max30102();
  start_signal();
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println(F("MAX30105 was not found. Please check wiring/power."));
    while (1);
  }

  Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  //while (Serial.available() == 0) ; //wait until user presses a key
  Serial.read();



  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps

  //read the first 100 samples, and determine the signal range
  for (byte i = 0 ; i < bufferLength ; i++)
  {
    while (particleSensor.available() == false) //do we have new data?
      particleSensor.check(); //Check the sensor for new data

    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample(); //We're finished with this sample so move to next sample

    /*Serial.print(F("red="));
    Serial.print("\t");
    Serial.print(redBuffer[i], BIN);
    Serial.print("\t");
    Serial.print(redBuffer[i], DEC);
    Serial.print("\n");
    Serial.print(F(", ir="));
    Serial.print("\t");
    Serial.println(irBuffer[i], BIN);
    Serial.print("\t");
    Serial.print(irBuffer[i], DEC);
    Serial.print("\n");*/
  }

  //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  while (1)
  {
    //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
    for (byte i = 25; i < 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }

    //take 25 sets of samples before calculating the heart rate.
    for (byte i = 75; i < 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data

      digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read

      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample(); //We're finished with this sample so move to next sample


      checkSum = redBuffer[i] + irBuffer[i] + heartRate + validHeartRate + spo2 + validSPO2;

      //send samples and calculation result to terminal program through UART
      Serial.print(F("RED: "));
      Serial.print("\t");
      Serial.print(redBuffer[i], BIN);
      Serial.print("\t");
      Serial.print(redBuffer[i], DEC);
      Serial.println("");
      
      Serial.print(F("IR: "));
      Serial.print("\t");
      Serial.print(irBuffer[i], BIN);
      Serial.print("\t");
      Serial.print(irBuffer[i], DEC);
      Serial.println("");

      Serial.print(F("HR: "));
      Serial.print("\t");
      Serial.print(heartRate, BIN);
      Serial.print("\t");
      Serial.print("\t");
      Serial.print(heartRate, DEC);
      //Serial.println("\t");
      //Serial.print("(HRvalid = ");
      //Serial.print("\t");
      //Serial.print(validHeartRate, BIN);
      //Serial.print("\t");
      //Serial.print(validHeartRate, DEC);
      //Serial.print((" )"));
      Serial.println("");
      
      Serial.print(F("SPO2: "));
      Serial.print("\t");
      Serial.print(spo2, BIN);
      Serial.print("\t");
      Serial.print("\t");
      Serial.print(spo2, DEC);
      //Serial.println("\t");
      //Serial.print("(SPO2Valid = "); 
      //Serial.println(validSPO2, BIN);
      //Serial.print("\t");
      //Serial.println(validSPO2, DEC);
      //Serial.print(" )");
      Serial.print("\n");
      Serial.println("");

      Serial.print(F("Checksum Byte: "));
      Serial.print("\t");
      Serial.print(checkSum, BIN); 
      Serial.print("\t");
      if((byte)checkSum == (byte)(redBuffer[i] + irBuffer[i] + heartRate + validHeartRate + spo2 + validSPO2)){Serial.print("(CHECKSUM_OK)");}
      else {Serial.print("(CHECKSUM_ERROR)");}
      Serial.println("\n");
      Serial.println("\n");
      Serial.println("");
      Serial.println("");
      Serial.println("");
      delay(1000);
    }

    //After gathering 25 new samples recalculate HR and SP02
    maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  }
  Serial.end(); 
}

void wait_for_Max30102()
{
delay(2000);
}

void start_signal(){
pinMode(readLED, OUTPUT);
pinMode(pulseLED, OUTPUT);
digitalWrite(pulseLED, LOW); 
digitalWrite(readLED, LOW); 
delay(18);
digitalWrite(pulseLED, HIGH);
digitalWrite(readLED, HIGH);
pinMode(pulseLED, INPUT);
pinMode(readLED, INPUT);
digitalWrite(pulseLED, HIGH);
digitalWrite(readLED, HIGH);
}

For Sensor 2:

#include <Wire.h>

#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

#define offsetX   -10.5       // OFFSET values
#define offsetY   -2.5
#define offsetZ   -4.5

#define gainX     257.5        // GAIN factors
#define gainY     254.5
#define gainZ     248.5

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[512];                      //string buffer to transform data before sending it to the serial port

int x,y,z, X;

int xavg, yavg,zavg, steps=0, flag=0;
int xval[15]={0}, yval[15]={0}, zval[15]={0};
int threshhold = 60.0;

uint16_t checkSum = 0;

void setup(){}

void loop()
{
for(unsigned int x = 0; x < 5; x++)
{ clv(); }
Serial.end();
}


void start_signal(){
  writeTo(DEVICE, 0x2D, 0);      
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8); 
}

void clv(){
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  start_signal();
  //Turning on the ADXL345
  
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  
  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
  
   //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
   //thus we are converting both bytes in to one int
  x = (((int)buff[1]) << 8) | buff[0];   
  y = (((int)buff[3])<< 8) | buff[2];
  z = (((int)buff[5]) << 8) | buff[4];
  
//we send the x y z values as a string to the serial port 
 //sprintf(str, "%d %d %d", x, y, z);
 //Serial.print(str);
 //Serial.print(10, byte());
 
  Serial.print("X = ");
  Serial.println(x);
  Serial.print("Y = ");
  Serial.println(y);
  Serial.print("Z = ");
  Serial.println(z);
  
 //write steps
  X = ArduinoPedometerSteps();
  Serial.print("steps = ");
  Serial.println(X);
  Serial.println(" ");
  
  //It appears that delay is needed in order not to clog the port
  Serial.print(F("X: "));
  Serial.print("\t");
  Serial.print(x, BIN);
  Serial.print("\t");
  Serial.print(x, DEC);
  Serial.print("\n");

  Serial.print(F("Y: "));
  Serial.print("\t");
  Serial.print(y, BIN);
  Serial.print("\t");
  Serial.print(y, DEC);
  Serial.print("\n");

  Serial.print(F("Z: "));
  Serial.print("\t");
  Serial.print(z, BIN);
  Serial.print("\t");
  Serial.print(z, DEC);
  Serial.print("\n");
  Serial.print(F("steps: "));
 Serial.print("\t");
 Serial.print(X, BIN);
 Serial.print("\t");
 Serial.print(X, DEC);
 Serial.println("\n");
 
checkSum = x + y + z + X;

 Serial.print(F("Checksum Byte: "));
 Serial.print("\t");
 Serial.print(checkSum, BIN); 
 Serial.print("\t");
 if((byte)checkSum == (byte)(x + y + z + X)){Serial.print("(CHECKSUM_OK)");}
 else {Serial.print("(CHECKSUM_ERROR)");}
 Serial.println("\n");
 Serial.println("\n");
 Serial.println("");
 Serial.println("");
 Serial.println("");
 delay(150);
}

//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device 
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission(device); //start transmission to device 
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(device); //start transmission to device
  Wire.requestFrom(device, num);    // request 6 bytes from device
  
  int i = 0;
  while(Wire.available())    //device may send less than requested (abnormal)
  { 
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}


//Get pedometer.

int ArduinoPedometerSteps(){
    int acc=0;
    int totvect[15]={0};
    int totave[15]={0};
    int xaccl[15]={0};
    int yaccl[15]={0};
    int zaccl[15]={0};
    for (int i=0;i<15;i++)
    {
      xaccl[i]= x;
      delay(1);
      yaccl[i]= y;
      delay(1);
      zaccl[i]= z;
      delay(1);
      totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zval[i] - zavg)*(zval[i] - zavg)));
      totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
      //Serial.print("Total Average = ");
      //Serial.println(totave[i] );
      
      //delay(50);
  
      //cal steps 
      if (totave[i]>threshhold && flag==0)
      {
         steps=steps+1;
         flag=1;
      }
      else if (totave[i] > threshhold && flag==1)
      {
          //do nothing 
      }
      if (totave[i] <threshhold  && flag==1)
      {
        flag=0;
      }
     // Serial.print("steps=");
     // Serial.println(steps);
     return(steps);
    }
  delay(100); 
 }
6
  • Are you trying to combine two programs together, or do you have a single program that already contains all the sensor code and you're trying to work out how to "send" that data? Send it to what, over what medium?
    – Majenko
    May 24 at 9:31
  • What do you mean by “packet”? You seem to be sending over Serial, and there is no such thing as a “packet” over Serial: you can only send a stream of bytes. May 24 at 9:32
  • @EdgarBonet In a previous question the OP asked how to send packets of binary data over serial. I gave an answer explaining a general concept with start byte, message length and data bytes as protocol imposed on the serial stream. Though I cannot see that implemented in the codes above.
    – chrisl
    May 24 at 10:20
  • @chrisl after your instructions i followed this site: engineersgarage.com/microcontroller-projects/… . i found that they are sending start bit then adding data and at the end checksum bytes are added. But i think so i cannot implement it in really good way. I am really new on arduino. can you please guide me by seeing the code given above?
    – Namra Noor
    May 24 at 11:06
  • @EdgarBonet can you please guide me how to convert this stream of bytes into packet?
    – Namra Noor
    May 24 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.