Invalid conversion when making a function that passes parameters to digitalWrite

westfw:

  if(strcmp(relay, "relay1") == 0){

if(strcmp(state, "LOW") == 0){
        digitalWrite(relay1, LOW);




That will work. You'd normally use some sort of table:



cmdType NameToRelayNo[] = {
  {"relay1", relay1},
  {"relay2", relay2},
  {"pump", relay2},  // another name for relay2
  {"relay3", relay3},
  {"lights", relay4} ...
}





If you really only want to accept names like "relayNN", you can convert the NN part to a number directly.


relayNo = atoi(&relay[5]);  // parse number starting at 6th character.

  • Are there any downsides or edge cases to using atoi?
  • The "table" looks like a 2D array. So NameToRelayNo[0][0] would be the string name or char name, and NameToRelayNo[0][1] would be the variable. So like digitalWrite(NameToRelayNo[0][1], LOW)

There's still my question of how to use char or string "numbers" like "1" or "0" if I'm forced to use them (since they're coming from the MQTT server).

Would it have to use atoi then?

void relayControl(const char relayNo){
  int relayNumber = atoi(relayNo);
  digitalWrite(NameToRelayNo[relayNumber][1], LOW);
}