Serial weirdness - Reads one or five potentiometers, but not three
in
Core Library Questions
•
2 years ago
Hello all,
I have a fairly odd problem and I can't quite pinpoint the cause, so after a few hours I've given in and come to ask for help.
I've got an Arduino Uno and I'm trying to pass values of some potentiometers to Processing. I have made a sketch which will read five pots, and Processing reads each of the perfectly. I've also made a sketch that reads just one pot. Now I'm trying to make a sketch with three pots, and it will only read the first, even though the Arduino PDE's serial viewer shows that each pot is working fine.
The Processing code for the five pot example is for a digital Etch-A-Sketch, it reads the first two pots as X and Y coordinates for one end of a line (then on the next loop the previous value is used as the other end of the line, similar to mouseX and pmouseX), the other three are used to mix red, green and blue for the stroke colour. I added a reset button but I haven't bothered to make it work yet. The code is as follows:
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];
}
}
}
And now, with my latest sketch, I want to use the first pot to rotate a line using the rotate command and the second and third pots are to be used as translate coordinates, but it's these pots that it just won't read. I mapped the potentiometer readings (see the fifth and fourth line from the bottom) to see if that would work, but it didn't. The terminal prints the first pot's value fine (in radians, at least), but the next two are just '[pot 1's value], 0.0, 0.0'. The code is:
import processing.serial.*;
float spinValue = 0;
float xVal = 0;
float yVal = 0;
int linefeed = 10;
Serial myPort;
void setup() {
size(1024,1024);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(linefeed);
smooth();
background(0);
}
void draw() {
print(spinValue);
print(", ");
print(xVal);
print(", ");
println(yVal);
stroke(255);
fill(128);
translate(xVal, yVal);
rotate(spinValue);
line(0, 0, 512, 0);
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if (myString != null){
myString = trim(myString);
int sensors [] = int(split(myString, ','));
if (sensors.length == 3) {
spinValue = radians(map(sensors[0], 0, 1023, 0, 360));
xVal = map(sensors[1], 0, 1023, 0, 100);
yVal = map(sensors[2], 0, 1023, 0, 100);
}
}
}
I won't post the Arduino code just yet because a) it works, as far as the Arduino PDE's serial monitor is concerned and b) it's only slightly different to the code that worked for the first Processing sketch I posted here.
I'm not very new to Processing but this is only the second time I've tried USB serial communication with an Arduino, so I'm sure whatever it is is either a Java bug (unlikely), something very obvious that I'm just not seeing (likely) or a problem with the Arduino (I'm almost certain it isn't).
I used the book 'Making Things Talk' as a guide to setting up serial communication and as such I don't fully understand the code, nor do I really understand how you're supposed to know what a library is doing or what it can do, but as it worked with one potentiometer and also with five, I can't understand why it won't work with just two!
Also I'm not sure if this belongs in Integration and Hardware or not as I'm almost 100% sure it's to do with Processing and maybe Java, not the hardware. Sorry if this is the wrong place.
Thank you,
neema_t
p.s. I have tried this in Processing 1.5.1 and 1.2.1 (the Etch-A-Sketch works in both anyway), with Java 1.6 64- and 32-bit, and still nothing.
1