Dimmer tutorial problem

I could not get the Dimmer to work as written. The only way I could get it to work is to add a Serial.write command and open the serial monitor. The modified code follows:

/* Dimmer

Demonstrates the sending data from the computer to the Arduino board, in this case to control the brightness of an LED. The data is sent in individual bytes, each of which ranges from 0 to 255. Arduino reads these bytes and uses them to set the brightness of the LED.

The circuit: LED attached from digital pin 9 to ground. Serial connection to Processing, Max/MSP, or another serial application

created 2006 by David A. Mellis modified 30 Aug 2011 by Tom Igoe and Scott Fitzgerald

This example code is in the public domain.



const int ledPin = 9; // the pin that the LED is attached to

void setup() { // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT); analogWrite(ledPin, 255); delay(1000); analogWrite(ledPin, 0); }

void loop() { byte brightness; int dummy;

// check if data has been sent from the computer: if (Serial.available()) { // read the most recent byte (which will be from 0 to 255): brightness = Serial.read(); Serial.write (ledPin); //Serial.print (brightness); // set the brightness of the LED: analogWrite(ledPin, brightness); //Serial.print(brightness); } }

With this modification, it works fine, but there must be a better way to solve the problem. There appears to be some kind of timing problem that I have been unable to otherwise resolve. I am using a Mac, and all serial port users are set to 9600 baud.

Any ideas on 1) source of problem, and 2) solutions are welcome.

Serial.write (ledPin);

This line is correct code but it makes no sense. What is to be achieved by printing the pin number?

Please go back and modify your Post to put the code into code tags (the # button) so it looks like the above.

The example works properly for me without any modifications. I have just tested it by entering single characters from the Serial Monitor.


Post your code for both your arduino program and your processing program. (assuming you are following the example and using processing or similar).