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
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.