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?