controlP5 number box variable printing

595 Views Asked by At

I am trying to print a variable in the number box using controlp5. The data is read from the serial port and stored in a variable. I want to print the value in the number box it is not working. when I print it in the console it shows the value, but not in the number box.

Here is my code:

import controlP5.*;
import processing.serial.*;
ControlP5 cp5;
PFont font;
enter code here
void setup(){
size(300, 450); 
port = new Serial(this, "COM3", 19200);
cp5 = new ControlP5(this);
font = createFont("calibri light bold", 20);

cp5.addButton("increase").setPosition(100, 50)
.setSize(120, 70).setFont(font);

cp5.addButton("decrease").setPosition(100, 250)
.setSize(120, 70).setFont(font);

cp5.addNumberbox("temp").setPosition(100,160)
.setSize(120,70).setFont(font).setStringValue(val);

void draw(){
background(150, 0 , 150);`
 fill(0, 255, 0); 
textFont(font);
text("CONTROL", 80, 30);
if ( port.available() > 0) 
{ 
  val = port.readStringUntil('\n'); 
}
println(val); 


void increase(){


port.write('r');
}


void decrease(){
  port.write('b');
}

In this code, val is the variable and the data is read from the serial port and stored in val but .setStringValue(val) is not printing the value of val. but if I print val in command console it shows the value of variable val.

2

There are 2 best solutions below

0
On

Please try to post valid code. This code will not compile as you're missing brackets, and we can't run it because it's not a MCVE.

I also recommend that you get into the habit of debugging your code to understand what's going on. Step through your code line-by-line to figure out when each line is happening.

Specifically, you're only setting the value here:

cp5.addNumberbox("temp").setPosition(100,160)
.setSize(120,70).setFont(font).setStringValue(val);

This line of code happens once at the beginning, when val is set to its default value. (Note that you don't even declare the variable in the code you've posted. Please post a MCVE.)

Later on in the code, you update the val variable:

if ( port.available() > 0) 
{ 
  val = port.readStringUntil('\n'); 
}
println(val); 

But note that you are not updating the value that's displayed in your number box. Updating the val variable does not automatically update places that have already used the variable. You need to manually update the number box every time the value changes.

0
On

Use .setValue() rather than .setStringValue().

You will have to call Integer.valueOf(val) to cast it to a type the that your Numberbox will accept.

The resulting code may look like this:

if ( port.available() > 0) 
{ 
  val = port.readStringUntil('\n');
  cp5.get("temp").setValue(Integer.valueOf(val));
}

However, if you simply want to display the value, I'd recommend using Processing's text() or CP5 TextArea or Textlabel since Numberbox is intended for use when you want to provide an easy way for the user to change / specify a value, rather than just display it. A Numberbox's value can be adjusted by scrolling the middle mouse button when the mouse is over the box.