Receiving multidimensional array in a function

Hi friends.. I need your help with this...
I wanna send this array to this function:

piece of code...

char commands[][9] = {"TEST1","TEST2","TEST3"};

void findCommand(char cmd[]){
	for(int i=0; i<=storedCommands; i++){
		if(strcmp(receivedCommand,cmd[i]) == 0){
			taskIndex = i;
		}
}
void loop(){
  if (newCommandReceived()){
    findCommand(commands[]);
  }  
}

Piece of answer...

To solve the problem you need to...


If you want help, post either ALL of your code or a simplified version which compiles and demonstrates the issue you are having. Then tell us what the problem you are having is, why it doesn't do what you want, what it does instead.

Ok...

SerialCommand.h

#include "Arduino.h"
//--------------------------------DECLARATIONS-------------------------------------
char serialCommand[8];  		//String received
byte serialIndex = 0;                          		//Index to write char in string

//----------------------------READ COMMAND FROM SERIAL-----------------------------
void readSerialCommand(){
    if(Serial.available()) {			  				//If data available in serial buffer
		serialIndex = 0;								//Move to start array position
		delay(50);										//Wait to receive complete message
		while(Serial.available()){ 		  				//Read message, mount array
		  cli();										//Stop interruptions
          serialCommand[serialIndex] = Serial.read();	//Read byte
          serialIndex++;								//Increase array
          serialCommand[serialIndex] = '\0';	  		//Flush array
       }
	   sei();											//Return interruptions
    }
}

//--------------------------------HAVE NEW COMMAND?---------------------------------
bool newSerialCommand(){
	readSerialCommand();
	if (strlen(serialCommand) > 0){								//If your command was received
		if (debugCommand){										//If debug is active
			printSerialCommand();								//Print command
		}
		return true;											//Device received a new message
	}
	else{
		return false;											//Nothing received
	}
}

//-----------------------COMPARE COMMAND RECEIVED FROM SERIAL-----------------------
int findCommand(char cmd[]){
uint8_t storedCommands = sizeof(cmd[])/(MAX_SIZE_SERIAL_COMMAND+1);			//How many commands are configured
uint8_t taskIndex = storedCommands+1;										//What task is matched from the serial, put out range to do switch case....

	for(int i=0; i<=storedCommands; i++){
		if(strcmp(serialCommand,cmd[i]) == 0){	//Find command in commands[]
			taskIndex = i;
		}
	}
	serialCommand[0] = '\0';				//Command received, so flush array in start position. newSerialCommand returns 
false now.
    return taskIndex
}

Comunication.ino

#include <SerialCommand.h>
char commands[][9] = {"TESTE1","TESTE2","TESTE3"};	//Array of commands from serial
int task;
void setup() {
  Serial.begin(115200);
}
 
void loop(){
  if (newSerialCommand()){
    task = findCommand(commands[]);
   switch (task)... //missing programming
  }  
}

Thanks in advance!

You would need to change "findCommand(char cmd[]){" to look for a pointer "findCommand(char * cmd){"
Then you will also need to use the strcmp() function.

Not a great example but it should help get you going.

char command[][9] = {
  "RAPID","SHORT","SOLID","OFF"};

void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  for(int i = 0; i < 4; i++)
  {
    getCommand(command[i]);
    Serial.println(command[i]);
    delay(2000);
  }
}

void loop() {

}


void getCommand(char * cmd)
{
  if(!strcmp(cmd,"RAPID"))
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
  }

  if(!strcmp(cmd,"SHORT"))
  {
    digitalWrite(13,HIGH);    
    delay(1000);
    digitalWrite(13,LOW);
  }    

  if(!strcmp(cmd,"SOLID"))
  {
    digitalWrite(13,HIGH);
  }

  if(!strcmp(cmd,"OFF"))
  {
    digitalWrite(13,LOW);
  }

}

Added:
If you want to use Case Statements, then you need to assign or #define the "command" to a number.
So RAPID = 0, SHORT = 1, SOLID = 2, OFF = 3 and so on.

Ok... I understand and this way work's....
But if I want to point to the array [] [] can I do this? Have a way to send all data to function?

But if I want to point to the array [] [] can I do this?

Yes. A pointer and an array (address) are nearly interchangeable. Since a 2D array is an array of arrays, a pointer to a pointer points to one dimension of the array of arrays.

int findCommand(char **whereToLook)

I tried but dont work...

void findCommand(char **cmd){
	...
}

void loop(){
  if(serialNewCommand()){
    findCommand(commands);
  }
}

SerialCommand.ino: In function 'void loop()':
SerialCommand:14: error: cannot convert 'char (*)[9]' to 'char**' for argument '1' to 'void findCommand(char**)'

I got it another way, comparing each command, but would like to learn how to point to the array of array ...

Workking with this:

void loop(){
getSerial();
}

void getSerial(){
    if (serialNewCommand()){
         if(matchCommand(commands[0])){
         //
    } 
    if(matchCommand(commands[1])){
         //
     }
    }
  }
}

bool matchCommand(char *cmd){
	if(strcmp(serialCommand,cmd) == 0){			//Compare command whit message received from serial
		return true;
	}
	else{
		return false;
	}
}

I used a slightly different parsing technique here... May be too simple to be helpful:
http://forum.arduino.cc/index.php?topic=147550.5

Ray

There is a library, Arduino Playground - HomePage