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
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.
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);
}
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]
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.
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());
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.
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?
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);
}