Help with code

Archer

  • Newbie
  • *
    • Posts: 3
    • View Profile
I was wondering why this piece of code doesn't ever have the red light in the pulse ox sensor turn on. I know it is very finicky for values in heart rate but the sensor isn't even turning on even though the code says it is plugged in and detected.

#include <Wire.h>
#include <Wireling.h>
#include <BMA250.h>
#include <SPI.h>
#include <SD.h>
#include <MAX30101.h>

#if defined (ARDUINO_ARCH_AVR)
#define Serial Serial
#elif defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif

const int chipSelect = 10;


MAX30101 pulseSensor = MAX30101();
int pulseSensorPort = 0;
BMA250 accel_sensor;
int x, y, z, xNew, yNew, zNew, hr, walkingTime, runningTime, playingTime, restingTime;
File myFile;
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  Serial.begin(115200);
  Wire.begin();
  Wireling.begin();
  Wireling.selectPort(1);
  accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms);
  accel_sensor.read();
  x = accel_sensor.X;
  y = accel_sensor.Y;
  z = accel_sensor.Z;
  if (x == -1 && y == -1 && z == -1) {
    while (true) {
    Serial.println("ERROR! NO BMA250 DETECTED!");
    delay(1000);
    }
  } else {
    Serial.println("Complete");
  }
  delay(250);
  Serial.begin(9600);
  Wire.begin();
  Wireling.begin();
  delay(200);
  while (!Serial && millis() < 5000);
  Wireling.selectPort(pulseSensorPort);
  if (pulseSensor.begin()) {
    while (true) {
      Serial.println("MAX30101 Wireling not detected!");
      delay(1000);
    }
  } else {
    Serial.println("Complete");
  }
  pulseSensor.begin();
}

void loop() {
  Wireling.selectPort(pulseSensorPort);
  pulseSensor.begin();
  myFile = SD.open("test.csv", FILE_WRITE);
  x = accel_sensor.X;
  y = accel_sensor.Y;
  z = accel_sensor.Z;
  hr = pulseSensor.BPM();
  delay(150000);
  xNew = accel_sensor.X;
  yNew = accel_sensor.Y;
  zNew = accel_sensor.Z;
  if (hr > 80 && hr < 120 && xNew - x > 2743.2 || yNew - y > 2743.2) {
    walkingTime = walkingTime + 5;
    myFile.println(walkingTime);
    Serial.println(walkingTime);
  } else if (hr > 120 && xNew - x > 2743.2 || yNew - y > 2743.2) {
    runningTime = runningTime + 5;
    myFile.println(runningTime);
    Serial.println(runningTime);
  } else if (xNew - x < 2743.2 && yNew - y < 2743.2 && zNew - z < 2743.2 && hr > 80 && hr < 200) {
    playingTime = playingTime + 5;
    myFile.println(playingTime);
    Serial.println(playingTime);
  } else if (xNew - x < 914.4 && yNew - y < 914.4 && zNew - z < 914.4 && hr < 90) {
    restingTime = restingTime + 5;
    myFile.println(restingTime);
    Serial.println(restingTime);
  } else if (xNew - x > 2743.2 || yNew - y > 2743.2) {
    walkingTime = walkingTime + 5;
    myFile.println(walkingTime);
    Serial.println(walkingTime);
  }
  else
  {
    restingTime = restingTime + 5;
    myFile.println(restingTime);
    Serial.println(restingTime);
  }
  myFile.close();
  delay(150000);
}


lennevia

  • Administrator
  • Hero Member
  • *****
    • Posts: 344
    • View Profile
Hi there,

I ran your program through diffchecker (https://www.diffchecker.com/diff) with the pulse sensor wireling example from the setup tutorial: https://learn.tinycircuits.com/Wirelings/Pulse-Oximetry_Wireling_Tutorial/
I attached a picture of what this difference looks like.

It looks like you never update the sensor, or store any data from it for that matter. In the loop() you use the pulseSensor.begin() function. So instead of polling for new data each loop, you are re-initializing the sensor. The delay is also far too large - some discussion on why that is.

I advise going through the tutorial linked above.

Cheers,
RĂ©na


 

SMF spam blocked by CleanTalk