Sending right datatype to board

Hello,

i got a BLE-Shield for my Arduino to communicate via Bluetooth with my iPhone. I want to send a specific unsigned long value to my Arduino which then should be sent via an IR-LED to a receiver. I have a working example but my code is very ugly and i am sure there has to be an easier way. Here is my code which works:
iOS Code

-(IBAction)sendCode:(id)sender {
  
    UInt8 code = 0x00;
    switch([sender tag]) { //the buttons have tag from 0 to 18 so i can identify each button and send another code
        case 0: //button 0 was pressed
            code = 0x50;
            break;
        case 1: //button 1 was pressed
            code = 0x51;
            break;
        case 2:
            code = 0x52;
            break;
        case 3:
            code = 0x53;
            break;
   ....
goes up to 18
    }
    UInt8 buf[1] = {code};
    NSData *data = [[NSData alloc] initWithBytes:buf length:sizeof(buf)];

    [self.ble write:data];
        
}

So i am sending some unsigned integer values to my Arduino (self.ble is the interface to my Bluetooth shield).

On the Arduino side i am retrieving like this:

void loop()
{      
  
  // If data is ready
  while(ble_available())
  {
   // read out command and data
    //byte data0 = ble_read();
   byte data0 = ble_read();
 if(data0 == 0x50) {
    irsend.sendRC5(0xA01,12);
    }
     else if(data0 == 0x51) {
    irsend.sendRC5(0xA02,12);
    }
     else if(data0 == 0x52) {
    irsend.sendRC5(0xA03,12);
    }
....

and a lot more else if statements...
}
}

You can see that i actually don't want to send for example 0x50 but 0xA01. This way i could remove all the if clauses and just send the right codes to my arduino and then send it directly. Like this:

  NSMutableArray *codeArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithUnsignedLong:0xA01],[NSNumber numberWithUnsignedLong:0xA02],
                 [NSNumber numberWithUnsignedLong:0xA03],
                 [NSNumber numberWithUnsignedLong:0xA04],... and a lot more,,nil];

    unsigned long msg = [[codeArray objectAtIndex:[sender tag]]unsignedLongValue];
    NSData *data = [[NSData alloc] initWithBytes:(const void*)(&msg) length:sizeof(msg)];

    [self.ble write:data];

Then on arduino side my code would look like:

void loop()
{      
  
  // If data is ready
  while(ble_available())
  {
   unsigned long data0 = ble_read();
    irsend.sendRC5(data0,12);
}
}

But this isn't working. I think because data0 hasn't the exact value i was sending and i think this occurs because of the different data types here.

So do you know any possibility for me to get rid of all the if-statements on arduino side and this really long switch statement on iOS side ?

Hope you can help me out on that :slight_smile:

but my code is very ugly

Yes, it is. Using a switch statement to create an output value that is 0x50 higher than the input value does not make sense.

But this isn't working. I think because data0 hasn't the exact value i was sending and i think this occurs because of the different data types here.

No, it's because ble_read() is doing something wrong. What that would be is a mystery though. Perhaps they can help you unravel it at http://snippets-r-us.com.

I am not really using the switch statement to create an output value that is 0x50 higher than the input value. I have a lot of buttons on my GUI (on iOS App). I am developing a TV Remote Control that's why there are so many buttons. All these button have the same action which gets triggered on click (sendCode). Now i need to find out which of these buttons triggered the action. This is why i have my switch statement. Then i am sending codes from 0x50 to 0x68 to the Arduino and on the Arduino side i am sending the actual IR-Codes based on which 0xxx value i retrieved via the if statements.. But i actually want to just send the right values directly from iOS App but i am not sure how to read the values on the Arduino later than.

I am not really using the switch statement to create an output value that is 0x50 higher than the input value.

Really?

switch([sender tag]) { //the buttons have tag from 0 to 18 so i can identify each button and send another code
case 0: //button 0 was pressed
code = 0x50;
break;

code ends up 0x50 higher than whatever [sender tag] returns. Why not just add the value returned by [sender tag] and skip the switch statement?

But i actually want to just send the right values directly from iOS App but i am not sure how to read the values on the Arduino later than.

Serial data is sent one byte at a time. It is read one byte at a time. If you send 4 bytes that make up an unsigned long, you need to read 4 bytes, and reassemble the unsigned long, using a union or bit shifting.

Why not just send the [sender tag] value, and add the byte received to 0xA00 to define the code to send?

Ah ok I see. I'll have a look and try to edit my code like this.

Actually, it is probably better to send ASCII, especially between systems.