We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i have been trying to make a hand motion controller to play a game. (which i basically simulate key press using Robot). but the sensor readings are not coming through to the processing all the time ill post my codes below Arduino code:
` int pinX=A6; int pinY=A7; int pinZ=A8; int pinA0=A0; int pinA4=A4; int pinA3=A3; int pinA1=A1; int pinA2=A2;
void setup()
{
Serial.begin(9600); // starts the serial communication
}
void loop()
{
int valX=analogRead(pinX); // reads the Analog Input, t.e the value from the X - axis from the accelerometer
Serial.print(valX); // sends that value into the Serial Port
Serial.print(","); // sends addition character right next to the read value needed later in the Processing IDE for indexing
int valY=analogRead(pinY);
Serial.print(valY);
Serial.print("/");
int valZ=analogRead(pinZ);
Serial.print(valZ);
Serial.print(";");
int valA0=analogRead(pinA0);
Serial.print(valA0);
Serial.print(":");
int valA4=analogRead(pinA4);
Serial.print(valA4);
Serial.print("<");
int valA3=analogRead(pinA3);
Serial.print(valA3);
Serial.print("!");
int valA2=analogRead(pinA2);
Serial.print(valA2);
Serial.print("?");
int valA1=analogRead(pinA1);
Serial.print(valA1);
Serial.print(".");
delay(200);
}`
Processing Code:
` ![](
import processing.serial.*;
import java.awt.Robot; // imports library for key press or release simulation
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;
Serial port; // defines Object Seria
Robot robot; // defines Object Robot
//defining variables
String A0= "";
String A1= "";
String A2= "";
String A3= "";
String A4= "";
String X= "";
String Y= "";
String Z= "";
String data= "";
int index=0;
int index2=0;
int index3=0;
int index4=0;
int index5=0;
int index6=0;
int index7=0;
int iX=0;
int iY=0;
int iZ=0;
int iA0=0;
int iA1=0;
int iA2=0;
int iA3=0;
int iA4=0;
// creates new robot object
void setup()
{
try
{
robot = new Robot();
}
catch (Exception e) {
e.printStackTrace();
exit();
}
delay(1);
size (200,200);
port = new Serial(this,"COM7",9600);
port.bufferUntil('.');
}
void draw()
{
background(0,0,0);
fill(255, 255, 255);
//Simulating key press or release
// turn left
// turn right
// turn up
// accelerate - indexFinger
if( iA4<493)
{
robot.keyPress(KeyEvent.VK_W);
}
if(iA4>=493){
robot.keyRelease(KeyEvent.VK_W);
}
// handbrake - thumbFinger
if( iA0<-1 )
{
robot.keyPress(KeyEvent.VK_SPACE);
}
if(iA0>=529){
robot.keyRelease(KeyEvent.VK_SPACE);
}
// reverse - middleFinger
if( iA3<477 )
{
robot.keyPress(KeyEvent.VK_S);
}
if(iA3>=477){
robot.keyRelease(KeyEvent.VK_S);
}
// shift up - ringFinger
if( iA2<467 )
{
robot.keyPress(KeyEvent.VK_R);
}
if(iA2>=467){
robot.keyRelease(KeyEvent.VK_R);
}
// shift down - littleFinger
if( iA1<461)
{
robot.keyPress(KeyEvent.VK_F);
}
if(iA1>=461){
robot.keyRelease(KeyEvent.VK_F);
}
println("A1:" + iA1 );
println("A2:" + iA2 );
println("A3:" + iA3 );
println("A0:" + iA0 );
println("A4:" + iA4 );
println("data" + data);
println(port.read());
}
// Reading data from the Serial Port
void serialEvent (Serial port) // starts reading data from the Serial Port
{
data = port.readStringUntil('.');
data = data.substring(0,data.length()-1);
index4 = data.indexOf(":");
A0= data.substring(0,index4 );
index5 = data.indexOf("<");
A4= data.substring(index4+1, index5);
index6 = data.indexOf("!");
A3= data.substring(index5+1, index6);
index7 = data.indexOf("?");
A2= data.substring(index6+1, index7);
A1= data.substring(index7+1, data.length());
// Converting the String variables values into Integer values needed for the if statements above
iA0= int(A0);
iA4= int(A4);
iA1= int(A1);
iA2= int(A2);
iA3= int(A3);
iX= int(X);
iY= int(Y);
iZ= int(Z);
}`
)
i used the Print function and the outputs should be data 490,513/493;593:565<560!541?526 but its printing out data3413?530 or stuff similar to this
can anyone help me? a bit time sensitive project for School
Answers
Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.
Kf
Hi Lizard94, The format is tricky to read, but anyway... I would move the prints, lines 121..127 to the end of the SerialEvent function then you'll get 1 set of prints per Ard message (=per Ard loop). Until it's right make the loop delay to e.g 2000 so you can see everything. For the prints I would make it easier to see with print("A3:" + iA3 + " "); and put println(""); after the last.
Your draw will be running at 30 fps, but the Ard messages are about 5 Hz, and asynchronous compared with Processing. I don't know what you want but I'd use a boolean set in SerialEvent so that the draw action acts when the bool is set and then clears it.
Simplify: On e.g. line 98 change 'if(iA3>=477)' to 'else' to avoid duplicate 477. I think you can/should move the 9 String declarations inside SerialEvent.
Can't say all that will fix it, but I think you'll see better what's happening.