Hey, im trying to send the values from my flex sensor from arduino and into processing where they can be read as 2 individual variables. I have written this code but for some reason it will only ever print the values out in processing as. Can anyone tell me how to fix this code? Really need to get this completed by Friday haha. How it works is that the serial reads in the string of numbers into processing where the string is split up (using the split function) and the values are stored in the variables xAxis and yAxis, in theory anyway... p.s i know the circuitry is all correct as i have tested it in the arduino console.
Any help would be greatly appreciated by my group, Thank you in advance.
|
Heres the arduino code:
int xPot = 1;
int yPot = 0;
int reset = 2;
int ValA0 = 0;
int ValA5 = 0;
int resetVal = 0;
void setup(){
Serial.begin(9600);
pinMode(reset, INPUT);
}
void loop(){
ValA0 = analogRead(xPot);
ValA5 = analogRead(yPot);
resetVal = digitalRead(reset);
Serial.print(ValA0, DEC);
Serial.print(",");
Serial.print(ValA5, DEC);
}
And heres the current processing code:
import processing.serial.*;
int xAxis, yAxis;
int pxAxis, pyAxis;
int linefeed = 10;
Serial myPort;
void setup() {
size(1024, 1024);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(linefeed);
xAxis = 0;
yAxis = 0;
pxAxis = 0;
pyAxis = 0;
smooth();
background(0);
}
void draw() {
line(xAxis, yAxis, pxAxis, pyAxis);
pxAxis = xAxis;
pyAxis = yAxis;
print(yAxis);
print(",");
println(xAxis);
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil(linefeed);
if (myString != null) {
myString = trim(myString);
int sensors[] = int(split(myString, ','));
if (sensors.length == 2) {
xAxis = sensors[0];
yAxis = sensors[1];
}
}
}