"Dimmer" noob question

Hey there
I’m just trying to make my first steps with processing communicating with Arduino and am trying to run the Dimmer example.
What happens is that once communication is established, I can see the RX ked on my Mega constantly on, but no dimming - or in fact no light at all - from the LED I’ve attached to A9.

I’m using Processing 1.5.1 with the corresponding sketch and have changed the serial value here

port = new Serial(this, Serial.list()[0], 9600);

from 0 to 1, to correspond with the arduino.

Any ideas? Is this a common noob error…?

Any ideas? Is this a common noob error...?

The part about making random changes that you don't understand? Yes, I'm afraid that it is. Example or not, you need to post your code.

If you are changing the array index, and that is the right thing to do, some proof of that would be good.

Some mention of the type of Arduino that is receiving would be good. I doubt that the example code suggested using pin A9 for the LED, since most Arduinos don't have a pin A9.

Thanks.
Using Mega 2560

Arduino code:

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);
}

void loop() {
  byte brightness;

  // 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();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
  }
}

Processing code:

 import processing.serial.*;
 Serial port;
 
 void setup() {
 size(256, 150);
 
 println("Available serial ports:");
 println(Serial.list());
 
 // Uses the first port in this list (number 0).  Change this to
 // select the port corresponding to your Arduino board.  The last
 // parameter (e.g. 9600) is the speed of the communication.  It
 // has to correspond to the value passed to Serial.begin() in your
 // Arduino sketch.
 port = new Serial(this, Serial.list()[1], 9600);  
 
 // If you know the name of the port used by the Arduino board, you
 // can specify it directly like this.
 //port = new Serial(this, "COM1", 9600);
 }
 
 void draw() {
 // draw a gradient from black to white
 for (int i = 0; i < 256; i++) {
 stroke(i);
 line(i, 0, i, 150);
 }
 
 // write the current X-position of the mouse to the serial port as
 // a single byte
 port.write(mouseX);
 }

So, you're LED is not attached to A9. Why did you say it was?

 println("Available serial ports:");
 println(Serial.list());

What did this show? Which port is the Arduino actually connected to?

 // write the current X-position of the mouse to the serial port as
 // a single byte
 port.write(mouseX);

Are you sure about this? Unlike the Arduino's HardwareSerial class, Processing does not appear to have binary vs. ASCII methods.

Echo everything that the Arduino gets on the serial port, and have Processing read and print it, to confirm exactly what is sent across.

Is it safe to assume that you have written a simple sketch on the Arduino to confirm that the LED is wired correctly?

you're LED is not attached to A9. Why did you say it was?

I'm used to the UNO and am confused by the MEGA. According to this code I should connect the LED to PWM 9, right? That was my (wrongly stated) meaning.

What did this show? Which port is the Arduino actually connected to?

Com9 which according to the processing feedback is [1]

Are you sure about this?

Yeap. This is taken from the Arduino playground. http://arduino.cc/en/Tutorial/Dimmer

Is it safe to assume that you have written a simple sketch on the Arduino to confirm that the LED is wired correctly?

Indeed I have

I don't know what to tell you. I started Processing and the Arduino IDE. I pasted your two sketches into the appropriate windows. I uploaded the code to the Arduino, and ran the Processing sketch. When I move the mouse back and forth in the little window that Processing posts, my LED gets brighter and dimmer.

I did confirm that the Processing sketch is sending a byte.

First of all thanks for checking this out. This must mean physical connection is wrong. I'm going over everything again. :relaxed:

This is really weird. Still nothing. I have a message stating

"Display 0 does not exist, using the default display instead."

But from reading the various Processing forums this seems redundant...

Fixed the "Display 0 does not exist..." by editing prefernces. Still nothing

PaulS, read one of your comments here http://arduino.cc/forum/index.php/topic,40414.0.html

And so added this in the arduino sketch:

 if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    Serial.print (brightness);  //this was added now to check data w/o openineg serial monitor
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
  }

and this in processing sketch:

 // write the current X-position of the mouse to the serial port as
 // a single byte
 port.write(mouseX);
 println (port.readString());

I get "null" repeatedly.

PaulS, just found this post by you:

I've confirmed that Processing does not talk to the Mega 2560

It's from this discussion :http://arduino.cc/forum/index.php?topic=79341.0 Is this the problem you think?

Just tested the sketches with my Nano - and it works!
Is there no way for Processing to talk to Mega?

Is there no way for Processing to talk to Mega?

I was using a Mega. So, yes, Processing CAN talk to a Mega.

One thing I forgot to ask was which version of Processing you are using. I am using 1.5.1.

I did the same thing to the Arduino sketch, except that I added:

Serial.print("brightness: [");
Serial.print(brightness);
Serial.println("]");

And, I modified the Processing sketch, adding a serialEvent(Serial port) function, that Processing calls when there is sufficient serial data. I got the correct response in the Processing window, though I did have to read it one character at a time, casting to a character, in order to print it.

PaulS:

Is there no way for Processing to talk to Mega?

I was using a Mega. So, yes, Processing CAN talk to a Mega.

One thing I forgot to ask was which version of Processing you are using. I am using 1.5.1.

I did the same thing to the Arduino sketch, except that I added:

Serial.print("brightness: [");
Serial.print(brightness);
Serial.println("]");

And, I modified the Processing sketch, adding a serialEvent(Serial port) function, that Processing calls when there is sufficient serial data. I got the correct response in the Processing window, though I did have to read it one character at a time, casting to a character, in order to print it.

Using 1.5.1 as well. So what else can be going on here? Having a hard time figuring what you mean you added to the processing sketch. Could you post the code?

Thanks again!

Arduino code:

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);
}

void loop()
{
  byte brightness;

  // 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.print("brightness: [");
    Serial.print(brightness);
    Serial.println("]");    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
  }
}

Processing code:

import processing.serial.*;
Serial port;

void setup()
{
  size(256, 150);

  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[1], 9600);  

  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw()
{
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++)
  {
    stroke(i);
    line(i, 0, i, 150);
  }

  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
}

void serialEvent(Serial port)
{
  char c = (char)port.read();
  print(c);
}

Processing output:

Available serial ports:
WARNING:  RXTX Version mismatch
	Jar version = RXTX-2.2pre1
	native lib Version = RXTX-2.2pre2
[0] "COM3"
[1] "COM13"
brightness: [0]
brightness: [0]
brightness: [0]
brightness: [0]
brightness: [0]
brightness: [1]
brightness: [3]
brightness: [5]
brightness: [6]
brightness: [8]
brightness: [10]
brightness: [10]
brightness: [11]
brightness: [12]
brightness: [13]
brightness: [13]
brightness: [13]
brightness: [13]
brightness: [13]
brightness: [13]
brightness: [13]
brightness: [13]

Here's what I get:

Available serial ports:

Stable Library

Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 [0] "COM1" [1] "COM9"

And nothing happens... What is going on here? Why is it that with the Nano it simply works, and with the Mega it's not?

Searching some more I came upon this thread: http://arduino.cc/forum/index.php?action=printpage;topic=52151.0 It seems to be a known issue, and I can't find a published solution.

I'm beginning to think the problem is in the pc side. i.e. something with the com address.