Joining integers into a string for transmission

I have been trying all day and have read many examples, this being the nearest: Similar issue which I can't get to work..

The purpose is to blast sequential codes at a Kenwood device to see what it responds to. The codes are made up of an address (9D62) the function ( 0-FF) and the inverse of the function as a checksum. I can get the string I want from the serial monitor but not delivered to the IR LED.

I would be grateful for any help you could offer to see what I am doing wrong please.

/*
Sequential code blaster for Kenwood.

Delay used because it doesn't matter in this application.
*/

#include <IRremote.h>

IRsend irsend;

#define IR_SEND_PIN 9
const byte val1 = 0;
const byte val2 = 0;
byte val3 = 0;
byte val4 = 0;


void setup()
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop()
{

  struct MyData   {
    const byte val1;
    const byte val2;
    byte val3;
    byte val4;
  };

  for (byte i = 0; i < 255; i++) {
    byte inv = (~i);
    byte pos = (i);

    struct MyData data ;
    data.val1 = 0x9D;
    data.val2 = 0x62;
    data.val3 = pos;
    data.val4 = inv;


    // irsend.sendNEC((0x9D626897), 32);//  this Tx is correct

    irsend.sendNEC((&data, sizeof(struct MyData), 32));//  Kenwood IR



    //A typical working code looks like: 0x9D626897
    //(constant address 9D62) (code) (inverse code)




    Serial.print(((byte)add), HEX),
                 Serial.print(((byte)add2), HEX),
                 Serial.print((byte)(i), HEX),
                 Serial.print(((byte)inv), HEX),
                 Serial.println("    ");

    delay(1000);
  }
  delay(5000); //restart delay

You haven’t shown us the complete sketch.

You do know the modulation frequency and the IR wavelength between the TX and RX must match ?

So what is your code doing that you consider to be "wrong" ?

Seems you are abusing the comma operator… your first parameter will be 32.

The API looks like this

That’s probably what you should use

This is everything and yes I do know that. It all works fine if I send it a complete code as per the notes. The issue is merely getting the correct string down the IR.

It isn't formatting the string I need, just sending the length of it (4) to the IR.

That is for version 4, rather than the version 2 I use (for legacy reasons).

that's usually not a good reason :slight_smile:


nevertheless when you use the comma operator - something like

a, b, c

the compiler executes a then b then c and the value returned is what the c expression was evaluated to.

so when you write

irsend.sendNEC((&data, sizeof(struct MyData), 32));

because of the double parenthesis those are not separated parameters to the irsend.sendNEC function. The compiler sees that as "pass to the function the result of the evaluation of (&data, sizeof(struct MyData), 32) which is one such comma operator expression.
so the compiler evaluates &data âžś it does nothing so it's ignored. Then the compiler evaluates sizeof(struct MyData) it returns 4 but has no side effects, so it's ignored and last it evaluates 32 and returns its execution which basically is 32. So what you wrote is seen by the compiler as

irsend.sendNEC(32);

so hopefully you can clearly see why it won't do what you had in mind.


I downloaded a stable release of version 2.0.1.
The signature of the function seems to be

void  sendNEC (unsigned long data,  int nbits) ;

the NEC protocol uses 32 bits, so that's our last parameter and we need to build the 4 bytes of the first parameter.

According to what you say this needs to be 0x9D62XYZT where XY is the function code and ZT its inverse.
âžś so you can build that

unsigned long data = 0x9D620000;    // put the address in the MSBs
unsigned long function = 0x10 ; // whatever function you want
data = data | (function << 8] | (~function & 0xFF); // build the four 4 bytes
irsend.sendNEC(data, 32); // send it over

I have no clue if the data needs to be little endian or big endian so you might want to try in the reversed way

unsigned long data = 0x0000629D;    // put the address in the LSBs
unsigned long function = 0x10 ; // whatever function you want
data = data | (function << 16] | ((~function & 0xFF) << 24); // build the four 4 bytes
irsend.sendNEC(data, 32); // send it over

you could try that out


side note: you are abusing the comma operator as well in

    Serial.print(((byte)add), HEX),
                 Serial.print(((byte)add2), HEX),
                 Serial.print((byte)(i), HEX),
                 Serial.print(((byte)inv), HEX),
                 Serial.println("    ");

in that case it does not matter much but take the good habit of separating individual statements with semi colon rather than comma. This is more common and the indentation will look nicer.

  Serial.print(((byte)add), HEX);
  Serial.print(((byte)add2), HEX);
  Serial.print((byte)(i), HEX);
  Serial.print(((byte)inv), HEX);
  Serial.println("    ");

Thank you for such a useful and informative response, I have learned from it and will tinker again next weekend. Much appreciated that you took the time to explain that so well.