Use retrieved sensor data to assign LEDS

Hi!
So i’m working on product design to use sensory data such as Heartrate and Interbeat Interval(IBI) to use this data as variables to assign to LEDs. I find a lot of info of storing data to SD but not retrieving data from PC and not SD to use it within an arduino project.
The data is not retrieved via Arduino, so I have just these .csv files with timestamps and HR values.

If the files are on the PC and not on an SD card attached to the PC then you cannot read them directly with an Arduino.

What you could do is to have a program on the PC send the data to the Arduino via a serial link or even a Bluetooth or TCP/IP link of you have the appropriate hardware and software

You could, of course, copy the files to an SD card and move that to the Arduino

Great thanks, I’m able to read the .csv files on the SD card with the RobotDyn Data Logger Shield.


As you can see there is the timestamp and HeartRate value. I used the SD example file of ReadWrite.

/*
  SD card read/write

  This example shows how to read and write data to and from an SD card file
  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 9 (for MKRZero SD: SDCARD_SS_PIN)

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(9)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.

  // re-open the file for reading:
  myFile = SD.open("hr_data.csv");
  if (myFile) {
    Serial.println("hr_data.csv:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

I’m really still a beginner in working with arduino, but how could I assign an LED to be blue when the Heartrate value is below 70 and turning red when the value is above 70?

The first stage is to extract the data from the line read from the SD card

If the data will always be in the same format you could test each character as it is read and ignore them until you receive a comma. Then you know that the heartrate is next.

At that point you need to read the value into an array of chars until you get a linefeed. When that is received put a ‘0’ in the next char array position to terminate the string. The need to use an array of chars arises because you are receiving characters representing the heartrate, not the actual number

Now that you have the heartrate in a zero terminated array (aka a C-string) you can convert it to a real number using the dtostrf() function and finally compare it with a fixed number to decide what range it falls in and act as required

What do you mean by “when”? This is not live data, after all. The SD card file might contain heart rates above and below 70, so what happens to the led in that case?

There are standard Arduino functions to make that easier, such as .parseFloat() and .readStringUntil().

What do you mean by “when”? This is not live data, after all. The SD card file might contain heart rates above and below 70, so what happens to the led in that case?

@PaulRB I know, but the data is read by the serial monitor right? Is it possible to read every HR value per line and then change the LED according to that?

SD Card arduino - Servo Playback Shows a very basic example where he reads back the data from the file, but I don’t know if something likewise is possible with what I want.

No, the Arduino is reading it from the SD card, not the serial monitor. You began by asking about reading from a file on pc via the serial monitor and were told that won’t work and you needed a piece of software to read the file and send to the Arduino, or copy the file to SD card and have that read by the Arduino.

Anyway, perhaps what you mean is have the Arduino read the file and play the recording back in “real time” with the led reacting as the data changes? So if the file represents 60 seconds of recorded data, it would take 60 seconds to play back and animate the led based on the data second-by-second.