# Using oscP5 with Buttons

**URL:** <https://discourse.processing.org/t/using-oscp5-with-buttons/14620>\
**Category:** Libraries\
**Created:** [October 11, 2019, 11:30pm UTC](https://discourse.processing.org/t/using-oscp5-with-buttons/14620 "2019-10-11T23:30:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SarcasticIrony](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@SarcasticIrony](https://discourse.processing.org/u/SarcasticIrony)\
**Post date:** [October 11, 2019, 11:30pm UTC](https://discourse.processing.org/t/using-oscp5-with-buttons/14620/1 "2019-10-11T23:30:35Z")

</div>

Hi, I’d like to change the background color of the second sketch using buttons from the first one. I get them to talk to each other using mouse click back and forth, but I’m struggling to switch it to a button. I first started it out with just a mousePressed, but want to move it to buttons.

Here’s the code for the first Sketch

```auto
import oscP5.*;
import netP5.*;
import interfascia.*;
 
OscP5 oscP5Location2;
NetAddress location1;

GUIController c1;
IFButton b1, b2;
IFLabel l;

int c =0;
 
void setup() {
  size(400, 400);
  
   c1 = new GUIController (this);
  
  b1 = new IFButton ("Green", 30, 35, 60, 30);
  b2 = new IFButton ("Blue", 110, 35, 60, 30);

  b1.addActionListener(this);
  b2.addActionListener(this);

  c1.add (b1);
  c1.add (b2);
 
  oscP5Location2 = new OscP5(this, 6001);
  location1 = new NetAddress("127.0.0.1", 5001);
}
 
void draw() {
   background(255,c, 0);

}
 
void mousePressed() {  
  OscMessage myMessage = new OscMessage("/test");
 
  myMessage.add("Location 2: Transmit");
  myMessage.add(mouseX);
 
  oscP5Location2.send(myMessage, location1); 
  println("Sending message.");
  
}
 
void oscEvent(OscMessage theOscMessage, GUIEvent e) {  
  String incomingHeader = theOscMessage.get(0).stringValue();
  //int incomingMouseX = theOscMessage.get(1).intValue();
  int incomingButton1 = theOscMessage.get(1).intValue();
  
   if (e.getSource() == b1) {
    background(random(255),random(255),random(255));
  } else if (e.getSource() == b2) {
   background(100, 100, 130);
 }
 
  // print out the message
  print("OSC Message Received: ");
  println(incomingHeader + " " + incomingButton1);
}

```

This is the second sketch

```auto
import oscP5.*;
import netP5.*;
 
OscP5 oscP5Location1;
NetAddress location2;
 int c =0;
 
void setup() {
  size(400, 400);
 
  oscP5Location1 = new OscP5(this, 5001);
  location2 = new NetAddress("127.0.0.1", 6001);
}
 
void draw() {
   background(255,c, 0);

}
 
void mousePressed() {  
  OscMessage myMessage = new OscMessage("/test");
 
  myMessage.add("Location 1: Transmit");
  myMessage.add(mouseX);
  myMessage.add(mouseY);
 
  oscP5Location1.send(myMessage, location2); 
  println("Sending message.");
  
}
 
void oscEvent(OscMessage theOscMessage) {  
  String incomingHeader = theOscMessage.get(0).stringValue();
  int incomingMouseX = theOscMessage.get(1).intValue();

  if (mousePressed) {
    background(random(255),random(255),random(255));
  } else {
    background(255);
  }
  
  
  // print out the message
  print("OSC Message Received: ");
  println(incomingHeader + " " + incomingMouseX);
  c = incomingMouseX;

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 12, 2019, 2:55am UTC](https://discourse.processing.org/t/using-oscp5-with-buttons/14620/2 "2019-10-12T02:55:21Z")

</div>

hi,

-a- i not see what example of INTERFASCIA button you follow?

-b- i would start with something symmetric  
( between 2 computers ( using VNC i see both screens ) )

 ![OSC_send_receive_buttons](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f6a440091eef93b6c5be37c9704f8a53d36dafca.png)

```auto
// make a symmetric sketch send recieve via network OSC by Button press

/* oscP5sendreceive by andreas schlegel */
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup_osc() {
  oscP5 = new OscP5(this,12345);
  myRemoteLocation = new NetAddress("192.168.1.204",12345); // RPI4
  //myRemoteLocation = new NetAddress("192.168.1.4",12345); // PC
}

// ___________________________________
import interfascia.*;
GUIController c;
IFButton b1,b2;

void setup_button() {
  c = new GUIController (this);  
  b1 = new IFButton ("send 1", 20, 15, 60, 30);
  b1.addActionListener(this);
  c.add (b1);
  b2 = new IFButton ("send 2", 20, 55, 60, 30);
  b2.addActionListener(this);
  c.add (b2);
}

color bg;

void setup() {
  size(100,100);
  bg = color(200);
  setup_osc();
  setup_button();
}

void draw() {
  background(bg);  
}

void actionPerformed (GUIEvent e) {
  if (e.getSource() == b1) send(1);
  if (e.getSource() == b2) send(2);
}

void send(int num) {
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(num);
  oscP5.send(myMessage, myRemoteLocation); 
}

void oscEvent(OscMessage theOscMessage) {
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  if(theOscMessage.checkTypetag("i")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int value = theOscMessage.get(0).intValue();
      print("### received an osc message /test with typetag i ");
      println(" value: "+value);
      if ( value == 1 ) bg = color(0,200,200);
      else if ( value == 2 ) bg = color(200,0,200);
      else bg = color(200,0,0);
  }
}

```

hope that example helps, sorry just my first try of oscP5 and interfascia lib…

---

<div class="post-metadata">

**Author:** ![SarcasticIrony](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@SarcasticIrony](https://discourse.processing.org/u/SarcasticIrony)\
**Post date:** [October 14, 2019, 6:30pm UTC](https://discourse.processing.org/t/using-oscp5-with-buttons/14620/3 "2019-10-14T18:30:01Z")

</div>

Thanks for trying. I’ll work on seeing if this helps, but it’s not on two different computers. It’s on the same computer, but two sketches are open.

I oscP5 file I’m working with is from[here](https://forum.processing.org/two/discussion/7109/oscp5-send-data-between-two-sketches-running-on-same-machine-ip-different-ports).

The button I’m using is from the examples in the interfascia lib.

---

<div class="post-metadata">

**Author:** ![SarcasticIrony](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@SarcasticIrony](https://discourse.processing.org/u/SarcasticIrony)\
**Post date:** [October 14, 2019, 6:43pm UTC](https://discourse.processing.org/t/using-oscp5-with-buttons/14620/4 "2019-10-14T18:43:43Z")

</div>

I got it to work!!

 ![34%20PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8417480d17086af954d0048f7d73a3de86ce41d5.png)

Here’s the code I ended up using

Sketch 1

```auto
import oscP5.*;
import netP5.*;

import interfascia.*;
GUIController c;
IFButton b1,b2;
 
OscP5 oscP5Location1;
NetAddress location2;

 
 void setup_osc() {
  oscP5Location1 = new OscP5(this, 5001);
  location2 = new NetAddress("127.0.0.1", 6001);
}

void setup_button() {
  c = new GUIController (this);  
  b1 = new IFButton ("send 1", 20, 15, 60, 30);
  b1.addActionListener(this);
  c.add (b1);
  b2 = new IFButton ("send 2", 20, 55, 60, 30);
  b2.addActionListener(this);
  c.add (b2);
}
 color bg;
void setup() {
  
  size(400, 400);
 bg = color(200);

  
  setup_osc();
  setup_button();
}
 
void draw() {
   background(bg);
  
}
void actionPerformed (GUIEvent e) {
  if (e.getSource() == b1) send(1);
  if (e.getSource() == b2) send(2);
}

void send(int num) {
 
  
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(num);
  oscP5Location1.send(myMessage, location2); 
  println("Sending message.");
  

}

void oscEvent(OscMessage theOscMessage) {
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  if(theOscMessage.checkTypetag("i")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int value = theOscMessage.get(0).intValue();
      print("### received an osc message /test with typetag i ");
      println(" value: "+value);
      if ( value == 1 ) bg = color(0,200,200);
      else if ( value == 2 ) bg = color(200,0,200);
      else bg = color(200,0,0);
  }
}

```

Sketch 2

```auto
import oscP5.*;
import netP5.*;
 
OscP5 oscP5Location2;
NetAddress location1;

import interfascia.*;
GUIController c;
IFButton b1,b2;

color bg;

void setup_osc() {
  
  oscP5Location2 = new OscP5(this, 6001);
  location1 = new NetAddress("127.0.0.1", 5001);
}

void setup_button() {
  c = new GUIController (this);  
  b1 = new IFButton ("send 1", 20, 15, 60, 30);
  b1.addActionListener(this);
  c.add (b1);
  b2 = new IFButton ("send 2", 20, 55, 60, 30);
  b2.addActionListener(this);
  c.add (b2);
}
 
void setup() {
  size(400, 400);
  bg = color(200);
  setup_osc();
  setup_button();
}
 
void draw() {
   background(bg);
 
}

void actionPerformed (GUIEvent e) {
  if (e.getSource() == b1) send(1);
  if (e.getSource() == b2) send(2);
}

void send(int num) {
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(num);
  oscP5Location2.send(myMessage, location1); 
  println("Sending message.");

}

void oscEvent(OscMessage theOscMessage) {
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  if(theOscMessage.checkTypetag("i")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int value = theOscMessage.get(0).intValue();
      print("### received an osc message /test with typetag i ");
      println(" value: "+value);
      if ( value == 1 ) bg = color(0,200,200);
      else if ( value == 2 ) bg = color(200,0,200);
      else bg = color(200,0,0);
  }
}

```
