Code directly fm this website, bad results

This was copied and pasted directly from this website to my IDE with no modifications whatsoever, compiled, and loaded to my UNO:

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);
}
}

Serial Monitor gives this result when I type the number 1 in the Send box:

I received: 49

The ASCII value ! Why is it not 1? >:( When I change DEC to BYTE, I get 1! :-/ WTH

I've just about had it with this thing. :frowning:

Maybe try the below:

char incomingByte;      // 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);
     }
}

I'd be demanding my money back for the software.

I've just about had it with this thing.

My dear bka,

going by your various posting, all your problems are caused by the simple fact that you don't know how to program in C or C++. The Arduino isn't to blame at all for your frustration.

To reduce that frustration I advise you to work through some basic "How to Program in C" text (there are many available on the net) to learn the basics like variables, data types, looping up to pointers and arrays. That course doesn't even need to have any relation with the Arduino, any course will do as long as you like it. Once you have that, many of the strange things you encounter in the Arduino environment will become clear and you can go on and concentrate on the specific challenges presented by programming microcontrollers like the Arduino. I think, that will give you a better way to progress than the way you try to learn at present.

Korman

My dear bka,

going by your various posting, all your problems are caused by the simple fact that you don't know how to program in C or C++. The Arduino isn't to blame at all for your frustration.

To reduce that frustration I advise you to work through some basic "How to Program in C" text (there are many available on the net) to learn the basics like variables, data types, looping up to pointers and arrays. That course doesn't even need to have any relation with the Arduino, any course will do as long as you like it. Once you have that, many of the strange things you encounter in the Arduino environment will become clear and you can go on and concentrate on the specific challenges presented by programming microcontrollers like the Arduino. I think, that will give you a better way to progress than the way you try to learn at present.

Korman

My dear Korman,

You may be right. However, I did not program the code the I copied and pasted from this website. The very code that gave the results I posted. I'm assuming the author of the example that I copied does know how to program C/C++. Perhaps you can point out his/hers lack of knowledge.

Oh, and the data type for incomingByte was int in the author's original code - the result was the same...I had changed it to char to see what the result would be and didn't change it back prior to copying it for this post. Korman, I didn't mean to come across as a smart a$$...I just want this to work so much....

The program you posted did exactly that what the original author wanted it to do. At least I assume that much as it tests quite well that the serial communication is working properly and provides good debugging data. The problem arises only because you imagine that the program does something else and then wonder, why you don't get the results you expect.

The ASCII value ! Why is it not 1? When I change DEC to BYTE, I get 1!

You could have answered yourself the the question why isn't it 1 in 30 seconds by taking a look at the documentation of the Serial.println function, realise on line 2 of the description that you need to click on the link to Serial.print function where in the second paragraph of the description exatly this problem is explained. They just used the letter N instead of 1.

WTH

This is a good question, why didn't you bother to look in the obvious place?

Korman

Thanks zoomkat, your code worked perfectly. (I was looking at your code when I posted my last, hence the confusion with the char and int typing for incomingByte). But something weird happened...I ran zoomkats code - good to go as stated - but I then changed the char to int, uploaded it, and ran it again. It worked! I then added the =0 so I had int incomingByte = 0 instead of int incomingByte. I got the 49 result again. I've printed out a lot of material on C/C++ types...I'll get to the bottom of this.

Serial Monitor gives this result when I type the number 1 in the Send box:

I received: 49

Yahoo - the program works. Sorry, no money back!

The thing is ...... Arduino told you exactly what you said.
And you did say "49" !
down the serial line ...
but it's not a prime ...

If you need Arduino to return the number Uno you have to substract the ASCII value of zero.

-Fletcher

:-[

Korman, why am I such a dumb a$$. ;D

Note to self: No Hennesey while programming.
You guys are great!

I didn't mean to come across as a smart a$$

I don't think there was much danger of that.

yeah, well.............

The problem arises only because you imagine that the program does something else and then wonder, why you don't get the results you expect.

Exactly! :wink:

It probably uses automatic type detection.
Either use the correct type or use a type cast.