views:

240

answers:

2

So I'm making a sketch that takes a two digit number from the usb port, checks the state of the pin that matches the number, then toggles the pin on/off.

Take a peek at the source

For some reason, when I send 13 through the Arduino serial monitor, I get this message back: Pin number is greater than 14, details: 490 51 541

Meaning that the IDE is sending weird numbers, or the Arduino is processing them wrong. Do any of you see a problem as to why this isn't working right?

A: 

Is it expecting Hex, and interpreting 13 (base 16) as 19 (base 10) ?

Mitch Wheat
If so, how would I fix this?
Vestonian
Try sending 0D (ie zero D) which is 13 in Hex
Mitch Wheat
I was just looking at this arduino article and I don't think that's your problem...
Mitch Wheat
... http://www.arduino.cc/en/Tutorial/SoftwareSerial
Mitch Wheat
Does your implementation of digitalRead() take into account timing (as per the linked artcile)?
Mitch Wheat
+2  A: 

If you enter the ASCII characters "1" then "3" then Serial.read() will return 49 and 51. This is because in the ASCII character table "1" and "3" are represented by the numbers 49 and 51, respectively. If you want to find the number that the user typed out you have to convert it from ASCII.

I'm not very familiar with the Arduino language, but assuming it's similar to C you can find the changes needed Here.

I rewrote the program in another way, which may be clearer to Read.

The '0' used in the source is simply another way of saying "the number used to represent the character '0'", so is 48. In C-like languages '0' == 48, '1' == 49, etc, etc.

paranoidgeek