How to make a processing GUI with a slider and button talking to Arduino

Dear all;

I am trying to make a guy with a slider and two buttons talking to Arduino. Unfortunately, I cannot detect the signals send from processing to Arduino. I think I am struggling to separately read the serial signal from slider and button in Arduino code. I guess I should differentiate slider and buttons as separate objects so that port.write can send it as separate variable.

Any suggestions?

Processing code:

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

void setup() {

  size(500, 300); //window size, (width, height)
  printArray(Serial.list());
  port = new Serial(this, "/dev/cu.usbmodem14101", 9600); //connected arduino port
  cp5 = new ControlP5(this); //do not change

     font = createFont("calibri", 20);    // custom fonts for buttons and title
  
     cp5.addButton("SFL_ON")     //"Turn On" is the name of button
    .setPosition(300, 50)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    .setFont(font)
  ;

      cp5.addButton("SFL_OFF")     //"Turn On" is the name of button
    .setPosition(300, 150)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
    .setFont(font)
  ;
   
     cp5.addSlider("led")
    .setPosition(125, 20) //x and y upper left corner
    .setSize(50, 250) //(width, height)
    .setRange(0, 255) //slider range low,high
    .setValue(4) //start val
    .setColorBackground(color(0, 0, 255)) //top of slider color r,g,b
    .setColorForeground(color(0, 255, 0)) //botom of slider color r,g,b
    .setColorValue(color(255, 255, 255)) //vall color r,g,b
    .setColorActive(color(255, 0, 0)) //mouse over color
    ;
    
}

void draw() {

  background(0, 0, 0); // background color of window (r, g, b)
    textFont(font);
  text("SFL CONTROL", 300, 30);
}

void led(int led)
{
  port.write(led);
}

void SFL_ON(){
  port.write('y');
}


void SFL_OFF(){
  port.write('n');
}

Arduino code

const int NumberRepeats = 5;
const int washtime = 1000;
const int flowdelaytime = 1000;
const int exposetime = 1000;

void setup() {

  pinMode(3, OUTPUT); // SELENOID VALVE/ FLOW
  pinMode(4, OUTPUT); // UV sourece
  Serial.begin(9600); //start serial
  Serial.println("<Arduino is ready>");
}

void loop() {

  if (Serial.available()) { //if data available

//char SFL=Serial.readString();
char SFL = Serial.read();
  //  int val = Serial.read();

    if (SFL == 'y') {     //if n received

      for (int i = 1; i < NumberRepeats; i++) {
        //Turn on the flow i.e. air pressure
        digitalWrite(3, HIGH);
        //Wait for the flow to wash out particles
        delay(washtime);// washtime

        //Turn off the flow
        digitalWrite(3, LOW);

        //Wait for the flow to stop
        delay(flowdelaytime);//flowdelaytime


        //Turn the UV light on i.e. open shutter
        digitalWrite(4, HIGH);

        //Expose the polymer to UV
        delay(exposetime);// exposetime

        //Turn the UV light off i.e. close shutter
        digitalWrite(4, LOW);

      }

    }


    //  // Exit the loop if SFL_OFF received

    // if (SFL == 'n') {     //if n received
    //    exit(0);//The 0 is required to prevent compile error
    //  }

  }

}
1 Like

pls take a step back from your project and start a new one,
just use the serial lib for processing
https://processing.org/reference/libraries/serial/serialEvent_.html

and make a bidirectional code for arduino and processing

to verify what is send and understood try some code like a ECHO in arduino
so you have a feedback ( to processing ) what later can be disabled.

1 Like

Dear kll
Great Thanks for the suggestion. I am going through tutorials in serialEvent and simplifying the code. I will update you on the progress.

Cheers
B

Hi There;

I simplified my program and managed to isolate my issue. What I want is to send is 4 integers and a text string from Processing to Arduino as shown in the attachment.

1)When I used the SimpleWrite example from Processing>File>Examples>Serial>SimpleWrite. I have managed to turn on/off a LED in my Arduino so sending characters ‘H’ or ‘L’ from Processing to Arduino with serial communication works.

  1. I changed the Processing and Arduino codes to see if I can send a number 1 & 2 from P to A. It also works. I am not sure if Arduino accepts them as integer through serial communication.

  2. I replaced the simple GUI in SimpleWrite with a slider from ControlP5 and send the integer value of the slider to Arduino. It does not work.

To sum up, I think I can not get the Processing to send an integer value of the slider to Arduino. Any ideas will help. Thanks

I copy paste the Processing and Arduino codes:
Processing code: SFL_gui_v1

import controlP5.*; //library
import processing.serial.*; //library
Serial port; //do not change
ControlP5 cp5; //create ControlP5 object
//int led;
//
void setup() {
  size(400, 450); //window size, (width, height)
  printArray(Serial.list());
  port = new Serial(this, "/dev/cu.usbmodem14101", 9600); //connected arduino port
  cp5 = new ControlP5(this); //do not change
//
  cp5.addSlider("STOP_TIME")
    .setPosition(125, 50) //x and y upper left corner
    .setSize(200, 50) //(width, height)50, 250
    .setRange(0, 200) //slider range low,high
    .setValue(100) //start val
    .setColorValue(color(255, 255, 255)) //vall color r,g,b
    .setColorActive(color(255, 0, 0)) //mouse over color
   // .setScrollSensitivity(1)
  //  .setNumberOfTickMarks(20)
    ;    
}
void draw() {
  background(0, 0, 0); // background color of window (r, g, b)
}
void led(int STOP_TIME)
{
  port.write(STOP_TIME);
}

Arduino code

 int val; // Data received from the serial port
 int ledPin = 4; // Set the pin to digital I/O 4
 void setup() {
 pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
 Serial.begin(9600); // Start serial communication at 9600 bps
 }
 void loop() {
 while (Serial.available()) { // If data is available to read,
 val = Serial.read(); // read it and store it in val
 }
 if (val == 100 ) { // If H was received 'H'
 digitalWrite(ledPin, HIGH); // turn the LED on
 } else {
 digitalWrite(ledPin, LOW); // Otherwise turn it OFF
 }
 delay(100); // Wait 100 milliseconds for next reading
 }

1 Like

serial print is ASCII string,
so if you send a CSV type line in both directions,

1023,1023,1023

you need a split on “,” and convert to numbers on both sides

1 Like

Hi kll;

Great Thanks Do you mean that STOP_TIME i.e. 100 I send from Processing is broken into three 10 bits separated with commas? How can I see this in Arduino or Processing?

I tried the to print what I received in serial monitor but it does not work , it seems to clash with processing as it shares serial port and in processing print(STOP_TIME) does not work with void led.

I need to read up more on Serial communication I think. I get that serial communication is ASCII but It is not clear to me why it 10 bit not 8 bit,

Cheers

Hi all,

I am having the same problem as you are, burak.eral. I am trying to read integers from two different sliders in Processing into Arduino. I, too, have be able to work with the H/L button options but seem to have gotten stuck where you did.

Were you able to work around this problem?

Best

2 Likes

Hi Ctj001;

Yes I did solve the problem I am using 4 sliders and a button. You need to use port.write in the format below with Processing then read on Arduino side using Serial.parseInt.

This video was useful

On Processing side you need the following

  1. Define integers S1,S2,S3,S4,SFL
  2. Use sliders from controlP5 library by importing them then activating, name them “S1” "S2: …
  3. port.write(“a”+ S1 + “,” +S2+“,” +S3+“,” +S4+“,”+SFL+“\n” );

On Arduino side, you need to read the data then parse.

if (Serial.available()>0)
  {
    if(Serial.find('a'))  {
    val1=Serial.parseInt();
    val2=Serial.parseInt();
    val3=Serial.parseInt();
    val4=Serial.parseInt();
    val5=Serial.parseInt();
    
  }
3 Likes

Thank you for taking your time to reply!

This was extremely helpful.