Sending weird data sequentially

So I'm making a sketch that takes a 2 digit number from the USB port, checks the pin state that matches the number, and then toggles the pin / off.

Look 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

The value that the IDE is sending strange numbers, or the Arduino is not handling them correctly. Do any of you see the problem why this doesn't work?

0


source to share


1 answer


If you enter ASCII characters "1" and then "3", then it Serial.read()

will return 49 and 51. This is because in the ASCII character table "1" and "3" are represented by numbers 49 and 51 respectively. If you want to search the number typed by the user, you must convert it from ASCII .

I am not very familiar with the Arduino language, but assuming it is similar to C, you can find the necessary changes here .



I rewrote the program in a different way, which may be easier to read .

"0", as used in the source, is just another way of saying "the number used to represent the symbol" 0 "," no less than 48. C-like languages '0' == 48

, '1' == 49

etc.

+1


source







All Articles