Sending multiple variables with an HC-06

Hi, a few weeks ago i built a 8x8x8 LED Cube for a project, and now they asked me to control it wireless. I am pretty new to arduino, and programming in general, so i started with a much simpler task to try and understand it.
I connect the HC-06 to the arduino, link with my smartphone and send a 1, to light a led. Which works. (There are 3 leds connected to pins 12, 26, 27 and ground)

void setup() {
 pinMode(12, OUTPUT);
void loop() {
if (
{digitalWrite(12, HIGH); 
digitalWrite(12, LOW); } }

Now i try to appr

Now, i want to send two bytes of information, so the code reads its like this. If you get a 2, and another 2, light 12. Else if you get a 1, wait for a 1 or a 2. If you get a 1 light 26. Else if you get a 2 light 27. Else if you get a 2, do that. Else, restart.

void setup() {
 pinMode(12, OUTPUT);
  pinMode(26, OUTPUT);
   pinMode(27, OUTPUT);
void loop() {
  if ({delay(100);if ({ digitalWrite(12, HIGH); delay(1000);}}
else if ({delay(100);
 if ({  digitalWrite(26, HIGH);delay(1000); }
 else if ({ digitalWrite(27, HIGH); delay(1000); } }
else{digitalWrite(12, LOW); digitalWrite(26, LOW);digitalWrite(27, LOW); } }

Now, when i send a 12, the 12 led lights, but when i send a 21, or a 22 nothings happens. Actually, when i send at least eleven 2s (22222222222) sometimes the LED connected to 27 lights up. Sometimes it doesnt.
I tried defining variables, like

byte x1
byte x2

void loop{

but x2 was always empty(or at least, the code didn't respond), and never worked. I dont understand how serial communication works :(.

If you are wondering how does this apply to a LED Cube, What i want to achieve, is being able to send to the HC-06 something like


And make the arduino read it: "use function vector, with variables x=12, y=32, z=4."

But as you can se, i can barely make a led light up with 1s.

I would be very grateful if someone answered me why the 21 doesnt make the Arduino respond, and why sometimes, sometimes not the 22 does!

Hello and welcome,

Here is how I do such commands…

Store all the incoming characters into a char array, until a “end of line” is found (it can be \r, \n, or whatever you want ). When the end of line is found, terminate the char array to make it a valid C string. Then use sscanf or strtok to split it into arguments, that you will then pass to your function.


void setup()
  Serial.begin( 115200 );

void loop()
  if ( Serial.available() > 0 )
    static char input[ 64 ];
    static uint8_t i;

    // read one character
    char c =;

    // if the character is not a '\r' or '\n' and the input buffer is not full
    if ( c != '\r' && c != '\n' && i < 64 - 1 )
      // store the character at the end of the input buffer
      input[ i++ ] = c;

    // else, the input is considered ready to be processed
      // add a null terminator to make it a valid C string
      input[ i ] = '\0';
      //reset index for the next serial message
      i = 0;

      // try parsing the input as you described
      uint8_t x;
      uint8_t y;
      uint8_t z;

      if ( sscanf( input, "vector %hhu %hhu %hhu", &x, &y, &z ) == 3 )
        vector( x, y, z );

The examples in serial input basics are similar to what @guix has suggested.


Thanks Guix and Robin!
I will chew you answers a bit, i am overflowing with concepts i didn't know!