Go Down

Topic: MAking mouse move using Pots (Read 1 time) previous topic - next topic

rohit7gupta

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!!!

Msquare

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 :)

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

rohit7gupta

Thanks for the reply...
Im using pots on arduino as analog inputs..
the code is
Code: [Select]

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("|");
}

fkeel


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: http://download.oracle.com/javase/1.3/docs/api/java/awt/Robot.html

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.
http://embodimentlabs.tumblr.com/
http://paulstrohmeier.info/

AWOL

Quote
"unexpected token:void".

As MSquare pointed out, it is most likely this:
Code: [Select]
void draw()
{ void serialEvent(Serial myPort)
nested function definition that the compiler is complaining about.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

liudr



  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.

fkeel

#6
Jul 25, 2011, 06:53 pm Last Edit: Jul 25, 2011, 06:57 pm by fkeel Reason: 1
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
http://embodimentlabs.tumblr.com/
http://paulstrohmeier.info/

liudr

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

rohit7gupta

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??

liudr

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?

rohit7gupta

@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...
Code: [Select]

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];
   }
 
   }
    }

Msquare

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)

liudr

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

PaulS

Code: [Select]
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.

fkeel

#14
Jul 26, 2011, 09:50 pm Last Edit: Jul 26, 2011, 09:52 pm by fkeel Reason: 1
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.
http://embodimentlabs.tumblr.com/
http://paulstrohmeier.info/

Go Up