# Combining Processing Code

**URL:** https://discourse.processing.org/t/combining-processing-code/18583
**Category:** Electronics (Arduino, etc.)
**Created:** [March 11, 2020, 2:41am UTC](https://discourse.processing.org/t/combining-processing-code/18583 "2020-03-11T02:41:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kristipham](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kristipham](https://discourse.processing.org/u/kristipham)
#### Post date: [March 11, 2020, 2:41am UTC](https://discourse.processing.org/t/combining-processing-code/18583/1 "2020-03-11T02:41:39Z")

</div>

I am controlling multiple LEDs and was able to get a simple code running in Processing with Arduino. My master code controls the LEDS based on an RTD clock module while a sensor reads the amount of lux the lighting arrays give off. After I added the processing code for the on/off switch into the master code, I get an error stating:

> Error opening serial port /dev/cu.usbmodem14331: port not found.

Both codes work perfectly fine on its own, so I’m thinking it’s the serial ports from the line: port = new Serial(this, “/dev/cu.usbmodem14331”, 9600); and myPort = new Serial(this, Serial.list()[3], 9600); from the sensors. that clashes into each other and I can’t figure out how to fix this error.  
Below is the on/off code for Processing

```auto
import controlP5.*; //library
import processing.serial.*; //library
Serial port; //do not change
ControlP5 cp5; //create ControlP5 object
PFont font; //do not change

void setup() { //same as arduino program

size(300, 300); //window size, (width, height)
port = new Serial(this, "/dev/cu.usbmodem14331", 9600); //connected arduino port
cp5 = new ControlP5(this); //do not change
font = createFont("Georgia Bold", 20); //font for buttons and title

cp5.addButton("ON") //name of button
    .setPosition(180, 50) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
;

cp5.addButton("OFF")
    .setPosition(50, 200) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
;
}

void draw() {

background(0, 0, 0); // background color of window (r, g, b)

}

void ON() {
port.write(1);
}

void OFF() {
port.write(2);
}

```

Below is the master code:

```auto
import controlP5.*; //import ControlPS library
import processing.serial.*;
Serial port; 
Serial myPort; //serial port
String inString; //input string from serial port
int lf = 10; //ASCII linefeed
ControlP5 cp5; //create ControlPS object
PFont font;

void setup(){
  size(600,800); //window size, (width, height)
  port = new Serial(this, "/dev/cu.usbmodem14331", 9600); //connected arduino port
  
  
  //adding buttons
  
  cp5 = new ControlP5(this);
 
  font = createFont("calibri light", 14); //fonts for buttons and title
  
  
    cp5.addButton("Room 1 ON")
    .setPosition(50, 50) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
    ;
    
     cp5.addButton("Room 1 OFF")
    .setPosition(180, 50) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
  ;
    cp5.addButton("Room 2 ON")
    .setPosition(50, 200) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
    ;
    
     cp5.addButton("Room 2 OFF")
    .setPosition(180, 200) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
    
  ; cp5.addButton("Room 3 ON")
    .setPosition(50, 350) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
    ;
    
     cp5.addButton("Room 3 OFF")
    .setPosition(180, 350) //x and y coordinates of upper left corner of button
    .setSize(120, 70) //(width, height)
    .setFont (font)
    .setColorBackground(color(255, 0, 0)) //background r,g,b
    .setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
    .setColorLabel(color(0, 0, 0))
  ;
  
    
  // List all the available serial ports: 
  printArray(Serial.list()); 
  
  // Open whatever port is the one you're using. 
  myPort = new Serial(this, Serial.list()[3], 9600); 
  myPort.bufferUntil(lf);
  delay(1000);
    
  myPort = new Serial(this, Serial.list()[2], 9600); 
  myPort.bufferUntil(lf);
  delay(1000);
  
  myPort = new Serial(this, Serial.list()[1], 9600); 
  myPort.bufferUntil(lf);
  delay(1000);
 
  ;  
  cp5.addButton("Errors")
    .setPosition(60, 500)
    .setSize(125, 100)
    .setFont (font)
  ;
  cp5.addButton("Alarms")
    .setPosition(60, 650)
    .setSize(125, 100)
    .setFont (font)
  ;
}
void draw(){ 
  background(0, 0, 0); //background color of window 
  
  //text
  fill(0, 255, 0); // (r, g, b) or (0 to 255)
  text("CONTROL CENTER", 250, 30);

background(0); 
  text("Sensor Reading 1: " + inString, 325,100); 
  text("Sensor Reading 2: " + inString, 325,250); //will need to revise once 3 sensors connected
  text("Sensor Reading 3: " + inString, 325,400); 
  } 
  
void ON() {
port.write(1);
}

void OFF() {
port.write(2);
}

 
void serialEvent(Serial p) 
  { 
  inString = p.readString(); 

}

```

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [March 11, 2020, 4:16am UTC](https://discourse.processing.org/t/combining-processing-code/18583/2 "2020-03-11T04:16:24Z")

</div>

My first attempt would be to bring all the serial code together and remove **controlP5** which adds unnecessary complexity to your code. Please read what is required to generate an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) which helps in cases like this.

Why you are explicitly using this line(`Serial(this, "/dev/cu.usbmodem14331", 9600)`) and why you are not using the definition right from the `serial.list()`?

If you run the four serial line definitions by themselves, does your code work?

Kf

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [March 11, 2020, 12:35pm UTC](https://discourse.processing.org/t/combining-processing-code/18583/3 "2020-03-11T12:35:08Z")

</div>

This is a related post from @kristipham those joining this topic:

> [@LED CONTROL Serial myPort vs. Serial port](https://discourse.processing.org/t/led-control-serial-myport-vs-serial-port/18293):
>
> I am trying to control multiple LEDs but I am not quite sure on how to adjust my code accordingly. I have a master code that runs an RTC clock module and a light sensor to an LED. I have separated both my codes. I was able to control an LED simply by connecting it to ground and pin 9 on an Arduino board. After running processing for just that, I was able to turn the LED on/off. Once I tried to combine it with my master code, I believe the problem was that I am using port = new Serial(this, "/de…

`:)`
