picking a #define from a char?

Hello,
On my Arduino IR remote / Cassette deck project I have I have set-up the controls with a load of #define statements, like so:

#define PAUSE_1	        0x411E807F
#define FAST_FWD_1      0x411EA05F
#define PLAY_FWD_1      0x411E20DF

and when I want to have the deck do something, I send the IR Command:
irsend.sendNEC(PLAY_FWD_1, 32); and this works.
However, I am using the serial port on the Ardunio to get the commands, the command is a char array, and this does not work:

char cmd[11] = "PLAY_FWD_1";
irsend.sendNEC(cmd, 32);

gives the errors:
W_890_IR_Control_test:151: error: invalid conversion from 'char*' to 'long unsigned int'
W_890_IR_Control_test:151: error: initializing argument 1 of 'void IRsend::sendNEC(long unsigned int, int)'

So, how do I change the char so it is recognised as the defined constant, or am I doing it wrong?
Thanks
Karl.

From the Serial you are getting an array of characters. That first needs to be converted to an unsigned long.

#define macros are only in existence up to the point when you compile the sketch. One of the first things the compiler does is go through and replace any instances of a #define macro with the contents of the macro. You will never have access to those macros from within your sketch.

You will have to compare the contents of your char array with some predefined (constant) char arrays (literals) and call the send function accordingly:

char cmd[11] = "PLAY_FWD_1";

if (strcmp(cmd, "PLAY_FWD_1") == ) {
  irsend.sendNEC(PLAY_FWD_1, 32);
}

This of course gives you the flexibility to use any command you choose to trigger the sending of PLAY_FWD_1.

Sorry, that didn't work, here is what I tried:

#include <IRremote.h>

#define PAUSE_1	        0x411E807F
#define FAST_FWD_1      0x411EA05F
#define PLAY_FWD_1      0x411E20DF

const int IR_TX=3;
IRsend irsend;

void setup() {
  Serial.begin(9600);
  
  char cmd[11] = "PLAY_FWD_1";
  
  unsigned long ircmd = atoi(cmd);
  Serial.println(cmd);    //output:  PLAY_FWD_1
  Serial.println(ircmd);  //output: 0
  
  //irsend.sendNEC(PLAY_FWD_1, 32);  // works
  
  irsend.sendNEC(ircmd, 32);  // does not work

}

void loop() {
}

Karl.

karl101:
Sorry, that didn't work, here is what I tried:

#include <IRremote.h>

#define PAUSE_1         0x411E807F
#define FAST_FWD_1      0x411EA05F
#define PLAY_FWD_1      0x411E20DF

const int IR_TX=3;
IRsend irsend;

void setup() {
  Serial.begin(9600);
 
  char cmd[11] = "PLAY_FWD_1";
 
  unsigned long ircmd = atoi(cmd);
  Serial.println(cmd);    //output:  PLAY_FWD_1
  Serial.println(ircmd);  //output: 0
 
  //irsend.sendNEC(PLAY_FWD_1, 32);  // works
 
  irsend.sendNEC(ircmd, 32);  // does not work

}

void loop() {
}




Karl.

Of course not - "PLAY_FWD_1" is not a number, in the same sense that "Tunafish" isn't a number. Macros don't work in quotes. Also, as I mentioned previously, they don't exist after compilation, so you can't use the name of them for anything.

Hello majenko,

I didn't see your strcmp reply until after I'd posted.

Thanks for the answer. I now have it working.

Karl.