Sending a variabel between sketches

edited January 2017 in Questions about Code

Hi again i´m working on a project that consists of three sketches each one is using a different kind of data from three arrays that all represent the same thing but in a different form (Pic, word, rect). The sketches would run on the same computer at the same time. As i understand it sending and reading it from a serialport doesn´t work.

is there a lib or something smart to do it with?

Tagged:

Answers

  • Have a look at the OSC library. You can install it from the processing's library manager. Check the provided examples or previous relevant posts:

    https://forum.processing.org/two/search?Search=oscP5

    Now, if you run all three sketches from a single sketch, then you could access global data from what I have seen in previous posts.

    Kf

  • thank you but all i seem to find is stuff about making sliders etc and same sort of interface? sorry

  • i am really lost with the lib. could i just use something like this https://github.com/alexandrainst/processing_websockets/blob/master/README.md and have one sketch just writing the variabel to the server and each one receiving the value?

  • edited January 2017

    @EtJA -- It sounds like you might be confusing the ControlP5 GUI library with what was recommended, the OSC P5 library for signal passing, which uses the Open Sound Control protocol. However OSC messages don't have to be sound-related or UI related -- they can be any variable.

  • @jeremydouglass thank you that helped my confusion.

    but i´m still not sure how to implement that in the sketches i check most of the examples. if i break the send and receive up into two it doesn´t work very well

  • I believe this approach can be helpful: https://forum.processing.org/two/search?Search=spacefromtop

    You can look at other relevant posts: https://forum.processing.org/two/search?Page=p2&Search=oscP5

    You can also provide your code so people can see your approach and provide effective feedback.

    Kf

  • some basic idea is got two sketches both select a word at random from a list of words but both should select position within the list at the same time. i need both to share value for the int erstes.

    number one:

    String[] wort;
    String[] text;
    int erstes;
    int zweites;
    int xpos;
    int ypos;
    int h=8; // vertikale länge
    int laenge = 0;
    String toBinary(String word) {
      String binaryv="";
      for (int i=0; i<word.length(); i++) {
        binaryv+=binary(word.charAt(i), 8);
      }
      return binaryv;
    }
    
    void setup() {
      noSmooth();
      size (1080, 1680, P2D);
      frameRate(23);
      background(255);
    
      String[] wort = loadStrings("text4.txt");
      String  alles = join(wort, " ");
      text = split(alles, " ");
    }
    
    void draw() {
      int d=(int) random (50,150);
      int c=frameCount%d;
      if (c==d-1) {
       erstes = int(random(0,10000));
    
        String binaer=toBinary(text[erstes]);
      println(binaer);
      laenge = binaer.length();
      println(laenge);
    
        int rectlength1 =  (int) random(8, 160);
        int rectlength2 =  laenge;
        fill (255);
        noStroke();
        xpos = int (random(rectlength1, width)-rectlength1);
        ypos = int (random(height/5)*5);
        text(binaer, xpos, ypos);
        rect(xpos, ypos, laenge, 4);
      }
      for (int y=0; y<height; y=y+h) {
        for (int x=0; x<width; x++) {
          stroke((int)random(2)*255, 8);
          line(x, y, x, y+1.5*h);
        }
      }
    }
    

    number two

    String[] wort;
    String[] text;
    int laenge = 0;
    int erstes;
    
    
    PFont myFont;
    
    void setup(){
    
      size(1680,1050);
      String[] wort = loadStrings("text4.txt");
      String  alles = join(wort, " ");
      text = split(alles, " ");
      frameRate(0.5);
       myFont = createFont("MyriadPro-Bold",34);
      textFont(myFont);
      smooth();
    
    
    }
    
    
    void draw(){
    
       erstes = int(random(0,9953));
    
      background(0);
      fill(255);
      textSize(10);
      textAlign(CENTER);
      text(text[erstes], (width/2), (height/2));
    }
    
      void mousePressed() { 
    
      link("https://books.google.com/ngrams/graph?content="+text[erstes]+"&year_start=1800&year_end=2008&corpus=20&smoothing=3&share=&direct_url=t1%3B%2C"+text[erstes]+"%3B%2Cc0"); 
    
    }
    
  • Answer ✓

    Here is a basic implementation. I modified the sketch a bit to adapt it to my text4.txt input. Kf

    Sketch transmitting:

    import oscP5.*;
    import netP5.*;
    
    OscP5 oscP5;
    NetAddress myRemoteLocation;
    int rxPort=12000;
    int txPort=12001;
    
    String[] wort;
    String[] text;
    int laenge = 0;
    int erstes;
    
    
    PFont myFont;
    
    void setup() { 
      size(300,300);
      String[] wort = loadStrings("text4.txt");
      String  alles = join(wort, " ");
      text = split(alles, " ");
      frameRate(0.5);
      myFont = createFont("MyriadPro-Bold", 34);
      textFont(myFont);
      smooth();
    
      oscP5 = new OscP5(this, rxPort);
      myRemoteLocation = new NetAddress("192.168.0.13", txPort);
    }
    
    
    void draw() {
    
      erstes = int(random(0, text.length));
      OscMessage myMessage = new OscMessage("/picked");
      myMessage.add(erstes);
      myMessage.add(text[erstes]);
      oscP5.send(myMessage, myRemoteLocation); 
    
      background(0);
      fill(255);
      textSize(10);
      textAlign(CENTER);
      text(text[erstes], (width/2), (height/2));
    }
    
    void mousePressed() { 
    
      //link("<a href="https://books.google.com/ngrams/graph?content=" target="_blank" rel="nofollow">https://books.google.com/ngrams/graph?content=</a>;"+text[erstes]+"&year_start=1800&year_end=2008&corpus=20&smoothing=3&share=&direct_url=t1%3B%2C"+text[erstes]+"%3B%2Cc0");
    }
    

    Sketch receiving:

    import oscP5.*;
    import netP5.*;
    
    OscP5 oscP5;
    NetAddress myRemoteLocation;
    int rxPort=12001;
    int txPort=12000;
    
    int rxValue=0;
    
    
    String[] wort;
    String[] text;
    int erstes;
    int zweites;
    int xpos;
    int ypos;
    int h=8; // vertikale länge
    int laenge = 0;
    String toBinary(String word) {
      String binaryv="";
      for (int i=0; i<word.length(); i++) {
        binaryv+=binary(word.charAt(i), 8);
      }
      return binaryv;
    }
    
    void setup() {
      size (500,500, P2D);
      frameRate(23);
      background(255);
      noSmooth();
    
      String[] wort = loadStrings("text4.txt");
      String  alles = join(wort, " ");
      text = split(alles, " ");
    
      oscP5 = new OscP5(this, rxPort);
      myRemoteLocation = new NetAddress("192.168.0.13", txPort);
    }
    
    void draw() {
      int d=(int) random (50, 150);
      int c=frameCount%d;
      if (c==d-1) {
        erstes = int(random(0, text.length));
    
        String binaer=toBinary(text[erstes]);
        println(binaer);
        laenge = binaer.length();
        println(laenge);
    
        int rectlength1 =  (int) random(8, 160);
        int rectlength2 =  laenge;
        fill (255);
        noStroke();
        xpos = int (random(rectlength1, width)-rectlength1);
        ypos = int (random(height/5)*5);
        text(binaer, xpos, ypos);
        rect(xpos, ypos, laenge, 4);
      }
      for (int y=0; y<height; y=y+h) {
        for (int x=0; x<width; x++) {
          stroke((int)random(2)*255, 8);
          line(x, y, x, y+1.5*h);
        }
      }
    }
    
    void oscEvent(OscMessage theOscMessage) {
      /* print the address pattern and the typetag of the received OscMessage */
      print("### received an osc message.");
      print(" addrpattern: "+theOscMessage.addrPattern());
      println(" typetag: "+theOscMessage.typetag());
    
    
    
      rxValue = theOscMessage.get(0).intValue();
      String strVal = theOscMessage.get(1).stringValue();
      println("The data: "+rxValue+" "+strVal); 
    }
    
Sign In or Register to comment.