Go Down

Topic: How do I link Processing to my arduino board? (Read 433 times) previous topic - next topic

monroehaus

Hello,
I am following a tutorial in chapter 4 of the book 'Arduino and Kinect Projects' by Melger and Diez.

It first walks you through creating a bit of code in the Arduino IDE that "turns an LED on or off depending on the data in the serial buffer." Next it's "time to write a processing sketch that will send a serial '1' message if the mouse is on the right side of the processing frame, and a '0' if it is on the left side. First, import the serial library into your sketch and create an object of the serial class."

The first bit of arduino code looks like this:

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  if (Serial.available()>0) {
    byte input=Serial.read();
    if(input =='1'){
      digitalWrite(13, HIGH);
    }else{
      digitalWrite(13, LOW);
    }
  }
}

The "processing" bit of code looks like this:

import processing.serial.*;
Serial myPort;

void setup()
{size(255,255);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}

void draw() {
  noStroke();
  rect(0,0,width/2,height);
 
  if (mouseX>width/2) {
    myPort.write('1');
  }
  else{
    myPort.write('0');
  }
}

When I upload the arduino code to the arduino board it doesn't give me any errors, seems to work (other sketches work). But then when I run the second code in Processing, it seems to work cause the rectangle pops up, but then it doesn't affect the LED on the arduino when I move my mouse back and forth. Processing doesn't seem to be communicating with the arduino. Am I supposed to do all the code in the arduino IDE? Or am I using Processing wrong?

In the attached image, arduino is on the left, processing is on the right.

What am I missing? 

Osgeld

you have 2 programs talking to eachother over the serial port, so the issue is in one or the other (maybe both)

you can use something like

http://www.serial-port-monitor.com/

to see the two devices talking and what is said (free version is pretty basic but works for most tasks)

monroehaus

Are you suggesting that I make sure both programs are connected to the same port? Arduino is connected to COM4, but I can't find a way to check or set the port for the processing program.

Osgeld

well the arduino is talking on com4, the processing app needs to be listening on com4 as well

but also you can watch the two talk to each other to see if they are speaking the same language, it wont work if processing is sending A and arduino is expecting B

monroehaus


Go Up