serial coms issues (not getting back what i expect)

so i've started screwing around with sending serial messages to my arduino uno via the serial monitor with the intention that i ll be able to replace the serial monitor with another atmega 328 down the track. so after a bit of reading i wrote what i intended to be a sketch which i send a serial number to press a button and it spits it back at you through the serial monitor.

here is my code

void setup()
{
  Serial.begin(9600);
  pinMode(12, INPUT);
}

void loop()
{
  while(digitalRead(12)==HIGH);
  while(digitalRead(12)==LOW);
  Serial.println(Serial.read());
}

what happens is i send a number and press the button, the serial monitor then returns a number between 48 and 51 then if i press the button again i'll get either a -1 (no data in buffer) or a number between 48-51 this continues until it goes to -1 and stays there until i enter another number into the serial monitor. it seems to be the bigger the number the more times it will return a number in response to a button press before going back to -1 but it always retuns to -1 not that that is an issue but the fact that it's not returning the number i send is confusing. this is based on the testing i have done so far. i know the switch is operating fine its the program. what im wondering is what is going on and why 1 doesn't return 1 etc.

Hi H-dawg

Serial.println(Serial.read());

Serial.read() returns a binary number representing the ASCII code of the next character received. The ASCII code for the character '1' is decimal 49.

Serial.println() prints a number as a string of digits. For example, Serial.println(49) would display "49" on the screen.

If you want to display the character received, use Serial.write() instead. For example, Serial.write(49) would display "1" on the screen.

The value of -1 means that Serial.read() was called but no character had been received.

Regards

Ray

what happens is i send a number and press the button

You don't check that there IS anything to read before reading. Stupid.

The Serial.read() function returns an int, so that it has some way to tell you that you read crap. The real data in the value returned is a char or byte (depending on what is sending data). If you paid attention to whether there was data to read, you could store the character read in a char variable, and print the variable's value. Then, you'd get a char ('1' instead of 49).

ok, so in the context of using this to make 2 arduinos talk to each other in the future how does this relate there? i want to ba able to send information between the 2 which will probably be decimal numbers and maybe text how does all that factor together? if im reading it as an ascii number how do i fix that? do i need to convert back? can the arduino do that by itself? or do i need to give it a formula type thing?

is this the sort of thing PaulS is talking about?

char [variable name] = Serial.read;

and then do all my operations on the variable. what about the fact that big numbers returned multiple ascii numbers when i kept pressing the button? how do they combine back together again?

is this the sort of thing PaulS is talking about?

Except without the square brackets...

what about the fact that big numbers returned multiple ascii numbers when i kept pressing the button?

Collect the serial data in an array.

You need to send some kind of end-of-packet marker to indicate when a stream of data has reached a logical conclusion.

You'llnotethatIusedsuchdelimitersabove,andthatthatdatawasfareasiertocomprehend.

Ok that all makes sense but what if I have a number bigger then then the le gth of an ascii byte? Like a 2 digit number? How do I treat that as 1 number? Or is that where deliniators come in? Say I send "15" i'll recieve 49 and 53 right? But thats 2 seperate numbers. Or because its 15 and not 1,5 it'll go into the array as 4953? And become 15?

Say I send "15" i'll recieve 49 and 53 right?

Or '1' and '5', depending on the type of variable you store the values in.

But thats 2 seperate numbers.

Yep. But, and array can hold more than one value. And, if you know anything about strings (NULL terminated arrays of chars) and atoi(), you can convert the array contents back to the value 15. If not, hit the books.

Ok, sweet! Thanks for that, it'll get me tinkering for a while until I hit another brick wal. Haha there's so many different commands etc. Its hard/impossible? To cover them all...

i was thinking about this and i have another question.

how come if i was to use say Serial.print(millis()); i would get the actual millis on the port? instead of the ascii value?

The print() method formats numbers into strings of characters that represent that number written out in (by default) decimal. So the number 65 decimal is displayed as two characters: 6 and 5.

The write() method displays the single character that relates to the ASCII code passed to the method. So the number 65 decimal is displayed as 'A'.