I'm sending potentiometer values from Arduino to Processing. When I send two pot values from Arduino - A0 to serialArray[0] and A1 to serialArray[1], the Processing sketch works fine.
When I add one more pot value from A2 intended to go to serialArray[2], the potentiometers work, but somehow they change their order so the wrong knobs are controlling the outputs in the sketch.
Not sure what I'm missing. Thanks for any feedback.
Arduino code:
int potArduino0;
int potArduino1;
int potArduino2;
int inByte = 0;
void setup() {
Serial.begin(115200);
establishContact();
}
void loop() {
if(Serial.available() > 0){
inByte = Serial.read();
potArduino0 = analogRead(A0)/4;
Serial.write(potArduino0);
delay(10);
potArduino1 = analogRead(A1)/4;
Serial.write(potArduino1);
delay(10);
potArduino2 = analogRead(A2)/4;
Serial.write(potArduino2);
delay(10);
}
}
void establishContact(){
while(Serial.available() <= 0){
Serial.println('A');
delay(300);
}
}
Processing code:
import processing.serial.*;
Serial myPort;
int[] serialArray = new int[3];
int serialCount = 0;
int xpos;
int ypos;
int colorChange;
boolean firstContact = false;
void setup(){
size(256,256);
background(255);
noStroke();
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);
}
void serialEvent(Serial myPort){
int inByte = myPort.read();
println(inByte);
if(firstContact == false){
if(inByte == 'A'){
myPort.clear();
firstContact = true;
myPort.write('A');
println('A');
}
}else{
serialArray[serialCount] = inByte;
serialCount++;
if(serialCount > 2){
xpos = serialArray[0]; //knob 1 somehow now changes color
ypos = serialArray[1]; //knob 2 somehow now controls x
colorChange = serialArray[2]; //know 3 somehow know controls y
//This order was not rearranged when there was only 2 pots for x and y
println(xpos + "x " + ypos + "y ");
println(colorChange);
myPort.write('A');
serialCount = 0;
}
}
}
void draw(){
fill(colorChange,0,0);
ellipse(xpos, ypos, 20, 20);
}