reading analog variables into processing

Im doing a project at the moment where i am using flex sensors to measure texture. These flex sensors are read by arduino as analog values. These values I want to be sent to processing. I have downloaded the Serial library for processing to do this. BUt despite researching all day I haven't been able to find a way to store the readings from the flex sensors into a variable in processing.

Here is my arduino code:
const int analogInPin = A0;
const int analogInPin2 = A5;
const int analogOutPin = 9;
const int analogOutPin2 = 9;

int sensorValue = 0;
int sensorValue2 = 0;
int outputValue = 0;
int outputValue2 = 0;

void setup() {

Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
sensorValue2 = analogRead(analogInPin2);

outputValue = map(sensorValue, 0, 1023, 0, 255);
outputValue2 = map(sensorValue2, 0, 1023, 0, 255);

analogWrite(analogOutPin, outputValue);
analogWrite(analogOutPin2, outputValue2);

Serial.print("\t output A0= ");
Serial.println(outputValue);
delay(10);
Serial.print("\t output A5= ");
Serial.println(outputValue2);

delay(10);
}

So my question is can anybody send me a code that can store 2 analog variables from arduino in processing?

I have written this processing code but I am positive that it is completely wrong haha:
import processing.serial.;
import cc.arduino.
;

Arduino arduino;
float myValue;
float myValue2;

void setup()
{
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 9600);
}

void draw()
{
myValue = arduino.analogRead(0);
println(myValue);
myValue2 = arduino.analogRead(5);
println(myValue2);
}

Thanks in advance!

Your Processing application and your Arduino sketch are not compatible.

The Arduino application is sending data directly to the serial port. The Processing sketch assumes that the Arduino has the standard firmata sketch installed, so that it can directly ask for a specific value.

You can either load the standard firmata sketch on the Arduino, or look at the Library + Serial + SimpleRead example to see how to make the Processing application read what you are currently sending it.

I made an Etch-a-Sketch type thing a while ago which uses two potentiometers to draw a line in Processing and three others to mix red, green and blue (which was also displayed on the Arduino board by way of an RGB LED), and there's also a reset button which redraws the background in Processing. It was one of the first things I ever made, but it does work.

Oh and I also included code so that you can read the x, y, red, green and blue values in Arduino's serial monitor for debugging. Of course, you need to close the Arduino PDE before running the Processing sketch to free up the port (not sure I know what I'm talking about here, but I hope that's correct).

Third edit: The codes were adapted from Making Things Talk, which I think is pretty much an essential book if you want to do much Arduino/Processing integration. There may be redundant code in the Processing sketch, but as I said, it does work, so I left it.

This is my Arduino code:

int xPot = 1;
int yPot = 0;
int rPot = 4;
int gPot = 3;
int bPot = 2;
int reset = 2;
int rLed = 3;
int gLed = 5;
int bLed = 6;

int xVal = 0;
int yVal = 0;
int rVal = 0;
int gVal = 0;
int bVal = 0;
int resetVal = 0;
int rLedVal = 0;
int gLedVal = 0;
int bLedVal = 0;

void setup(){
Serial.begin(9600);
pinMode(reset, INPUT);
pinMode(rLed, OUTPUT);
pinMode(gLed, OUTPUT);
pinMode(bLed, OUTPUT);
}

void loop(){
xVal = analogRead(xPot);
yVal = analogRead(yPot);
rVal = analogRead(rPot);
gVal = analogRead(gPot);
bVal = analogRead(bPot);
resetVal = digitalRead(reset);

Serial.print(xVal, DEC);
Serial.print(",");
Serial.print(yVal, DEC);
Serial.print(",");
Serial.print(rVal, DEC);
Serial.print(",");
Serial.print(gVal, DEC);
Serial.print(",");
Serial.print(bVal, DEC);
Serial.print(",");
Serial.println(resetVal);

rLedVal = map(rVal, 1023, 0, 0, 255);
gLedVal = map(gVal, 1023, 0, 0, 255);
bLedVal = map(bVal, 1023, 0, 0, 255);
analogWrite(rLed, rLedVal);
analogWrite(gLed, gLedVal);
analogWrite(bLed, bLedVal);

}

And this is my Processing code (written in 1.2.1, it didn't work in 0194 and I have yet to try the latest build)

import processing.serial.*;

float xAxis, yAxis;
float pxAxis, pyAxis;
int resetButton;
float mixRed, mixGreen, mixBlue;

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;
resetButton = 0;
mixRed = 0;
mixGreen = 0;
mixBlue = 0;

smooth();
background(0);
}

void draw() {

stroke(mixRed, mixGreen, mixBlue);
noFill();
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 == 6) {
xAxis = sensors[0];
yAxis = sensors[1];
mixRed = map(sensors[2], 0, 1023, 0, 255);
mixGreen = map(sensors[3], 0, 1023, 0, 255);
mixBlue = map(sensors[4], 0, 1023, 0, 255);
resetButton = sensors[5];
}
}
}

I hope this helps!

Hey, thanks A LOT for the replys, apreciate it. Ill try set it up and get back to you both tomorrow to let you know how it goes :slight_smile:

Sorry for the late reply, been quite busy, but i have finally got around to trying it out. I have changed the code to suit what i have on my arduino board (2 flex sensors). But it still isnt quite working properly. Can anyone see something obvious that im doing wrong? I still cant seem to manage to get processing to read the 2 different values individually from arduino.

Heres the current 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];

}
}
}

Can anyone see something obvious that im doing wrong?

Yes, but I answered in your other thread. Please don't crosspost. It breaks the continuity of threads.

Sorry for the double post, i didnt think i posted it in the right section which is why i wasnt get much interest.

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 =\