我在模拟按键时遇到麻烦,这意味着当我运行处理代码时,会自动模拟按键,但是我无法控制使用柔性传感器和加速度计。它定义了使用手套在PC上玩游戏的目的。同样,我得到这个错误:
Error, disabling serialEvent() for COM5
null
源代码:
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
// Defining variables
int pinA0=A0;
int pinA1=A1;
int pinA2=A2;
int pinA3=A3;
int pinA4=A4;
void setup()
{
Serial.begin(115200); // starts the serial communication
/* Initialise the sensor */
if(!accel.begin())
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL345_RANGE_16_G);
// displaySetRange(ADXL345_RANGE_8_G);
// displaySetRange(ADXL345_RANGE_4_G);
// displaySetRange(ADXL345_RANGE_6_G);
}
void loop()
{
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: "); Serial.print(event.acceleration.x);Serial.print(",");
Serial.print("Y: "); Serial.print(event.acceleration.y);Serial.print("/");
Serial.print("Z: "); Serial.print(event.acceleration.z);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(1000);
}
处理代码3.0:
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 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(2000);
size (800, 800);
port = new Serial(this,"COM3", 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
// turn left
if(iX>320)
{
delay(40);
robot.keyPress(KeyEvent.VK_J); // Simulates "I" key press if the value from the accelerometer for the X axis is greater than 320
}
if(iX<=320){
delay(40);
robot.keyRelease(KeyEvent.VK_J); // Simulates "I" key release if the value from the accelerometer for the X axis is less than 320
}
// turn right
if( iX<280 )
{
delay(40);
robot.keyPress(KeyEvent.VK_L);
}
if(iX>=280){
delay(40);
robot.keyRelease(KeyEvent.VK_L);
}
// turn up
if(iY>320)
{
delay(40);
robot.keyPress(KeyEvent.VK_I);
}
if(iY<=320){
delay(40);
robot.keyRelease(KeyEvent.VK_I);
}
// turn down
if( iY<280 )
{
delay(40);
robot.keyPress(KeyEvent.VK_K);
}
if(iY>=280){
delay(40);
robot.keyRelease(KeyEvent.VK_K);
}
// accelerate - indexFinger
if( iA4<510 )
{
delay(40);
robot.keyPress(KeyEvent.VK_W);
}
if(iA4>=510){
robot.keyRelease(KeyEvent.VK_W);
}
// handbrake - thumbFinger
if( iA0<500 )
{
robot.keyPress(KeyEvent.VK_SPACE);
}
if(iA0>=500){
robot.keyRelease(KeyEvent.VK_SPACE);
}
// reverse - middleFinger
if( iA3<560 )
{
robot.keyPress(KeyEvent.VK_S);
}
if(iA3>=560){
robot.keyRelease(KeyEvent.VK_S);
}
// shift up - ringFinger
if( iA2<400 )
{
robot.keyPress(KeyEvent.VK_R);
}
if(iA2>=400){
robot.keyRelease(KeyEvent.VK_R);
}
// shift down - littleFinger
if( iA1<250 )
{
robot.keyPress(KeyEvent.VK_F);
}
if(iA1>=250){
robot.keyRelease(KeyEvent.VK_F);
}
}
// Reading data from the Serial Port
void serialEvent (Serial port) // starts reading data from the Serial Port
{
data = port.readStringUntil('.'); // reads the data from the serial port up to the character '.' and it sets that into the String variable "data". So actually it reads this: 215,214/141;315:314<316!314?315.
data = data.substring(0,data.length()-1); // it removes the '.' from the previous read. So this will be the String "data" variable: 215,214/141;315:314<316!314?315
// Finding the indexes in the data and setting the variables from the sensors by taking from the String "data" the appropriate values that are between the characters in the "data" String
index = data.indexOf(","); // finds the index of the character "," from the String "data" variable
X= data.substring(0, index); // sets into the variable X the string from position 0 of the hole string to where the index was. That would mean that read will be : 215
index2 = data.indexOf("/"); // finds the index of the character "/" from the String "data" variable
Y= data.substring(index+1, index2); // sets into the variable Y data the string from position where the character "," was +1, to where the index2 was. That would mean that the read will be: 214
// We keep reading this way and that's how we get only the numbers, the values from the sensors coming from the serial port.
index3 = data.indexOf(";");
Z= data.substring(index2+1, index3);
index4 = data.indexOf(":");
A0= data.substring(index3+1, 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
iX= int(X);
iY= int(Y);
iZ= int(Z);
iA0= int(A0);
iA4= int(A4);
iA1= int(A1);
iA2= int(A2);
iA3= int(A3);
}