Controlling LED Intensity via Serial

Hello everyone, :) i'm here because i'm stuck in one part of programming, does anyone knows how i can control a LED intensity using serial? i want to send values via Serial (000 to 255) and control it via PWM please someone shed me some light

does anyone knows how i can control a LED intensity using serial?

Yes.

i want to send values via Serial (000 to 255)

From what?

and control it via PWM

Well, that's how.

please someone shed me some light

analogWrite(ledPin, 255); // full brightness.

thanks sir but i think you misunderstood me i want to send values via serial using Serial.read() and then send numbers, from 000 to 255 and these numbers have to go directly to the analog.write

i want to send values via serial
using Serial.read()

And read it using Serial.print()?

What is sending the data? How?

What is receiving the data?

thanks man but you complicates things more than you solves it :0 i think you don't want to help me. but anyway thanks

georgebenett: thanks sir but i think you misunderstood me i want to send values via serial using Serial.read() and then send numbers, from 000 to 255 and these numbers have to go directly to the analog.write

Well, you seem to have divided the problem up into logical steps. Which part are you having trouble with?

(Please don't say "all of it" - if you do, I will have to assume you haven't made any serious attempt to do any of this for yourself.)

Do you know how to display the serial monitor in the Arduino IDE and send input to your sketch? Do you know how to read a character from the serial port? Do you know how to perform an analog write to perform PWM output?

All of these things are simple in isolation, and there are example sketches which show you how to do them.

Do you know how to display the serial monitor in the Arduino IDE and send input to your sketch?

yes

Do you know how to read a character from the serial port?

yes

Do you know how to perform an analog write to perform PWM output?

yes

i don't know how to make it works, because when i send an value like 234 and 239 it displays this: 2342 39

here's the code i'm using:

int led = 9;

void setup() {
 Serial.begin(9600);
 pinMode(led, OUTPUT);
}

void loop() {
 char buffer[] = {' ',' ',' ',' '}; // Receive up to 4 bytes
 while (!Serial.available()); // Wait for characters
 Serial.readBytesUntil('n', buffer, 3); //read bytes until n
 int incomingValue = atoi(buffer);
 Serial.println(incomingValue);
 analogWrite(led, incomingValue);
}

George, thing is, all you need to do is send 1 char/byte down the serial line.

1 byte - 0 - FF (hex) and guess what values PWM with analogWrite has, yup 0 - 255 (Dec), so send 1 byte, collect the byte and use it to light the LED with... so forget string formatting if you want to keep it simple.

georgebenett: Serial.readBytesUntil('n', buffer, 3); //read bytes until n

This part caught my eye. Exactly what are you typing into the serial monitor, I mean which keys are you pressing?

I think your response to PaulS was a little 'off the mark'..

sometimes he makes comments.. but I feel his questions in this case were valid.

What are you trying to use to "SEND" the data to the Arduino?

Serial Monitor?

From my un-trained eye.. it seems you do 'not' know how to do the things you answered yes to?

I believe your problem lies with needing a starting and stopping 'flag' (character) to let the Arduino know when a packet is starting and when it ends.. (and when the next packet start is..etc)

here is a thread where PaulS showed me how to fix the same problem when I had it:

http://arduino.cc/forum/index.php/topic,122293.msg927690.html#msg927690

and here is where I took that same knowledge/code and used it to pass PWM values to my Arduino (from PHP/Wamp/LAN as well as serial monitor)

http://arduino.cc/forum/index.php/topic,124394.msg939596.html#msg939596