Arduino Flex sensor values stored in 2 processing variables.

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];

}
}
}

I have written this code but for some reason it will only ever print the values out in processing as.

As what?

Debugging this should be trivial. Answer the following questions.
Does serialEvent() ever get called? Probably not. Why? Because you've asked Processing to buffer serial data until a linefeed arrives, but the Arduino never sends a linefeed. The last Serial.print() in the Arduino code should be a Serial.println() to send a carriage return/linefeed.

Does myString look reasonable (value, comma, value)?

Does sensors end up with a length of 2?

Does sensors[0] contain a reasonable value?
Does sensors[1] contain a reasonable value?

Finally, if xAxis and yAxis contain reasonable values, what line will be drawn?

Thanks for the reply. I meant to say processing only prints numbers out as zeros. I changed the last print to a println in arduino so hopefully it will work now, ill test it out today and get back to you :slight_smile:

Awesome got it working, the problem was that I was using A0 and A5 in the analog read which was creating a string that looked like:
A0 value = 250
A5 value = 256
A0 value = 250
A5 value = 256
etc...
As soon as i changed A0 -> 0 and A5 -> 5 it generated the right string in the form of:
250,256
250,256

I didnt realise that A0 and 0 were 2 completely different things =\

I don't see any reference in the Arduino code posted in this thread to A0 or A5, but, of course those are aliased for the analog pins being used as digital pins. Too many of the tutorials mistakenly use An where they should be using n, so it is easy to see why you would make that mistake. I've asked that the tutorials that are wrong be fixed, but that request fell on deaf ears.

Anyway, I'm glad you got it working.