Help with Serial

I have a very simple sketch to read a value from the serial input and set a pin to an analog output value based on the input:

int ledPin = 9;      // select the pin for the LED
byte msg = 0;        // variable to hold data from serial 

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output  
  Serial.begin(9600);
  analogWrite(ledPin,10);
}

void loop() {
  // While data is sent over serial assign it to the msg  
  while (Serial.available() > 0){
    msg=Serial.read(); 
    analogWrite(ledPin, msg);  // turn LED ON
  }
}

In the serial monitor I send a ‘[’, which is a 91 and should result in fairly bright output on the LED I have attached to pin 9 (when I explicitly put in an “analogWrite(ledPin,msg)”, it is pretty bright), but the LED stays completely dark. I do see the RX led on the arduino board flicker, but I’m having no luck on getting the LED on pin 9 to light.

Actually, you’ll see that in setup I call analogWrite(ledPin,10), and the LED does comes on when I upload the sketch. But, the instant I start up the serial monitor, the LED goes dark and won’t light up again no matter what I send in the serial monitor.

I wondered if maybe it had something to do with null-terminated strings, but that doesn’t explain why the light goes off when I start the serial monitor, but haven’t sent anything yet (at least I don’t think it does). I put in a delay in the while loop to see if maybe it was a problem with the LED just going on and then off again too fast to see, but that didn’t seem to be the case.

Does anyone know what my problem is? I would really appreciate any help anyone can offer.

Thank you,
dan

Carriage return / line feed (0x0d 0x0a) would make your LED very dim indeed. Try excluding character codes less than 'space' (0x20).

Or use a proper terminal emulator.

Carriage return / line feed (0x0d 0x0a) would make your LED very dim indeed.

The Serial Monitor does not send carriage return/line feeds.

It doesn't send anything until you hit 'send' - could that be the problem?

just ran your code and it seems to work fine. Is your led connected right? long pin connected to pin9 and other to GND ...

Rob

Thanks everyone for your feedback and suggestions. I'm not sure what the problem is, but it seems like my serial connection just periodically gets into a funk.

When it happens, if I just close the terminal monitor, unplug the Arduino for about 30 seconds, and plug it back in it seems to work fine again (for a while, anyway).

Does anyone know what could cause this kind of behavior? Am I getting out of sync with the UART somehow, or getting it into a bad state that it can't recover from? Could it be the terminal facilities on my laptop (Asus EEEPC running Ubuntu 10.4)?

What seems weird to me is that even when the Arduino becomes un-responsive in the terminal monitor and to the python serial communications library, I can still upload sketches just fine.

Anyway, I guess for now I'll get by with just un-plugging and re-plugging when things break.

Thanks again, dan

What seems weird to me is that even when the Arduino becomes un-responsive in the terminal monitor and to the python serial communications library, I can still upload sketches just fine.

So what does python have to do with your setup? Are you using the the arduino serial monitor to test or are you using a python program?

I wrote a python program to drive the display in the project I'm putting together. I'm using the terminal monitor to test and debug the Arduino code. When things are working OK, both work fine, when it freezes up, neither work.