Hi,
This my first post so I dont know if i'm in the right section. Im completely new to arduino and I know very little about programming. So here is my problem, I'm learning how to communicate from arduino to processing and vice versa so I copy sketches off that book. Here's my sketchs:
Processing sketch
import processing.serial.*;
Serial myPort;
public static final char HEADER = 'H';
public static final char MOUSE_TAG = 'H';
void setup()
{
size(512, 512);
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
}
void draw()
{}
void serialEvent(Serial p)
{
String inString = myPort.readStringUntil('\n');
if(inString !=null)
{
print( inString);
}
}
void mousePressed()
{
sendMessage(MOUSE_TAG, mouseX, mouseY);
}
void sendMessage(char tag, int x, int y)
{
myPort.write(HEADER);
myPort.write(tag);
myPort.write((char)(x/256));
myPort.write(x & 0xff);
myPort.write((char)(y/256));
myPort.write(y & 0xff);
}
Arduino sketch
const char HEADER = 'H';
const char MOUSE_TAG = 'M';
const int TOTAL_BYTES = 6 ;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() >= TOTAL_BYTES)
{
if (Serial.read() == HEADER)
{
char tag = Serial.read();
if(tag == MOUSE_TAG)
{
int x = Serial.read() * 256;
x = x + Serial.read();
int y = Serial.read() * 256;
y = y + Serial.read();
Serial.print("Received mouse msg, x= ");
Serial.print(x);
Serial.print(", y = ");
Serial.println(y);
}
else
{
Serial.print("got messgae with unknown tag ");
Serial.write(tag);
}
}
}
}
The goal of this sketch is to have a processing window and it sends the position of the mouse as I click to my Arduino (UNO). The picture attached shows what I got instead of the position and the message thats supposed to appear.
PS: I'm a mac user (mavericks 1.9.4), I also had some trouble finding the right port and I got the error message " port already in use " on my arduino
Thanks in advance for the help

