MAking mouse move using Pots

i have written the following code in processing
import processing.serial.*;
Serial myPort;
int valX,valY;

void setup()
{ background(0);
size(1024,1024);
myPort=new Serial(this,Serial.list()[0],9600);
}
void draw()
{ void serialEvent(Serial myPort)
{
String myString=myPort.readStringUntil(124);
if(myString!=null)
{
myString=trim(myString);
int inputs[]=int(split(myString,','));
if(inputs.length==2)
{ valX=inputs[0];
valY=inputs[1];
}

}
}
mouseX=valX;
mouseY=valY;
fill(225);
ellipse(mouseX,mouseY,2,2);
if(mousePressed)
{background(0);
}
}

Its giving me an error while declaring the serialEvent as "unexpected token:void".
processing.app.SketchException: unexpected token: void
at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:326)
at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:197)
at processing.mode.java.JavaBuild.build(JavaBuild.java:156)
at processing.mode.java.JavaBuild.build(JavaBuild.java:135)
at processing.mode.java.JavaMode.handleRun(JavaMode.java:176)
at processing.mode.java.JavaEditor$20.run(JavaEditor.java:481)
at java.lang.Thread.run(Thread.java:662)

wats the error?I am still not able to figure out...Please help!!!

Interesting ... and where does the Arduino come into this? (drives the servo, yes)

May I suggest the Processing forum ( http://forum.processing.org )? Remember that Processing is based on Java, the Arduino is based on C++ - similar yet different :slight_smile:

Lastly, the probable error is that you are nesting function defenitions.

Thanks for the reply...
Im using pots on arduino as analog inputs..
the code is

int potX=A0;
int potY=A1;
int valX,valY;
void setup()
{ Serial.begin(9600);
}
void loop()
{
  valX=analogRead(potX);
  valY=analogRead(potY);
  Serial.print(valX,DEC);
  Serial.print(",");
  Serial.print(valY,DEC);
  Serial.print("|");
}

mouseX and mouseY are used to read the mouses position. you cannot assign a value to them (or maybe you can, I dont know, but they will never move the curser).

take a look at this link: JDK 20 Documentation - Home

I have done something similar in the past using robot class.

if you cant make it work, feel free to ask more questions, I can digg up my old code somewhere and take a look at how I did it.

p.

"unexpected token:void".

As MSquare pointed out, it is most likely this:

void draw()
{ void serialEvent(Serial myPort)

nested function definition that the compiler is complaining about.

rohit7gupta:
myPort=new Serial(this,Serial.list()[0],9600);

How sure are you that arduino is the first serial port on that list? I'm sure mine is the last on the list. You really should have asked on Processing forum for this code.

but the processing forum is horrible to navigate and has an awkward layout and I personally dont like it at all ... and it is an arduino project, so let the guy ask his questions.

and especially (I didnt look at the code beyond the mouseX/mouseY issue) if the actual problem he has is in the setup of the serial communication

edit: with me it always is the first serial port

Serial port number may depend on operating system. On my windows machines they are always last.

As some of the friends suggested it "was" the problem of nested functions ..i have corrected the same..
The serial communication is no problem as i had done many serial projects on arduino before..but i sent or received only one value...but this time i was experimenting with two values...
I have configured on one serial port on my laptop that is com7 so it always comes first in serial.list() at an index[0]...
Friends the problem i am now facing is that the mousex works but mouseY doesnt respond...
And lastly.. as some suggested that i should try the processing forum..i did it and i did it in the past too..but i get no replies and on this forum i get reply within a few minutes..

Thanks..
CAN U TELL ME HOW to MAKE mouseY work??

So what did you use in the robot class, this one?

mouseMove(int x, int y)
Moves mouse pointer to given screen coordinates.

Can you repost your codes?

@liudr
the new code is this..but still the mouseY is not working and in the output window the value of mouseY is always 0..I didnt use the robotclass...

import processing.serial.*;
Serial myPort;
int valX,valY;

void setup()
{ background(0);
  size(1024,1024);
  myPort=new Serial(this,Serial.list()[0],9600);
}
void draw()
{ serialEvent(myPort);
    mouseX=valX;
    mouseY=valY;
    fill(225);
    ellipse(mouseX,mouseY,20,20);
   if(mousePressed)
  {background(0);
  }
}
void serialEvent(Serial myPort)
    { 
      String myString=myPort.readStringUntil(124);
      if(myString!=null)
   { 
     myString=trim(myString);
     int inputs[]=int(split(myString,','));
     if(inputs.length==2)
   { valX=inputs[0];
    valY=inputs[1];
   }
  
   }
    }

if mouseX works, but mouseY does not ... presumes that the communication is working.

So, have you confirmed that potY is actually sending other values than 0 ? Or the string actually has the format you expect 2142,3453|

Try putting a print(myString) in. (or is it myString.print - my Processing skills are rusty)

As Msquare said your poty may not work. Can we see your arduino code and picture of your setup?

void draw()
{ serialEvent(myPort);
    mouseX=valX;
    mouseY=valY;
    fill(225);
    ellipse(mouseX,mouseY,20,20);
   if(mousePressed)
  {background(0);
  }
}

The serialEvent() function is called whenever there is serial data for you to read. You should NEVER call serialEvent() directly.

I have already explained to you. mouseX and mouseY are special variables reserved for reading the coordinates of your curser.

You are not interested in reading where your curser is. As far as I can tell, you are trying to move an ellipse within a processing sketch. Do not use mouseX and mouseY as variables for doing this, as it is bound to lead to problems.

This might have already been sorted....

void draw()
{ serialEvent(myPort);
    fill(225);
    ellipse(valX,valY,20,20);
   if(mousePressed)
  {background(0);
  }
}

You don't need to use mouseX or mouseY at all
just bung the interpreted serial data straight into drawing the ellipse.
MouseX and mouseY just map the position of the cursor within the sketch window.
You would use it the other way round normally, ie. using the mouse data for changing the background color (crap example).