Serial communication with monitor

I am trying to send a number from the IDE serial monitor to the Arduino (ultimately to control the speed of a motor), and have figured out a bunch of things:

  • typing in 255 (for example) actually sends 3 bytes of data (plus another if ther is a linefeed)
  • everything sent is interpretted as an ASCII character, so if you want the value you need to subtract 48
  • therefore, you need to do some math to get a variable set to the number you typed in

Here is my question:

When I use Serial.available() to check for data in the buffer and get the number of bytes to loop through for the aforementioned 'assembly' of the number, I always get 1 returned.

Is this because the Arduino is looping faster than the serial transmission (at 9600 baud)? That is, it gets into the loop when the first byte arrives and returns the Serial.available() number before the second byte arrives? I was going to test this by putting in a little delay, but didn't get to it.

Also, is there a good resource describing the speed of various functions on the Arduino? I understand that the clock speed is 16 MHz, but how does that relate to how long it takes to process a line of code?

Is this because the Arduino is looping faster than the serial transmission (at 9600 baud)?

That's a problem many people have, but if you are using Serial.available() correctly it should work.

Repeat after me...SHOW US YOUR CODE :)

is there a good resource describing the speed of various functions on the Arduino?

Not that I know of.


Rob

  • everything sent is interpretted as an ASCII character, so if you want the value you need to subtract 48

No. You need to subtract '0'. While the value of '0' is 48, the '0' makes it MUCH clearer what you are doing.

  • therefore, you need to do some math to get a variable set to the number you typed in

Or collect the characters in an array, keeping the array NULL terminated, and then call atoi() when the end of the string is read (the carriage return/line feed is encountered).

When I use Serial.available() to check for data in the buffer and get the number of bytes to loop through for the aforementioned 'assembly' of the number, I always get 1 returned.

Because serial data transmission is many orders of magnitude slower than the Arduino.

Is this because the Arduino is looping faster than the serial transmission (at 9600 baud)? That is, it gets into the loop when the first byte arrives and returns the Serial.available() number before the second byte arrives? I was going to test this by putting in a little delay, but didn't get to it.

Yes. Don't use delay. You know what character will arrive to indicate that end of the number has occurred. Just read, and process, all characters until that happens. When that happens, use the number.

Also, is there a good resource describing the speed of various functions on the Arduino? I understand that the clock speed is 16 MHz, but how does that relate to how long it takes to process a line of code?

I hope that you aren't planning to use this information to determine how long the delay should be.

For what it is worth here is something like the code I was originally using:

while (Serial.available()) {
//delay(5);
numBytes = Serial.available();
Serial.print (“I got this many bytes:”);
Serial.println(numBytes);
for (int x = 0; x < numBytes; x++) {
inByte = Serial.read();
Serial.print(“Here’s a byte:”);
Serial.println(inByte);

Until I added the delay (which I figured by rough math should be about 3ms - the time it takes to read 3 bytes at 9600 baud), I was only getting 3 separate instances of 1 byte returned.

Having said that, I have read up on character arrays and the atoi() approach seems much more elegant.

Thanks for your help.