Birghtness value on LED from Serial

Im trying to change the brightness on my LED. The brightness is changed by what ever value between 0 and 255 that I manually send to the serial monitor. Therefore I'm using PWM. I got some problems though. The brightness doesn't change. And the values that are printed in the serial monitor is different from what I send to it. Can anyone help me on what I am doing wrong?

 int brightness = 0; 

 const int ledPin = 9;

 void setup() { 
   pinMode(ledPin, OUTPUT); 
   digitalWrite(ledPin, LOW);

  Serial.begin(9600); 
 }

 void loop() {
   if (Serial.available() > 0){
     brightness = Serial.read();
     Serial.println(brightness); 

   }
   analogWrite(ledPin, brightness); 
 }

I read something about the serial communication. Serial.read() returnes a byte meaning sending 1 would return 49. How ever I don't know how to fix the code?

Serial.read() can read only a single byte of data.

Try this:

untested…

int brightness = 0; 

const int ledPin = 9;

void setup() 
{ 
  pinMode(ledPin, OUTPUT); 
  digitalWrite(ledPin, LOW);
  Serial.begin(9600); 
}

void loop() 
{
  if (Serial.available() > 0)
  {
    brightness = Serial.parseInt();
    Serial.println(brightness); 
  }
  analogWrite(ledPin, brightness); 
}

Serial.read() returnes a byte meaning sending 1 would return 49. How ever I don't know how to fix the code?

Pretty sure I read some where just to subtract 49? Or probably 48....

BulldogLowell: Serial.read() can read only a single byte of data.

Try this:

untested...

int brightness = 0; 

const int ledPin = 9;

void setup() {   pinMode(ledPin, OUTPUT);   digitalWrite(ledPin, LOW);   Serial.begin(9600); }

void loop() {   if (Serial.available() > 0)   {     brightness = Serial.parseInt();     Serial.println(brightness);   }   analogWrite(ledPin, brightness); }

This works great! Thanks. One question: There is a short delay on like 0.5-1 second before the value I send to the monitor is printed and the LED reacts. Can this delay be removed somehow?

yes.

there is a default one second (1000ms) timeout on the Serial.parseInt() function… Serial.setTimeout() changes that.

Put this in your setup:

Serial.setTimeout(50);

where you can manipulate the 50 to make sure you allow enough time to read your number(s).

don’t forget to add error checking on the number:

if (brightness>255) brightness = 255;

for example…

Wasd9595: This works great! Thanks. One question: There is a short delay on like 0.5-1 second before the value I send to the monitor is printed and the LED reacts. Can this delay be removed somehow?

Send a non-numeric value, letting parseInt() know that you're done sending the number.

Pretty sure I read some where just to subtract 49? Or probably 48....

Or you can subtract '0', and not have to keep an ASCII table on standby whenever you work on the code.

Wasd9595: This works great! Thanks. One question: There is a short delay on like 0.5-1 second before the value I send to the monitor is printed and the LED reacts. Can this delay be removed somehow?

Send a non-numeric value, letting parseInt() know that you're done sending the number.