Convert Serial data to unsigned int

Hello, I am having trouble using the information received via the Serial port with the IRremote.h library.
The function I want to use needs unsigned int* and I can’t find a way to convert it from the serial.

This is the function I want to call from the library:

void IRsend::sendRaw(unsigned int buf, int len, int hz)
{
enableIROut(hz);
for (int i = 0; i < len; i++) {
if (i & 1) {
space(buf*);*

  • }*
  • else {*
    _ mark(buf*);_
    _
    }_
    _
    }_
    _
    space(0); // Just to be sure*_
    }
    So what I have done is I declared some variables and then I created a switch case.
    unsigned int Q[] = {800, 750, 750, 800};
    unsigned int A[] = {750, 800, 700, 800};
    unsigned int Z[] = {750, 800, 700, 850};
    void loop()
    {
    * if (Serial.available() > 0)*
    * {*
    * char command = Serial.read();*
    * switch (command)*
    * {*
    * case ‘Q’: irsend.sendRaw(Q, sizeof(Q), 38);*
    * case ‘A’: irsend.sendRaw(A, sizeof(A), 38);*
    * case ‘Z’: irsend.sendRaw(Z, sizeof(Z), 38);*
    * }*
    * }*
    }
    It works perfect as it is, but the problem is that when I increase the number of variables and also the number of switch cases the program crashes.
    That’s why I need to create a function
    void play("a type variable){
    * irsend.sendRaw(“the variable”, sizeof(Q), 38);*
    }
    and I have been stuck in this part for a week because I can’t make it work.
    I would really appreciate your help!!!

I suggest that you look up the union structure available in C++. It allows you to define the same data space (in memory) as different variable types. In one form, you can receive two bytes and store them in a two byte array. Then you can read the two bytes out as a single unsigned integer. The union is absolutely worth mastering, since it allows any data received as bytes to be interpreted as any other data type.

Rather than a function have you considered using a 2 dimensional array?

Assuming you only have upper case letters A-Z

unsigned int letters[26][4]= {
  {800, 750, 750, 800},
  {750, 800, 700, 800},
  {750, 800, 700, 850},
  // etc etc for each letter
};

char letter_to_send='O';
char letters_index=letter_to_send-'A';  // because array is numbered 0-25 not 'A' to 'Z'
irsend.sendRaw(letters[letters_index],sizeof(letters[letters_index]),38)

Of course this will not help if you are running out of memory for all the variables.

Thank you very much for your help. I changed to arduino MEGA and it works perfect!!