0

I'm making a project with an sd card where the user enters a file name over the serial port, and then the aduino does thing to that file. Right now i'm working on getting the serial input to work, but one Serial.print() is somehow changing a variable/output of another Serial.print(), but before it is even called. Here is my code: char input, output;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Ready\nEnter input text");
  while (!Serial.available()); // wait till serial available
  delay(2000);
  byte avail = Serial.available() - 1;//extra newline char at end that we don't want
  char inBuf[avail];
  //Serial.println(avail);
  Serial.readBytes(inBuf, avail);
  Serial.read();
  Serial.print("'");
  Serial.print(inBuf);
  Serial.println("'");
  delay(1000);
  Serial.println(avail);//problematic line
}

void loop() {
  // put your main code here, to run repeatedly:

}

When i have the line uncommented, the output is:

Ready
Enter input text
'asd&.'
4

When all i have typed is 'asd' (no quotes). With the line commented out, the output is as expected:

Ready
Enter input text
'asd'

I have no clue how it is happening, and should be physically impossible, since it is changing code before it is called.

0
2

Your string needs to end with a null character '\0'. I think yours does not because of the way that you filled the buffer, so you're getting whatever junk is in memory next to your array printed in addition to the array itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.