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