Making an LED blink with Serial.read()

int atoi ( const char * str );.

:o

Okay, to add a new twist to this puzzle: I added the following line to my code:

Serial.write(Serial.read());

Now on one computer I can see what is being sent via the serial communication. It turns out specific characters make it across, but others do not. The only letters that make it are the letters "H, I, J, K, L, M, N, and O." Everything else comes across as gibberish. It doesn't make a difference whether I send capital or lower case letters. No numbers correctly appear.

Can anyone offer an explanation as to why these characters transmit correctly but no others?

Serial.write() sends bytes that the Serial Monitor (if that is what you are using to display the serial data) interprets as characters.

You would have better luck using Serial.print(), which assumes, unless explicitly told otherwise, that you want to send the data as a string (which can be just one character long).

Okay, now I'm getting closer, but still confused.

When I use Serial.print(), I get the following:

Sent Received
1 254
4 254
H 72 (this looks like the decimal equivalent)
k 107 (same thing)
7 254
8 200
9 201
23 250

What's going on here? Could it be the way the Arduino Serial Monitor is sending the letters? I could use Python instead, but it's considerably more work.

PROBLEM SOLVED!

I finally figured it out. It was a combination of incorrect baud rates in the Arduino program, as well as incorrect baud rates set on the Xbee modules. The final code used to decode the serial statement was as follows:

#include <LED.h>

LED led = LED(13);   // the pin that the LED is attached to

void setup() {
  Serial.begin(19200);
  led.blink(250, 2);}  // initialize serial communication:

void loop()          
{
  int numMails = 0;

  delay(2000);
  
  switch (Serial.available()) {
    /* Serial.available() is the number of bytes waiting.  Convert from 
     * ASCII val to an int.  Intentional switch-case fall through below. 
     */
    case 3:
      numMails = Serial.read();
      numMails -= 48;
      numMails *= 10;
    case 2:
      numMails += Serial.read();
      numMails -= 48;
      numMails *= 10;
    case 1:
      numMails += Serial.read();
      numMails -= 48;
      break;
    default:
      /* If >3 chars, just clear out the incoming buffer. */
      while (Serial.available())
        Serial.read();
      return;

  }
      led.blink(400, numMails);
}
numMails -= 48;

3 months from now, will you remember why you are subtracting 48 from numMails?

If you changed this line to

numMails -= '0';

the same value is subtracted, but it's a lot easier to see WHY the subtraction is being done.

Anyway, good job on persevering and solving the problem. I'm sure you have a much better understanding of how integer to string conversions are performed, and how to reverse the conversion.