I am currently working on an Arduino project using Arduino IDE and Processing IDE. The project reads sensor values and prints those to the serial port in arduino ide, to then be read by processing IDE to simulate keystrokes on a laptop. However in testing we have observed that the values that are being sent over are being read as 0 in processing ide, instead of the sensor value. Here is my processing code:
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 button1 = "";
String button2 = "";
String sw = "";
String joysticky = "";
String data= "";
int index=0;
int index2=0;
int index3=0;
int index4=0;
int index5=0;
int index6=0;
int index7=0;
int index8=0;
int iX=0;
int iY=0;
int iZ=0;
int iA0=0;
int iA1=0;
int ibutton1;
int ibutton2;
int isw;
int ijoysticky;
// 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,"COM9", 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<1!1?1>315.
}
void draw()
{
background(0,0,0);
fill(255, 255, 255);
//Simulating key press or release
// turn left
if(iX > 300)
{
delay(20);
robot.keyPress(KeyEvent.VK_LEFT); // Simulates "I" key press if the value from the accelerometer for the X axis is greater than 1000
delay(20);
robot.keyRelease(KeyEvent.VK_LEFT);
}
// turn right
if(iX < -1000)
{
delay(20);
robot.keyPress(KeyEvent.VK_RIGHT);
delay(20);
robot.keyRelease(KeyEvent.VK_RIGHT);
}
// gas - indexFinger //
if(iA0 < 100)
{
delay(50);
robot.keyPress(KeyEvent.VK_W);
delay(50);
robot.keyRelease(KeyEvent.VK_W);
}
// break - middleFinger
if(iA1 < 100)
{
delay(50);
robot.keyPress(KeyEvent.VK_S);
delay(50);
robot.keyRelease(KeyEvent.VK_S);
}
//press A
if(ibutton1 == 0)
{
robot.keyPress(KeyEvent.VK_A);
delay(30);
}
if(ibutton1 == 1)
{
robot.keyRelease(KeyEvent.VK_A);
delay(30);
}
delay(75);
}
// 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()); // 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-1); // 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-1); // 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-1);
index4 = data.indexOf(":");
A0 = data.substring(index3+1, index4-1);
index5 = data.indexOf("<");
A1 = data.substring(index4+1, index5-1);
index6 = data.indexOf("!");
button1 = data.substring(index5+1, index6-1);
index7 = data.indexOf("?");
button2 = data.substring(index6+1, index7-1);
index8 = data.indexOf(">");
sw = data.substring(index7+1, index8-1);
joysticky = data.substring(index8+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);
ibutton1 = int(button1);
ibutton2 = int(button2);
isw = int(sw);
ijoysticky = int(joysticky);
}
And here is my arduino code:
#include <Wire.h> //this library allows you to communicate with I2C devices
#include <MPU6050.h>
#include <LiquidCrystal.h> //this library assists in setting up the lcd screen
//Define the Pushbuttons
#define BUTTON1_PIN 6 //We want this one.
#define BUTTON2_PIN 7 //We want this one.
#define BUTTON3_PIN 8
#define BUTTON4_PIN 9
//Define the Joystick Values
#define VRX_PIN A9
#define VRY_PIN A8 //This is the one we want.
#define SW_PIN 52
//Accelerometer Setup Stuff
MPU6050 mpu;
//Settting the finger ints
int pinA0 = A0;
int pinA1 = A1;
//Golly gee, we love initialized ints.
int valA0 = 0;
int valA1 = 0;
int button1val = 0;
int button2val = 0;
int button3val = 0;
int button4val = 0;
int xstick = 0;
int ystick = 0;
int swval = 0;
void setup()
{
Serial.begin(115200); // starts the serial communication
Wire.begin();
mpu.initialize();
//Setup the INPUT buttons using the onboard pullup resistor
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
pinMode(BUTTON4_PIN, INPUT_PULLUP);
pinMode(SW_PIN, INPUT_PULLUP);
}
void loop()
{
//Accelerometer Stuff
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
//Read in the fingies
valA0=analogRead(pinA0);
valA1=analogRead(pinA1);
//read in the digital button values.
button1val=digitalRead(BUTTON1_PIN);
button2val=digitalRead(BUTTON2_PIN);
button3val=digitalRead(BUTTON3_PIN);
button4val=digitalRead(BUTTON4_PIN);
//read the analog and digital values from the joystick.
xstick = analogRead(VRX_PIN);
ystick = analogRead(VRY_PIN);
swval = digitalRead(SW_PIN);
//Setup the serial output to be read by ProcessingIDE.
// X,Y/Z;IndexFinger:MiddleFinger<button1!button2?SWval>JoystickY.
int valX=analogRead(ax); // 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(ay);
Serial.print(valY);
Serial.print("/");
int valZ=analogRead(az);
Serial.print(valZ);
Serial.print(";");
Serial.print(valA0);
Serial.print(":");
Serial.print(valA1);
Serial.print("<");
Serial.print(button1val);
Serial.print("!");
Serial.print(button2val);
Serial.print("?");
Serial.print(swval);
Serial.print(">");
Serial.print(ystick);
Serial.print(".");
delay(100);
}
This is all being run on an Arduino Mega 2560.