Combining Processing Code

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

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:

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(); 

}
1 Like

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 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

1 Like

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

:)

1 Like