I’ve been having trouble getting the ArduinoMega to read a complete text string sent from the serial monitor into a char array. I’ve tried a few different ways to do it but it will loose 2-4 charachters off the end 90% of the time. I tried looking for answers but didn’t find any that have worked. I tried one of Nick Gammon’s examples with the same effect.
It is to ask for text strings from the user computer and store the strings in EEPROM for later use after restart.
bool interface()
{
int l;
int p;
int pos = 0;
int noBytes = 0;
char inChar;
char inData[128];
boolean m = false;
inbyte = Serial.read();
if (inbyte == 'a'){
Serial.println("CONFIGURE UNIT STREAM OUTPUT");
//store 4 char stream ID at EEPROM 100-103;
Serial.println("enter 4 letter stream indentifier");
while( m == false){
if(Serial.available() >= 4){ //also tried simply "> 0"
inChar = Serial.read();
switch(inChar){
case '\n':
inData[pos] = 0;
Serial.println("you entered");
for(int n=0; n<4; n++){
Serial.print(inData[n]);
}
Serial.println();
p = 100;
for(int n=0; n<4; n++){
EEPROM.write(p, inData[n]);
p++;
}
Serial.flush();
m = true;
break;
case '\r':
break;
default:
if(pos < 5){
inData[pos] = inChar;
pos++;
}
break;
}//end switch
}//end if available
}//end while
m = false;
If I enter “ABCD” and send I get back AB and sometimes despite having called Serial flush I get the CD part on the next entry step.
I also tried it this way. Sometimes this step works correctly, sometimes not.
//store 10 char city state at EEPROM 104-113
Serial.println("enter 10 letter city state");
while( m == false){
if(Serial.available() >= 10){
delay(100);
noBytes = Serial.available();
l = 0;
while(Serial.available() > 0){
inData[l] = Serial.read();
l++;
}
Serial.println("you entered");
for(int n=0; n<10; n++){
Serial.print(inData[n]);
}
Serial.println();
int p = 104;
for(int n=0; n<10; n++){
EEPROM.write(p, inData[n]);
p++;
}
Serial.flush();
m = true;
}
}
So are there any exaples of entering strings of text via serial monitor???