Pages: [1] 2 3   Go Down
Author Topic: Serial.read crazy results  (Read 2666 times)
0 Members and 1 Guest are viewing this topic.
Berlin
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I'm getting this problem that's driving me crazy, tried everything and googled for hours, now my last hope is this forum.

Here is the code:
Quote
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:
  while ( Serial.available( ) > 0 ) {

                incomingByte = Serial.read();
                
                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte);
                
  }
        delay(500);

}


When I enter an integer, I should get it back right?

Here is what I get when I enter "4" instead:

Serial monitor:

I received: 52
I received: 10

Here is "0":

Serial monitor:


I received: 48
I received: 10


What am I missing?

Please advise

Thanks
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25884
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're missing the difference between the ASCII representation of a digit, and the digit itself.
Comes up about once a month here.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Berlin
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I get that one, but it doesn't explain the systematic second line = 10.
It is the ASCII symbol for a new line and I don't know how to get rid of it.

Any ideas?
Logged


ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1593
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are going to get ASCII representation codes on Key stroke inputs , You will understand more on reading about ASCII values on respective Key Strokes, Google for it.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25884
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Carriage return / line feed,
13 / 10
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Berlin
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

and how can I fix this?
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25884
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean "fix"?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use something different to input your data, that doesn't send CR/LF.
Logged

Berlin
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your answer dxw00d, but man I'ma Newb smiley-grin

what you wrote is like chinese to me. could you please explain further? I'd be grateful to you

Thanks
Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you entering your "4"? Are you using the serial monitor?
Logged

Berlin
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes
Logged


Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you can try Serial.write for incomingByte, and use if instead of while if you only need 1 byte. I might try it later.
Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are two drop-downs at the bottom of the serial monitor. One of them selects what is sent at the end of whatever you type. It might say 'Newline', or 'Carriage return' currently. Try changing it to 'No line ending'.

If that doesn't work, you'll need a different terminal program.
Logged

Berlin
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Works great! with: 'No line ending'

but now it reads only one digit. So if I type 10, I get :

I received: 1
I received: 0

what should I change?

PS: Thanks dxw00d smiley
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25884
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not read the terminator....and ignore it?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2 3   Go Up
Jump to: