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.
#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
}
}
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.
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.
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.
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;
}
}