Help with Serial


i have simple sketch read value serial input , set pin analog output value based on input:

int ledpin = 9;      // select pin led
byte msg = 0;        // variable hold data serial

void setup() {
 pinmode(ledpin, output);      // declare led output  
 serial.begin(9600);
 analogwrite(ledpin,10);
}

void loop() {
 // while data sent on serial assign msg  
 while (serial.available() > 0){
   msg=serial.read();
   analogwrite(ledpin, msg);  // turn led on
 }
}


in serial monitor send '[', 91 , should result in bright output on led have attached pin 9 (when explicitly put in "analogwrite(ledpin,msg)", pretty bright), led stays dark.  i see rx led on arduino board flicker, i'm having no luck on getting led on pin 9 light.

actually, you'll see in setup call analogwrite(ledpin,10), , led comes on when upload sketch.  but, instant start serial monitor, led goes dark , won't light again no matter send in serial monitor.

i wondered if maybe had null-terminated strings, doesn't explain why light goes off when start serial monitor, haven't sent yet (at least don't think does).  i put in delay in while loop see if maybe problem led going on , off again fast see, didn't seem case.

does know problem is?  i appreciate can offer.  

thank you,
dan

carriage return / line feed (0x0d 0x0a) make led dim indeed.
try excluding character codes less 'space' (0x20).

or use proper terminal emulator.


Arduino Forum > Forum 2005-2010 (read only) > Software > Interfacing > Help with Serial


arduino

Comments