Serial communication garbage problem

i am a newbie and i want to send a simple integer to arduino via serial communication, i get the value but there is also garbage valuse i don’t understand to reslove it… a little help will be appreciated.

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

void loop()
{
  while (Serial.available() == 0);
  int val = Serial.read() - '0';
  Serial.flush();
  Serial.println(val);
  
}

http://www.droidmaniac.com/wp-content/u … pture3.png

Serial.flush() is to wait for your OUTGOING message to finish sending, does nothing here.

That being said, I just copy-pasted your code and I don’t get the -38s lol so don’t . This code can only handle the single digits 0-9 btw (or input a char and get back it’s ascii value). Put in two digits (or a negative number) and your serial.read will read them in separately.

That acsii value for ‘line feed’ is 10. So my best guess at what might be happening is you are sending your integer value AND a ‘line feed’ char. The first serial.read gives you the integer back like you intend. The second gives you ‘line feed’ - ‘0’ (10-48=-38). That’s all I got.

Your Arduino code is not just sending one byte but two. The println routine prints your data followed by a terninator (carriage return or line feed). Change that to a print routine and the extra character will go away.

Here is the answer to the OP’s problem:

http://forums.adafruit.com/viewtopic.php?f=25&t=42708

Abubakr022 now added to my ignore list for this breach in etiquette.

fll-freak:
Abubakr022 now added to my ignore list for this breach in etiquette.

Eh? What breach? Did he send you a PM?

UNTEngineer:

fll-freak:
Abubakr022 now added to my ignore list for this breach in etiquette.

Eh? What breach? Did he send you a PM?
I believe the breach is the simultaneous cross posting of the question in different forums. This has the scent of expecting and exercising a whole bunch of people to answer your question as quickly as the OP wants (expects/demands) them to. I consider the practice a bit rude. Asking in your primary forum and waiting some time before asking in another is good manners. Add to that he had his answer (2 hrs after asking) and didn't bother to say "Thx" there or let anyone here know that the problem was solved (and so don't waste your time people).

Mee_n_Mac:

UNTEngineer:

fll-freak:
Abubakr022 now added to my ignore list for this breach in etiquette.

Eh? What breach? Did he send you a PM?
I believe the breach is the simultaneous cross posting of the question in different forums. This has the scent of expecting and exercising a whole bunch of people to answer your question as quickly as the OP wants (expects/demands) them to. I consider the practice a bit rude. Asking in your primary forum and waiting some time before asking in another is good manners. Add to that he had his answer (2 hrs after asking) and didn't bother to say "Thx" there or let anyone here know that the problem was solved (and so don't waste your time people).

Ah…thats understandable.