Serial.available() gives echo?

I'm having trouble with getting the serial comunications to work correctly.

The goal is to send a letter to the arduino(Uno) via serial and to have the arduino reply with the next letter in the alphabet.

The method I'm using is to send the letter, turn the letter into an index value, increment the index value and send back the letter corresponding with the incremented index value.

The problem is that after I send a letter and it replies with the correct letter, that it seems to set of the serial.available() which takes some non-existent letter, gives letterindex = -1 , increments that and replies with "a".

(Serial monitor output):

give letter:
received letter : k
nextletter: l
received letter :  

nextletter: a
received letter : h
nextletter: i
received letter : 

nextletter: a

Program:

char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
String Alphabet = "abcdefghijklmnopqrstuvwxyz"; 
char letter;
int letterindex; 

char followletter;

void setup() {

	Serial.begin(9600);
	Serial.println("give letter:");
}

void loop() {

	
	if (Serial.available()>0){
		//Serial.print("probe : ");   Serial.println(letter);
		letter = Serial.read();
		Serial.print("received letter : "); Serial.println(letter);
		letterindex = Alphabet.indexOf(letter);
		//Serial.print("firstindex : " );  Serial.println(letterindex);
		letterindex ++ ;
		//Serial.print("secondindex : " ); Serial.println(letterindex);
		followletter = alphabet[letterindex] ;
		
		Serial.print("nextletter: ");
		Serial.println(followletter);
		
		
	}
	delay(100);
}

So, the question is: why is this occurring and what is the best way to fix it.

You are probably receiving a line-ending character. You can constrain your input characters:

		letter = Serial.read();
        letter = constrain(letter, 'a', 'z');

johnwasser:
You are probably receiving a line-ending character. You can constrain your input characters:

		letter = Serial.read();

letter = constrain(letter, 'a', 'z');

Tried it, it does not prevent the echo, it just turns the

received letter : 
nextletter: a

echo into an

received letter : a
nextletter: b

echo

So, the question is: why is this occurring and what is the best way to fix it.

The monitor is probably set with a new line or carriage return line ending.

Set the serial monitor for no line ending.

You are better off avoiding the String class, and use strchr and pointers to find the letter index. I have put in a test for 'z' .

char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
//String Alphabet = "abcdefghijklmnopqrstuvwxyz";
char letter;
//int letterindex;
char* letterindex;
//char followletter;

void setup() {

  Serial.begin(9600);
  Serial.println("give letter:");
}

void loop() {
  if (Serial.available() > 0) {
    //Serial.print("probe : ");   Serial.println(letter);
    letter = Serial.read();
    Serial.print("received letter : "); Serial.println(letter);
    //letterindex = Alphabet.indexOf(letter);
    //Serial.print("firstindex : " );  Serial.println(letterindex);
    letterindex = strchr(alphabet, letter);
    //letterindex ++ ;
    //Serial.print("secondindex : " ); Serial.println(letterindex);
    //followletter = alphabet[letterindex] ;
    Serial.print("nextletter: ");
    if (letter == 'z')
      Serial.println("last letter, no next letter");
    //Serial.println(followletter);
    else
    {
      Serial.write(letterindex + 1, 1);
      Serial.println();
    }
  }
  delay(100);
}

Thank, you were right the serial monitor's "new line" was causing it.
though I don't quite understand why (or how) serial.Write is used.

akory:
though I don't quite understand why (or how) serial.Write is used.

Well, the "how" is how you used it.
The "why" only you can answer.

I don't quite understand why (or how) serial.Write is used.

When using the c string function strchr() it returns a pointer to a matching location in the null terminated character array called alphabet. This array lives somewhere in 27 bytes of continuous memory holding 26 letters plus the null terminator.

Serial.print(letterindex) will print all the characters from the pointer to the null. In order to print only one byte/char you need to use the Serial.write() function with the number of bytes as the second parameter. In order to print the next character after the pointer returned by the match, you need start at letterindex+1.