using Serial.read()

hi
i am using the reference code for Serial.read() in order to recieve a number:

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

but...
it seems like it is getting the '\n' as well.
the output for enterring 7 for example:

I received: 55
I received: 10

any suggestions?

Set the Serial monitor to "No line ending".

Steve

any suggestions?

Don't send the '\n' or if you have no control over whether it is sent or not then test the received byte and ignore irrelevant data

Incidentally, why is incomingByte declared as an int ?

UKHeliBob:
Incidentally, why is incomingByte declared as an int ?

Because that’s the type that Serial.read returns?

TheMemberFormerlyKnownAsAWOL:
Because that's the type that Serial.read returns?

Whilst it is true that the function returns an int, I bet that the OP does not know that, and in any case the sketch does not make use of the fact that the value could be negative.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

thanks!!!
no link ending solve it :slight_smile: