Code Example

/*
   Code by Brian Patton
   3/24/2017
   Feel free to do whatever you want with this code example

 Buetooth      Teensy
  Vcc----------5Volts
  GND----------GND
  RXD----------Pin1
  TXD----------Pin0

*/
//********************************************************
//    Setup
//********************************************************
#define blueSerial Serial1

char getChar;            // Variable to hold singel character off of serial buffer
String tempStr;       // build strings from the above characters
String xString;       // String to work with after collecting from hardware serial
String yString;       // String to work with after collecting from hardware serial
char xChar,yChar;
byte xVal,yVal;

void setup() {
  Serial.begin(9600);    // Set Baud rate
  while (!Serial);       // Wait for window to open
  blueSerial.begin(9600);  // Set Baud rate
  delay(500);            // Wait an extra 1/2 second
}
//********************************************************
//   Main Loop
//********************************************************
void loop() {
  getblueSerial();

  if (xString.charAt(0) == '*') {
    xString.remove(0, 1);        // trim off the '*'
    xVal = byte(xString.charAt(0));
    Serial.println("X Value = " + (String)xVal);
  }
  if (yString.charAt(0) == ',') {
    yString.remove(0, 1);        // trim off the ','
    yVal = byte(yString.charAt(0));
    Serial.println("Y Value = " + (String)yVal);
  }
  //  printIt();
}

//********************************************************
//    Get hardware Serial data
//********************************************************
void getblueSerial() {

  while (blueSerial.available() == 0);  // wait until there is data in the buffer

  if (blueSerial.available() > 0) { //if there is data in the serial buffer.....
    getChar = blueSerial.read();    // collect a byte of data

    if (getChar == ',') {
      xString = tempStr;  // Send Data before appending the ","
      tempStr += getChar;  // append ,
      tempStr = "";        // Clear tempStr Buffer
//      Serial.println("xString = " + (String)xString);
    }
    if (getChar == '#') {
      yString = tempStr;  // Send Data before appending the "#"
      tempStr += getChar;  // append #
      tempStr = "";        // Clear tempStr Buffer
//      Serial.println("yString = " + (String)yString);
    }
    else {
      tempStr += getChar; // append characters until a 'Z' is found.
    }
  }
}