0

I am currently sending data via my bluetooth and to it I send with me 4 different values looking like this:

byte[] myByte = {"HelloOne", "HelloTwo", "HelloThree", "HelloFour"};
WriteAsync(myByte);

And now I am looking for a way to store all these Hello's in different values.

What I have started with is something like this:

if (Bluetooth.available())
{
 while (true)
 {
   delay (50);
   char c = Bluetooth.read();
   readString += c;

   if (readString.length() >0) {

    if (readString[0] == "HelloOne")
    {
       Serial.println ("Hello One has been sent!");
    }

    if (readString[1] == "HelloTwo")
    {
       Serial.println ("Hello Two has been sent!");
    }

    if (readString[2] == "HelloThree")
    {
       Serial.println ("Hello Three has been sent!");
    }

    if (readString[3] == "HelloFour")
    {
       Serial.println ("Hello Four has been sent!");
    }

   }

 }

}

And I have not actually been able to debugg (not currently at home but eager to get to fix this problem asap) so there is a slim chance that my current code might be working? Very unlikely though.

So my question is, what is the correct way to get each value out in a byte array that is being sent with bluetooth to arduino?

3
  • 1
    No, not a chance of that working, I am afraid. FOr a start you need a 2D array not a 1D array, since you can only store 1 byte in a byte variable, and you want to store 4 groups of multiple bytes. You should read up on using C strings. en.wikibooks.org/wiki/C_Programming/Strings
    – Majenko
    Jan 16, 2017 at 21:50
  • Ok will take a look at that and see if i understand! I am a bit new to c++ as you can probably tell :) Thanks a lot for the link
    – Martman
    Jan 16, 2017 at 22:06
  • Is it possible to do a solution to chop up the byte[]? Because I know how much chars that each byte in the array contains.
    – Martman
    Jan 16, 2017 at 22:51

1 Answer 1

1

Sorry if this sound patronising, its not meant to, I'm just trying to cover all aspects.

byte[] myByte = {"HelloOne", "HelloTwo", "HelloThree", "HelloFour"};
WriteAsync(myByte);

You are assigning 4 strings to the array mByte. A string (lets ignore the Arduino type string for now) is a char* or a char[], so:

char hello1[] = "HelloOne";
char hello2[] = "HelloTwo";
char hello3[] = "HelloThree";
char hello4[] = "HelloFour";

(You don't need to do this in the code, how you have done it is fine, it just shows what you are really saying)

In English mByte should be an array of strings which in code is

char* mByte[] = {"HelloOne", "HelloTwo", "HelloThree", "HelloFour"};

You could do it as

char mByte[][] = {"HelloOne", "HelloTwo", "HelloThree", "HelloFour"};//ERROR

But this won't compile, because the inner array (the text) is different length and the compiler doesn't like that.

Assuming that writeAsync takes a byte as a parameter then the code to write the data becomes this:

for (int stringCounter = 0; stringCounter < 4; ++stringCounter)
{
    // get a pointer to the first element of the string
    // & means get the address (so the value can be set to a pointer)
    // (byte*) means cast (change) the value into a byte pointer.
    byte* currentLetter = (byte*)(&mByte[stringCounter][0];)
    // currentLetter != NULL means the pointer is not equal to NULL, because looking at a null pointer will crash the code.
    // *currentLetter != NULL means the thing pointed to by currentLetter is not the end of the string.
    while (currentLength != NULL || *currentLength != NULL)
    {
        // Write the thing pointed to by the pointer to the function.
        writeAsych(*currentLetter);
        currentLetter++;  // Move on the pointer
    }
}

Sorry I can't compile that at the moment, but it looks about write. I hope it helps you understand it.

2
  • Hey thanks a lot for the answer, Matt! Will take a look in a few hours :) Thanks
    – Martman
    Jan 17, 2017 at 14:09
  • To much stuff to take care off today. Trying it tomorrow
    – Martman
    Jan 18, 2017 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.