Using a variable as command

I am a bit unsure of my terminiology here so please bear with me. I am using a library (IRremote.h) and to send a IR code with this library I need to specify the manufacturer/protocol, for instance:

IrSender.sendNEC(sAddress, sCommand, sRepeats);
IrSender.sendPanasonic(sAddress, sCommand, sRepeats);
IrSender.sendSony(sAddress, sCommand, sRepeats);

I have already written a sketch to identify the manufacturer/protocol and keycodes and I want to automatically retransmit them. But I've gotten stuck with using a variable in the above command. For instance, I have the make "NEC", Sony" etc. in a variable called "make" and I have tried:

String sender = "send" + make;
Serial.print(sender);
IrSender.sender(address, command, 3);

The monitor duly prints "sendNEC" or "sendSony" but the sketch fails with the warning that:

'class IRsend' has no member named 'sender'

How can I dynamically change the command according to the manufacturer/protocol using my variable "make"?

At runtime, the processor knows nothing at all about symbols, so adding "send and "NEC" is...just a String.

The monitor duly prints "senderNEC" or "senderSony"

Then you have a MASSIVE problem, far beyond what you want to do.

Maybe you meant "The monitor duly prints "sendNEC" or "sendSony" ".

You could use "make" to select the correct class method.

if (make == "NEC") {
  IrSender.sendNEC(sAddress, sCommand, sRepeats);
}

Thanks, I have edited the OP to correct my typo. I will use your suggestion regarding using make to select the class method but as there are more than 20 possibilities I will use switch rather than if statements.

steveinaustria:
Thanks, I have edited the OP to correct my typo. I will use your suggestion regarding using make to select the class method but as there are more than 20 possibilities I will use switch rather than if statements.

Does switch work with Strings?

Bet not.

Absolutely not

Instead of your String 'make', use a variable of the type 'decode_type_t'. It's a enum defined in IRProtocol.h. You can use an enum in a switch / case structure.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.