Arduino to Processing importing variables.

edited September 2015 in Arduino

Hey all, I am quite new at that world, but I would like to know why does it happen to me. The thing is that I created a variable on Arduino which receives a random number between 0 and 100; however, I wanted to print in on Processing but it is not recognized because instead of that it writes COM3, the port where Arduino is connected. By the way, here is the Arduino code:

void setup() { //initialize serial communications at a 9600 baud rate

Serial.begin(9600); }

void loop() {

int num = random(0, 100);

Serial.println(num);

delay(100);

}

And here the Processing one:

import vsync.*;

import processing.serial.*;

Serial myPort;

void setup() {

myPort = new Serial(this, Serial.list()[0], 9600);

}

void draw() {

println(Serial.list());

delay(1000); }

I don't know if I am missing some libraries but I am kinda stocked for hours.

Thank you.

Answers

  • edited September 2015 Answer ✓
  • I am really thankful man! Everything worked fine at the end. For the ones with similar problems you could check this example as well: https://processing.org/reference/libraries/serial/serialEvent_.html

  • edited September 2015 Answer ✓

    Glad it worked! For next code posts, better learn how to format them for the forum though: *-:)
    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • Today I have been working a bit more on that, and I realised that I am not able to work with the variables separately. Here there is the example:

    ARDUINO:

        void setup() 
        {
        //initialize serial communications at a 9600 baud rate
        Serial.begin(9600);
        }
    
        void loop()
        {
        int num = random(0, 10);
        int num2 = random(10, 20);
        Serial.println(num);
        Serial.println(num2);
    
        int final = num * num2;
        Serial.println(final);
        //wait 1000 milliseconds so we don't drive ourselves crazy
        delay(1000);
        }
    

    PROCESSING:

        String inString;
        import vsync.*;
        import processing.serial.*;
        Serial myPort;
        float variable;
    
        void setup() {
          size(450, 450);
    
          myPort =new Serial(this, Serial.list()[0], 9600);
          myPort.bufferUntil(ENTER);
        }
    
        void draw() {
          background(0);
          text("NOMBRE= "+inString, 200, 200);
          text("NÚMERO= "+variable, 300, 300);
          line(200, 0, variable, variable);
          stroke(255, 255, 50);
        }
    
        void serialEvent(Serial N) {
          inString = N.readString();
          variable = float(inString);
    
        }
    

    What I would like to obtain somehow is the variables on their own. I mean, in Arduino I work with "num" and "num2" even though in Processing due to the readstring I can only use one, called "final", which turns into "variable".

    Thank you for all the help!

  • edited September 2015 Answer ✓

    Since you intend to sending out 2 int values, the 1st serialEvent() is gonna be num1.
    And the following 2nd trigger is thus num2.
    Just need an extra boolean flag variable to track down which 1 of those is currently being sent: :D

    // forum.processing.org/two/discussion/12386/
    // arduino-to-processing-importing-variables
    
    // 2015-Sep-04
    
    import processing.serial.Serial;
    
    int num1, num2;
    boolean isNum2 = true;
    
    void setup() {
      size(450, 450, JAVA2D);
      smooth(4);
      noLoop();
    
      new Serial(this, Serial.list()[0], 9600).bufferUntil(ENTER);
    }
    
    void draw() {
      background((color) random(#000000));
      println(frameCount, num1, num2, num1*num2);
    }
    
    void serialEvent(Serial s) {
      int n = int(s.readString().trim());
    
      if (isNum2 ^= true) {
        num2 = n;
        redraw = true;
      } else num1 = n;
    }
    
  • You saved my life, thanks! By the way, what does ^= mean? :D

  • edited September 2015 Answer ✓
    • ^= is 1 of the many compound assignments. Like +=, *=, %=, etc:
      https://Processing.org/reference/addassign.html
    • ^ is the bitwise operator XOR. When both bits are equal, we get 0. Otherwise 1.
    • if (isNum2 ^= true) is equivalent to if (isNum2 = !isNum2):
      https://Processing.org/reference/logicalNOT.html
    • isNum2 is switched its true/false states each time that assignment happens.
    • Since isNum2 has its initial value = true, the 1st isNum2 ^= true switches it to false.
    • Thus else num1 = n; happens 1st. And next time, num2 = n; redraw = true;. And so on... :bz
  • So now I am trying to do the same with 3 variables, and I get something like this in the serialEvent:

          int auxiliar = int(s.readString().trim());
    
        if (isNum3 ^= true)
          {
            num3 = auxiliar;
            redraw = true;
          } 
    
          else if (isNum2 ^= true){
              num2 = auxiliar;
              redraw = true;
               }
          else num1 = auxiliar;
        }
    

    It seems to work fine but the numbers I receive are mixed. I mean, num1 should stay between 0 and 10, num2 between 10 and 20 and num3 between 20 and 30 but practicaly some print lines are like this: (frame count) 22 10 23.

  • edited September 2015 Answer ✓

    That boolean approach was custom-made to deal w/ 2 diff. input values only.
    Since a boolean primitive data-type got 2 states only: false & true:
    https://Processing.org/reference/boolean.html

    We could "upgrade" it to the object data-type Boolean, so we got an extra 3rd state: null:
    http://docs.Oracle.com/javase/8/docs/api/java/lang/Boolean.html https://Processing.org/reference/null.html

    Logically it's not a definitive long-term solution b/c you may wish for more than 3 input values later...

    In order to deal w/ multiple input values, we can store them all in some array.
    Then have an extra variable to track down current input index being dealt for each serialEvent().

    And in order to reset index back to 0, we use the modulo/remaining % operator:
    https://Processing.org/reference/modulo.html

    And check out when it becomes 0 again and invoke redraw(), so we display the full array capture:
    https://Processing.org/reference/redraw_.html

    // forum.processing.org/two/discussion/12386/
    // arduino-to-processing-importing-variables
    
    // 2015-Sep-05
    
    import processing.serial.Serial;
    
    static final int QTY = 3;
    final int[] nums = new int[QTY];
    int idx;
    
    void setup() {
      size(450, 450, JAVA2D);
      smooth(4);
      noLoop();
    
      new Serial(this, Serial.list()[0], 9600).bufferUntil(ENTER);
    }
    
    void draw() {
      background((color) random(#000000));
      println("\n" + frameCount);
      printArray(nums);
    }
    
    void serialEvent(Serial s) {
      nums[idx] = int( s.readString().trim() );
      if ( (idx = (idx + 1) % QTY) == 0 )  redraw = true;
    }
    
  • Thank you another time! I am going to check it and see if that fits my project :)

  • It worked really fine, but in order to draw graphics and all of this stuff is quite hard without getting the variables as if it was the Arduino. I have tried the vSync library but without success. Is it just me or that works? It would be perfect!

  • edited September 2015

    Finally I can solve all the issues I had, thank you for everything!! :)

Sign In or Register to comment.