Parse through continuous data from Serial

Hey all!

I've read through a couple forum posts to solve this problem, but I'm pretty sure I'm doing this wrong, so I would really appreciate some advice.

Basically, I have a heart rate monitor connected to an Android device. This Android device is constantly sending data via bluetooth to the Arduino Serial regarding the heart rate data. The data I get from the Android looks something like this on the Serial:

There is a heart rate!
HR=50#
There is a heart rate!
HR=50#
There is a heart rate!
HR=51#
There is a heart rate!
HR=52#
There is a heart rate!
HR=52#
There is a heart rate!
HR=52#

This data is constantly coming in as fast as it can possibly be received. The # symbol is there so I can easily find the end of the line I need to parse. I guess it's called an escape character? So what I want to do is take that heart rate (eg. 52) and use it to blink a little LED on and off. The blinking part I've got down. I just can't seem to parse the data correctly. I've got some code written up, hopefully my error will be clear. I'm also realizing now that I may not have used the Serial.peek() function correctly... does "peek" return one single character?

const int ledPin =  9;      // red LED pin

int ledStateR = LOW;             // ledState used to set the LED
long previousMillisR = 0;        // will store last time LED was updated

long intervalR;           // interval at which to blink red LED

byte incomingByte;  // will be used to set the interval at which LED will blink

void setup() {
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
  
}

void loop()
{

  if(Serial.available() > 0){
    if(Serial.peek() == 'HR='){
      Serial.read();
      incomingByte = Serial.parseInt(); // read byte
    }
  }
  
  
  
  unsigned long currentMillis = millis();
  
  intervalR = setInterval(incomingByte); // set the interval based on the bpm we got
 
  if(currentMillis - previousMillisR > intervalR) {
    // save the last time you blinked the LED 
    previousMillisR = currentMillis; 

    // if the LED is off turn it on and vice-versa:
    if (ledStateR == LOW){
      ledStateR = HIGH;
    }else
      ledStateR = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledStateR);
    
  }

}

//this function will set the interval at which the light blinks based on the heart rate
float setInterval(byte byteReceived){
  float interval, fByteReceived;
 
  fByteReceived = (float) byteReceived;
  interval = ((60/fByteReceived)*1000)/2;
  
  return interval;

}

A couple small details: I'm not using delays because I need to have more than 1 LED on there doing different things. I also will need to send the breathing rate data over from the heart rate monitor (it also captures breathing) to the Arduino. So hopefully the code I write will work for data coming in that looks like this as well:

There is a heart rate!
HR=50#
There is a breathing rate!
BR=12#
There is a heart rate!
HR=51#
There is a breathing rate!
BR=12#
There is a heart rate!
HR=52#
There is a breathing rate!
BR=12#
There is a heart rate!
HR=52#
There is a breathing rate!
BR=12#

Thanks!! :slight_smile:

if(Serial.peek() == 'HR=')

peek returns a single character.

So I changed part of the code slightly, but it doesn't entirely solve my problem. I changed this:

  if(Serial.available() > 0){
    if(Serial.peek() == 'HR='){
      Serial.read();
      incomingByte = Serial.parseInt(); // read byte
    }
  }

to this:

  if(Serial.available() > 0){
      incomingByte = Serial.parseInt(); // read byte
   }

Right now this works because all I'm sending to the Arduino is the Heart Rate (HR). But since I also need to send the Breathing Rate (BR) in addition to the HR, I need to find a way to parse through the data to distinguish between HR and BR. That's where I'm stuck.

A possible way to get the heart rate being sent.

//zoomkat 12-17-13 simple delimited '#' string  
//from serial port input (via serial monitor)
//and print result out serial port

String readString, newString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like "There is a heart rate! HR=50#"
    
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '#') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      int pos = readString.indexOf('=');
      newString = readString.substring(pos+1, pos+4);
      Serial.print("newString is: ");
      Serial.println(newString);
      int val = newString.toInt();
      Serial.print("heart rate is: ");
      Serial.println(val);
      readString=""; //clears variable for new input    
      newString=""; //clears variable for new input
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}