how to compare multiple array and send data

Hello everyone. i m doing coding on dock-light and arduino interfacing where im receiving commands from dock-light and according to that im responding through arduino.
I’ve made an array of 4 const variables and the data in it. the problem im facing is that im unable to compare the second char with the 2nd command giving through dock-light. when ever im giving the second through docklight im getting the previous output as well as with the present output.
it’d be a oblige if would help me through this code. thanks in advance

CODE:
int i, compare=1;
char incomingdata[4];
const char a[4]={2,‘0’,‘1’,3};
const char c[4]={2,‘0’,‘2’,3};
const char d[4]={2,‘0’,‘3’,3};
const char e[4]={2,‘0’,‘6’,3};

void setup()
{
Serial.begin(115200);
}

void loop()
{
if(Serial.available()>0)
{
Serial.read(incomingdata,4);
for(i=0;i<4;i++)
{
if(incomingdata_!=a*) compare=0;_
_
if(compare)_
_
{_
_
Serial.write(“T”);_
_
}_
_
compare++;_
_
Serial.flush();_
_
} _
_
Serial.read(incomingdata,4);_
_
for(i=0;i<4;i++) _
_
{_
if(incomingdata_!=c) compare=0;
if(compare)
{
Serial.write(“u”);
}
compare++;
Serial.flush();
}
Serial.read(incomingdata,4);
for(i=0;i<4;i++)
{
if(incomingdata!=d) compare=0;
if(compare)
{
Serial.write(“i”);
}
compare++;
Serial.flush();
}
Serial.read(incomingdata,4);
for(i=0;i<4;i++)
{
if(incomingdata!=e) compare=0;
if(compare)
{
Serial.write(“p”);
}
compare++;
Serial.flush();
}
}
}*_

You can see now why we ask you to use code tags.

    Serial.read(incomingdata,4);

read() does not take any arguments.

Check out the memcmp() function.

econjack:
Check out the memcmp() function.

Is that the one to use in italicized code?

The way I read your code there are 4 valid 4 character commands that come in the serial port. When one of them is entered the Arduino sends a 1 character command to the serial port.

Your code would be more flexible if you put all the valid commands in one multi-dimensional array and compared each incoming group of 4 chars to each valid command in turn. Another array could hold the character to send when a valid command is found.

I wrote some demo code to consider. Your arrays named ‘a’, ‘c’, ‘d’ and ‘e’ mixed integer values and character codes - I changed them all to character codes to work easily with the serial monitor. You can upload the attached, open the serial monitor set to No line ending and type the commands, 2023 for example, and the correct output should be written to the serial monitor.

const byte NUM_COMMANDS = 4;
const byte COMMAND_SIZE = 4;
char incomingdata[NUM_COMMANDS];

// array with all incoming commands
const char commands[][COMMAND_SIZE] = {
  {
    '2','0','1','3'  }
  , {
    '2','0','2','3'  }
  ,{
    '2','0','3','3'  }
  ,{
    '2','0','6','3'  }
};

// commands to send
const char sends[NUM_COMMANDS] = {'T', 'u', 'i', 'P'};


void setup()
{
  Serial.begin(115200);
}

void loop()
{
  // if we have a full command
  if(Serial.available()>=COMMAND_SIZE)
  {
    //  read it
    Serial.readBytes(incomingdata,COMMAND_SIZE);
    
    boolean foundCommand = false;  // true => got valid command
    
    // while more commands
    for(int cmdIdx=0; !foundCommand && cmdIdx < NUM_COMMANDS; cmdIdx++)
    {
      // compare incoming to valid command
      int idx=0;
      for( ; idx < COMMAND_SIZE; idx++) if( commands[cmdIdx][idx] != incomingdata[idx] ) break;
      
      // if we got a match
      if( idx == COMMAND_SIZE )
      {
        // send appropriate character
        Serial.write(sends[cmdIdx]);
        
        // signal done
        foundCommand = true;
        
      } // if
      
    } // for

  } // if
}