LED Dimmer by receiving a one off serial value (solved)

Hello,

I am fairly new to the arduino enviroment and would really appreceate some help XD

Basically I have modified the PhysicalPixel example in attempt to get it to change the bightness of an LED when a value 1 to 255 is sent to the arduino through serial monitor, which will eventually be a VB program. However when I send a value through the serial monitor the led lights up to around 1/2 brightness regardless of the value and stays the same brightness dispite new values being sent to the arduino :(

I know I'm doing somthing wrong, prehaps my method of thinking?

Here is the code:

const int ledPin = 9; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // Write value 1-255 to LED (controlling brightness):
    analogWrite(ledPin, incomingByte);
    }
  }

Could anyone point me in the right direction to help me acheive what I intended?

Thanks in advance!

Basically I have modified the PhysicalPixel example in attempt to get it to change the bightness of an LED when a value 1 to 255 is sent to the arduino through serial monitor, which will eventually be a VB program. However when I send a value through the serial monitor the led lights up to around 1/2 brightness regardless of the value and stays the same brightness dispite new values being sent to the arduino

When you type a value like 69 in the serial monitor, and press send, what gets sent to the Arduino is '6', '9', and maybe, depending on settings, '\n' and/or '\r'.

You are reading each character, assuming it represents the value that you entered in the serial monitor. It does not. Try printing out the character you read, in ASCII (the default) and in HEX.

incomingByte should be char, not int, since you are testing that there is serial data to read before reading it.

When you receive data from the serial port it is in ASCII, so if you type in 123 what actually gets sent is 0x31, 0x32, 0x33, 0x0d. The last one is a carriage return You need to gather these up and get a value. This link shows you how to do it:- http://arduino.cc/en/Tutorial/ReadASCIIString

Grumpy_Mike: When you receive data from the serial port it is in ASCII, so if you type in 123 what actually gets sent is 0x31, 0x32, 0x33, 0x0d. The last one is a carriage return You need to gather these up and get a value. This link shows you how to do it:- http://arduino.cc/en/Tutorial/ReadASCIIString

Thanks a bunch! This link solved my problem! :D