How to assign text to a variable [Solved: 'enum' assignment to 'string' to LCD]

Hello all, I have been attempting to assign text to a variable, and I am not succeeding.

Essentially, I want to set a variable equal to a string, and then call that string & print the string. Everything works with the project, so I am only posting the relevant lines.

I have read the object constructors and the variables reference material, without any success.

Thank you all for the help, it is very much appreciated.

-JB

....


String DataRate = "RF24_250KBPS";   // I want to call DataRate's value of RF24_250KBPS


//char DataRate[14] = {'R','F','2','4','_','2','5','0','K','B','P','S',0}; (this is a different attempt)



....

void setup() 
{
  Serial.begin(9600)
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  radio.begin();
  radio.setDataRate(DataRate); // and place the value of RF24_250KBPS here
  lcd.print(DataRate);               // and here
}

The RF24 setDataRate() method takes an enumeration rf24_datarate_e for the data rate, not a string. Therefore, you can set the data rate like this:

  radio.setDataRate(RF24_250KBPS);

If you want to pass it a variable then you need to declare a variable of type rf24_datarate_e:

  rf24_datarate_e dataRate = RF24_250KBPS;

  radio.setDataRate(dataRate );

RF24_250KBPS is a member of an enumeration. The human readable name has no meaning at runtime. It is converted to an integer value.

Your example doesn't explain why this is not sufficient to meet your needs....

radio.setDataRate(RF24_250KBPS);

Other than perhaps...

lcd.print(DataRate);

Does not produce the expected results.

You will need a case statement to translate back from the enum value to a human readable string if you want to do that.

Edit:

String sDataRate;
rf24_datarate_e enumDataRate;

switch case (enumDataRate)
{
  case RF24_250KBPS:
    sDataRate = "250KBPS";
    break;
  .....
}

ToddL1962, Thank you sir! I definitely understand why that did not work now that I see the expected input type differed from the string type (would I be correct in saying the expected input 'class' differed from the string 'class'?).

The code snip below solved the problem.

  rf24_datarate_e DataRate = RF24_250KBPS;

  radio.setDataRate(DataRate);

However, to the point brought up by pcbbc, I have had the program running correctly, by running the code below.

radio.setDataRate(RF24_250KBPS);
lcd.print("F24_250KBPS");

I just wanted to learn how to assign a value of "RF24_250KBPS" initially so that I can then call that value to set the data rate, and print to the lcd. Eventually, I'll want to have the ability to change the DataRate using a switch case, and only need to change one value instead of searching through the code.

Thank you both!

The # stringify preprocessor operator may prove useful in situations like this.

For the sake of completion and the good of humanity here is how the problem was resolved :smiley:

....

String sDataRate;

rf24_datarate_e DataRate = RF24_250KBPS;

....

void setup() 
{

  radio.setDataRate(DataRate);
  
  switch (DataRate)
  {
    case RF24_250KBPS:
      sDataRate = "250KBPS";
      break;

// Other data rate cases will be placed here

  }
  lcd.setCursor(0, 0);
  lcd.print(sDataRate);
}