Calling One Specif function based on PreDefined Key-Words

Hey all,

What is the best way to a call specif function based on defined Keywords.

That is if, "HELP" or "SomeWord" is passed from Serial, My program Should Call respective Function.

KeyWords[ ][11]={"HELP","TIME","DAY","AAAAA","BBBBB"}; and I have respective functions help(),time(),etc...

My question is Simple, If i type HELP and hit enter in Serial monitor, In back-end My arduino Should call HELP();

Thanks in advance.

The ## preprocessor operator is useful in situations like this, as are the "strcmp" or "strcmp_P" functions.

The examples in serial input basics are simple reliable ways to receive data and there is a parse example that could be used to extract the words, if required.

If the data is being sent by a PC program (rather than a human operator) it would make your Arduino code much simpler if you reduce the commands to a single character such as 'H', 'T', 'D' etc

...R

Not too pretty, but should work:

char *KeyWords[11]={"HELP","TIME","DAY","AAAAA","BBBBB"}; 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  

}

void loop() {
  char buffer[20];
  int charsRead;
  int i;

  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
    buffer[charsRead] = '\0';      // make it a string

    for (i = 0; buffer[i]; i++) {
      buffer[i] = toupper(buffer[i]);   // Convert to uppercase so aaaaa or AAAAA work
    }

      
    for (i = 0; i < sizeof(KeyWords) / sizeof(KeyWords[0]); i++) {
      if (strcmp(KeyWords[i], buffer) == 0) {
        break;
      }
    }
    switch(i) {
      case 0:
        help();
        break;
      case 1: 
        time();
        break;
      case 2:
        day();
        break;
      case 3:
        aaaaa();
        break;
      case 4:
        bbbbb();
        break;
      default:
        Serial.println("Shouldn't even get here.");
        break;
    }  
  }
}

void help()
{
  Serial.println("In Help");
}

void time()
{
  Serial.println("In Time");
}

void day()
{
  Serial.println("In Day");
}

void aaaaa()
{
  Serial.println("In AAAAA");
}

void bbbbb()
{
  Serial.println("In BBBBB");
}

Thanks econjack & Robin2, Thanks for your time.. I totally got it.

My Progress: I Wrote a String compare function and calling It every time to compare, and returning no. of times It is call,To know the position. Works Great!

boolean myStrcmp(const char Str1[],const char Str2[])
{ 
  byte z;
  for(z=0;Str1[z]==Str2[z];z++)
     if(Str1[z]=='\0')
        return true; // if they are same
   return false; 
}

Now My doubt is, In My case, I am getting more than 25 Key-Words. Should I use the same Switch-statement method,or is there any other Optimal method ,If I want to scale it for more than 50(>50) words Like that.

Thanks in advance :slight_smile:

,If I want to scale it for more than 50(>50) words Like that.

Maybe you should start considering the use of flash memory, and "strcmp_P"

Thanks AWOL,..Ya I ll start using FlashMemory.

How should I call Functions?, Please Help me in this:

Now My doubt is, In My case, I am getting more than 25 Key-Words. Should I use the same Switch-statement method,or is there any other Optimal method ,If I want to scale it for more than 50(>50) words Like that.

Thanks in advance :slight_smile:

Via a table of function pointers?

BhanuTejaJ:
If I want to scale it for more than 50(>50) words Like that.

Are you going to have a different function for each of 50 different words ?

This is beginning to sound like an XY problem.

I think you should tell us what your project is all about.

...R

Now My doubt is, In My case, I am getting more than 25 Key-Words. Should I use the same Switch-statement method,or is there any other Optimal method ,If I want to scale it for more than 50(>50) words Like that.

Thanks in advance :slight_smile:

Via a table of function pointers?

Up till no, I just used the Code mostly similar to #3 post. Using SwitchCase and returning back to main function and waiting for next serial Inupt.

I dont know anything about functions pointers :frowning:

Are you going to have a different function for each of 50 different words ?

Uptill Now, 16 Functions completed and...more to come...

Yep, Every Key-word, has its own function :expressionless: , Thats why I am asking , My Projects is, When a key-word is typed it should respond back with some data serially, ..Some thing similar to "terminal or cmd" over serial.

Hardware: Arduino Mega, Sd Card, RTC, Some Sensors

BhanuTejaJ:
My Projects is, When a key-word is typed it should respond back with some data serially,

I find it hard to visualize the purpose of this from your description and I have a suspicion that if we knew what you are really trying to achieve there is a much easier way.

Or maybe it is not really a project for which an Arduino is suitable / ideal.

...R