Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: eschulzpsd on Oct 09, 2018, 03:26 am

Title: Serial Comm - three potentiometers from Arduino to Processing
Post by: eschulzpsd on Oct 09, 2018, 03:26 am
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:

Code: [Select]
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:

Code: [Select]
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);
}
Title: Re: Serial Comm - three potentiometers from Arduino to Processing
Post by: Robin2 on Oct 09, 2018, 11:04 am
I don't know Processing but if that was my problem I would send all the values in one message, always in the same order with code something like this
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


Then what Processing would get would be like this <23,45>

Have a look at the 3rd example in Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) to see how that concept is used. The idea can be adapted to any programming language.

...R
Title: Re: Serial Comm - three potentiometers from Arduino to Processing
Post by: eschulzpsd on Oct 10, 2018, 03:51 am
Thanks for the reply.  That's a great resource that I will keep in mind. :)

Problem fixed with a delay(10) line after the variables were assigned elements of the arrays.  Who knew?

E