[SOLVED] Problems reading serial data from Bluetooth

Hi every one. Im making a project with Leonardo and a bluetooth module HC-05 to control an RGB led stripe with Android, I have almost everything up and working, but I’m having some problems while reading the serial data.

I have an application thats sends via Bluetooth this string “number1, number2, number3, number4, number5\n”, I use those numbers as “Option, R, G, B, bright

The code is use to read the serial data is:

SoftwareSerial BT(8,7); //RX,TX.
BT.begin(38400);

if(BT.available()>0)
  {
    opcion=BT.parseInt();
    red=BT.parseInt();
    green=BT.parseInt();
    blue=BT.parseInt();
    bright=BT.parseInt();
    end=BT.read(); // Im not sure about this, but without this, the things don't work
}

On the Serial monitor (seted at 38400) sometimes I get out of range values:

Option is always 1 at the moment
R,G,B goes from 0 - 255
Bright 0 - 100

I think I’m messing with reading the Serial Data, because the app looks ok and its very simple, done with App inventor 2.
What I’m doing wrong?’

Thank you very much!

Screenshot_2014-12-23-20-10-10.png

Send a proper packet - something like <optionValue, redValue, greenValue, blueValue, brightValue>. Read and store data, starting when the ‘<’ arrives, ending when the ‘>’ arrives. When the ‘>’ arrives, parse the data in the char array, using strtok() and atoi(). Forget parseInt().

Thank you for your answer Paul. I’m sending the data between < > and its working better.
I’ve checked atoi and strtok because I didn’t knew them, will try to implement them.

Why you suggest to not use parseInt() ?

Thanks again.

Look at the Arduino code in this demo. It is a more comprehensive approach to data reception and takes in all the data before trying to parse any of it. The Arduino code will work with the Serial Monitor if you are not interested in the Python stuff.

...R

Why you suggest to not use parseInt() ?

Look at the source code. See for yourself.

The parseInt() function blocks everything until it is satisfied or the timeout expires. The code in the link I posted does not block anything.

...R

below is some code I use with servos that may be similar to what you are wanting to do.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Robin2:
The parseInt() function blocks everything until it is satisfied or the timeout expires. The code in the link I posted does not block anything.

...R

Hi Robin2, I think I love you, I've adapted your code and works beautiful. It was very clear to understand, thank you!

zoomkat:
below is some code I use with servos that may be similar to what you are wanting to do.

//zoomkat 11-22-12 simple delimited ',' string parse 

//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

//myservoa.writeMicroseconds(1500); //set initial servo position if desired

myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println(“multi-servo-delimit-test-dual-input-11-22-12”); // so I can keep track of what is loaded
}

void loop() {

//expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ‘,’) {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

int n = readString.toInt();  //convert readString into a number

// auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf(‘a’) >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf(‘b’) >0) myservob.writeMicroseconds(n);
          if(readString.indexOf(‘c’) >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf(‘d’) >0) myservod.writeMicroseconds(n);
        }
        else
        { 
          Serial.print(“writing Angle: “);
          Serial.println(n);
          if(readString.indexOf(‘a’) >0) myservoa.write(n);
          if(readString.indexOf(‘b’) >0) myservob.write(n);
          if(readString.indexOf(‘c’) >0) myservoc.write(n);
          if(readString.indexOf(‘d’) >0) myservod.write(n);
        }
        readString=””; //clears variable for new input
      }
    } 
    else {   
      readString += c; //makes the string readString
    }
  }
}

Thanks for your answer zoomkat, I’ve checked the code from Robin2 and works excellent