Hi,
I am trying to make a game controller using Arduino mega and ADXL 335 & linear potentiometer. The controller works fine but most of the time it gives the error
Error, disabling serialEvent() for COM11
null
I am attaching my arduino and processing code
**ARDUINO CODE**
/*
* Arduino Game Controller
*/
// Defining variables
int pinX=A2;
int pinY=A1;
int pinZ=A0; //accelerometer values
int pinA5=A5;
int pinA6=A6;
int pinA7=A7; //potentiometer
//int pinA2=A2;
void setup()
{
Serial.begin(115200); // 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
delay(30);
int valY=analogRead(pinY);
Serial.print(valY);
Serial.print("/");
int valZ=analogRead(pinZ);
Serial.print(valZ);
Serial.print(";");
//
int valA5=analogRead(pinA5);
Serial.print(valA5);
Serial.print(":");
int valA6=analogRead(pinA6);
Serial.print(valA6);
Serial.print("<");
int valA7=analogRead(pinA7);
Serial.print(valA7);
Serial.print("!");
delay(30);
}
PROCESSING CODE
/*
Controller
*/
import processing.serial.*; // imports library for serial communication
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 Serial
Robot robot; // defines Object Robot
//defining variables
String X= "";
String Y= "";
String Z= "";
String A0= "";
String A1= "";
String A2= "";
String A3= "";
String A4= "";
String data= "";
int index=0;
int index2=0;
int index3=0;
int index4=0;
int index5=0;
int index6=0;
int iX=0;
int iY=0;
int iZ=0;
int iA0=0;
int iA1=0;
int iA2=0;
// creates new robot object
void setup()
{
try
{
robot = new Robot();
}
catch (Exception e) {
e.printStackTrace();
exit();
}
delay(2000);
size (800, 800);
port = new Serial(this,"COM11", 115200); // starts the serial communication
port.bufferUntil('!'); // reads the data from the serial port up to the character '.'. So actually it reads this: 215,214/141;315:314<316!314?315.
}
void draw()
{
background(0,0,0);
fill(255, 255, 255);
//Simulating key press or release
// left
if(iX<325)
{
delay(40);
robot.keyPress(KeyEvent.VK_A);
}
if(iX>=325){
delay(40);
robot.keyRelease(KeyEvent.VK_A);
}
//right
if( iX>345 )
{
delay(40);
robot.keyPress(KeyEvent.VK_D);
}
if(iX<=345){
delay(40);
robot.keyRelease(KeyEvent.VK_D);
}
// up
if(iY>360)
{
delay(40);
robot.keyPress(KeyEvent.VK_W);
}
if(iY<=360){
delay(40);
robot.keyRelease(KeyEvent.VK_W);
}
// down
if( iY<330 )
{
delay(40);
robot.keyPress(KeyEvent.VK_S);
}
if(iY>=330){
delay(40);
robot.keyRelease(KeyEvent.VK_S);
}
// indexFinger punch
if( iA0<700 )
{
delay(40);
robot.keyPress(KeyEvent.VK_I);
}
if(iA0>=700){
robot.keyRelease(KeyEvent.VK_I);
}
// PINKIE sit in vehicle
if( iA1<700 )
{
robot.keyPress(KeyEvent.VK_F);
}
if(iA1>=700){
robot.keyRelease(KeyEvent.VK_F);
}
// middleFinger jump
if( iA2<700 )
{
robot.keyPress(KeyEvent.VK_SPACE);
}
if(iA2>=700){
robot.keyRelease(KeyEvent.VK_SPACE);
}
}
// 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);
index = data.indexOf(",");
X= data.substring(0, index);
index2 = data.indexOf("/");
Y= data.substring(index+1, index2);
index3 = data.indexOf(";");
Z= data.substring(index2+1, index3);
index4 = data.indexOf(":");
A0= data.substring(index3+1, index4);
index5 = data.indexOf("<");
A1= data.substring(index4+1, index5);
index6 = data.indexOf("!");
A2= data.substring(index5+1, data.length());
// Converting the String variables values into Integer values needed for the if statements above
iX= int(X);
iY= int(Y);
iZ= int(Z);
iA0= int(A0);
iA1= int(A1);
iA2= int(A2);
}