About Bluetooth data received

Hi,
I am testing the MIT APP Joystick to Arduino by HC 06, few questions need help please.

  1. can I use more variables to read Bluetooth like this:
while (Serial1.available() >= 2) {
    x = Serial1.read();  //WAS: xAxis = Serial1.read();
    delay(10);
    y = Serial1.read();  // WAS: yAxis = Serial1.read();

the serial monitor got just x value.
2. If only one variable can be use, how to separate the 'x' and 'y' data?
3. how to separate the number data and char?
Thanks
Adam

#include <SPI.h>

const int Start_Relay = 37;
const int Stop_Relay = 39;
uint8_t xAxis = 140, yAxis = 140;
uint8_t  x = 0;
uint8_t  y = 0;
uint8_t command = 0;

char Incoming_value = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600); // Default communication rate of the Bluetooth module
  SPI.begin();
  Serial.println("Setup complete");
  
  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);

  pinMode(Start_Relay, OUTPUT); //START Relay
  pinMode(Stop_Relay, OUTPUT); //STOP Relay

  digitalWrite(Start_Relay, LOW);
  digitalWrite(Stop_Relay, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  Bluetooth();
}

void Bluetooth()
{
  // Read the incoming data from the Smartphone Android App
  while (Serial1.available() >= 2) {
    x = Serial1.read();  //WAS: xAxis = Serial1.read();
    delay(10);
    y = Serial1.read();  // WAS: yAxis = Serial1.read();

    xAxis = x;
    yAxis = y;

    Serial.print("xAxis=");
    Serial.println(xAxis);

    Serial1.print("yAxis=");
    Serial1.println(yAxis);

    Incoming_value = Serial1.read();

    switch (Incoming_value) {

        Serial1.print("Incoming_value!!!!!!!!!!!!!!!!!!=");
        Serial1.println(Incoming_value);

      case 'S':
        Serial.println("START!");
        Serial1.println("START!");

        digitalWrite(Start_Relay, !digitalRead(Start_Relay)); //
        delay(1000);  ////MUST HAVE THIS, MILLS() NOT WORK HERE

        digitalWrite(Start_Relay, !digitalRead(Start_Relay));
        delay(5);

        break;

      case 'P':
        Serial.println("STOP!");
        Serial1.println("STOP!");

        digitalWrite(Stop_Relay, !digitalRead(Stop_Relay)); //
        delay(1000);  ////MUST HAVE THIS, MILLS() NOT WORK HERE

        digitalWrite(Stop_Relay, !digitalRead(Stop_Relay));
        delay(5);

        break;
    }
  }
}

Robin2's serial input basics tutorial should be of help.

1 Like

Example code using the example #2 receive with end markers combined with parsing with strtok() to receive the x and y axis values from Bluetooth on Serial1.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

uint8_t xAxis = 140;
uint8_t yAxis = 140;

void setup()
{
   Serial.begin(115200);
   Serial1.begin(9600);
   Serial.println("<Arduino is ready>  Send two numbers separated by commas");
   Serial.println("from a Bluetooth app.  Like 123,56");
   Serial.println("make sure newline is enabled in serial monitor line endings");
}

void loop()
{
   recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial1.available() > 0 && newData == false)
   {
      rc = Serial1.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[6]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiters, semicolon
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   //Serial.println(index);
   /*
   // print all the parts
   Serial.println("The Pieces separated by strtok()");
   for (int n = 0; n < index; n++)
   {
      Serial.print("piece ");
      Serial.print(n);
      Serial.print(" = ");
      Serial.println(strings[n]);
   }
   */
   // convert string data to numbers
   xAxis = atoi(strings[0]);
   yAxis = atoi(strings[1]);
     
   Serial.print("X Axis = ");
   Serial.print(xAxis);
   Serial.print("   Y Axis = ");
   Serial.print(yAxis);
   Serial.println();
   newData = false;
}
1 Like

Thank you.
I'll test it.

Make reception more robust by using start and end markers like in example #5.

1 Like

Thanks.
I am using MIT APP by phone, I did't figure out a proper way to send data yet.


Sorry, I know nothing of the MIT app and have no wish to learn. I use the serial Bluetooth terminal app on my Android tablet for testing.

If I were using an Arduino as the sender I would use:

Serial.print(xAxis);  // send x axis value
Serial.print(',');  // insert a comma delimiter
Serial.println(yAxis); // send y axis value and '\n' (line feed) terminator
1 Like

Thank you so much.
I got lot of helps.

Hi groundFungus, if I send the data like:

Serial.print(xAxis);  // send x axis value
Serial.print(',');  // insert a comma delimiter
Serial.print('A'); 
Serial.print(','); 
Serial.println(yAxis); // send y axis value and '\n' (line feed) terminator

How to pick the 'A' out in the receiver side and keep the xAxis/yAxis data Not destroy。
Thanks

A couple of small additions to the parseData function to parse and print the A and the addition of a global variable to hold the A is all it takes. See the **** comments.

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

uint8_t xAxis = 140;
uint8_t yAxis = 140;
char character;  // variable for A   **** added this  

void setup()
{
   Serial.begin(115200);
   Serial1.begin(9600);
   Serial.println("<Arduino is ready>  Send two numbers and a character separated by commas");
   Serial.println("from a Bluetooth app.  Like 123,A,56");
   Serial.println("make sure newline is enabled in serial monitor line endings");
}

void loop()
{
   recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial1.available() > 0 && newData == false)
   {
      rc = Serial1.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[6]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiters, semicolon
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
   //Serial.println(index);
   /*
      // print all the parts
      Serial.println("The Pieces separated by strtok()");
      for (int n = 0; n < index; n++)
      {
      Serial.print("piece ");
      Serial.print(n);
      Serial.print(" = ");
      Serial.println(strings[n]);
      }
   */
   // convert string data to numbers
   xAxis = atoi(strings[0]);
   character = char(*strings[1]); // get the character  **** added this  
   yAxis = atoi(strings[2]);

   Serial.print("X Axis = ");
   Serial.print(xAxis);
   Serial.print("   Y Axis = ");
   Serial.print(yAxis);
   Serial.print("   character = ");   // **** added this  
   Serial.print(character);  //  **** added this  
   Serial.println();
   newData = false;
}
1 Like

Great!
many thanks to you.

1 Like