Serial Printing Duplicate entries

I am using Serial to send commands to the Arduino, everything works fine except that after every command that is sent it duplicates the previous input.
Example:
Send 1: s085 Received: s085
Send 2: e100 Received: s085
Send 3: e095 Received: e095
Send 4: q045 Received: e095
Send 5: q045 Received: e045
etcetc

this is the current code:

char buffer[5];
byte bufferindex = 0;
long number = 0;


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

void loop() {
  while (Serial.available())
 {ReadSerialData(Serial.read());}
  
}


void ReadSerialData(char inChar) {
  
  //if buffer is full, reset
  if (bufferindex == 5) {bufferindex = 0; number = 0;}
  //if first character is 's', continue, increment
  if (bufferindex == 0 && ( inChar == 's' ||  
                            inChar == 'e' ||  
                            inChar == 'w' ||  
                            inChar == 'q' ||  
                            inChar == 'r')){
      buffer[bufferindex] = inChar;
      bufferindex++;}
   //if reading inputs 1-3 and input is numeric, set value, increment
 if (bufferindex >= 1 && bufferindex <= 4 &&  inChar >= '0' && inChar <= '9')
     {number = number * 10 + (inChar - 48); bufferindex++;}   
      //if the buffer is properly encoded, execute command; in this case display output
      if (bufferindex == 4) {
      // movement(buffer[0], number);  //this will be your payload
         Serial.print("Your input is: ");//delete this after testing
         Serial.print(buffer[0]); //delete this after testing
         Serial.println(number);//delete this after testing
       }}

EDIT: Code edited for clarity

Any tips on how to remove the duplicate entries?

It is best practice to delay some millis to avoid the serial buffer from overloading, i dont know if its causing the problem, but give it a try.

Would a delay() be sufficient or would I actually have to use the millis() function? I am still fairly new to working with the Arduino or the millis function :-/

EDIT: There is a minimum of 5 seconds before I send a command through to the arduino

It is best practice to delay some millis to avoid the serial buffer from overloading

It is best to slow down the speed at which you remove data from the serial buffer, to keep it from overflowing? This makes no sense.

A delay before sending data might be useful, but, since the sender has no control over how fast the data is read (and removed from the buffer) or the size of the buffer on the receiver, it likely isn't a useful thing to do, either.

( inChar == 115 ||  inChar == 101 ||  inChar == 119 ||  inChar == 113 ||  inChar == 114

:-?

AWOL:

Only commands beginning with character s || e || w || q || r are to be accepted, anything besides that will not allow the array to be filled/continue(or so i hope)

Only commands beginning with character s || e || w || q || r

So why didn't you write that? If you're OK with '0' and '9', why not 's', 'e' etc?

Hey, SPUD, is your return key broken?

if (bufferindex == 0 && ( inChar == 115 ||  inChar == 101 ||  inChar == 119 ||  inChar == 113 ||  inChar == 114)){buffer[bufferindex] = inChar;bufferindex++;}

Why are there numeric equivalents of ASCII characters in here? Are you sending a 115 or are you sending an 's'? If you are sending an 's', why not compare the value received to an 's'?

if (bufferindex == 0 &&
      ( inChar == 's' ||
        inChar == 'e' ||
        inChar == 'w' ||
        inChar == 'q' ||
        inChar == 'r'))
   {
      buffer[bufferindex] = inChar;
      bufferindex++;
   }

Spread the code out so that it is readable. Use values that are recognizable.

Sorry, forgot to clarify.

The Arduino listens for commands beginning with the listed characters, the number value from buffer[1-3] are viewed as an angle

once the array is filled with 4 bytes the 1st character of the array(the letter) and the integer value of the angle(variable named number in the code) is fed into a function called movement() which expects a character to specify which subroutine(case) to call and what angle to set it.

q & r control servos. e s w control a few solenoids firing which then move a robotic arm position of which is tracked by 3 potentiometers(one for shoulder, one for elbow, one for wrist)

PaulS: Sorry, a stupid habit from highschool coding days that stuck with me, always forget to modify the code for readability of others, should have un-Clusterfracked the code before posting.

Also thank you for the suggestion to compare to an actual char instead of the ASCII value of it.

I am using Serial to send commands to the Arduino

What is sending the serial data? Is it adding a carriage return/line feed at the end, perhaps?

What is sending the serial data? Is it adding a carriage return/line feed at the end, perhaps?

I myself am manually entering it into the Arduino Serial Monitor. Unless the serial monitor automatically adds a carriage return/line feed

You have a block to deal with bufferindex equal to 5. You have a block to deal with inChar equal to one of several values when bufferindex is 0. You have a block to deal with inChar equal to a number when bufferindex is 1 to 3. You have a block to deal with bufferindex equal to 4.

You should print out some message, showing inChar as a character and as a number, and bufferindex, when inChar is not the expected value for the given value of bufferindex.

Perhaps this would give you a clue.

Also, put the open and close curly braces on separate lines, and indent the code for the block. It's still hard (though not as bad) to read your code. It's not like white space costs you anything.