controlp5
controlp5 copied to clipboard
numberbox variable printing
Hi. 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 it in the number box but it is not working. when i print it in the console it shows the value but not in the number box.
This is my code:
import controlP5.; //import ControlP5 library import processing.serial.; String val; Serial port;
ControlP5 cp5; //create ControlP5 object PFont font;
void setup(){ //same as arduino program
size(300, 450); //window size, (width, height)
//printArray(Serial.list()); //prints all available serial ports
port = new Serial(this, "COM3", 19200); //i have connected arduino to com3, it would be different in linux and mac os
//lets add buton to empty window
cp5 = new ControlP5(this); font = createFont("calibri light bold", 20); // custom fonts for buttons and title
cp5.addButton("increase") //"red" is the name of button .setPosition(100, 50) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ;
cp5.addButton("decrease") //"blue" is the name of button .setPosition(100, 250) //x and y coordinates of upper left corner of button .setSize(120, 70) //(width, height) .setFont(font) ;
cp5.addNumberbox("temp") .setPosition(100,160) .setSize(120,70) .setFont(font) .setStringValue(val) ;
}
void draw(){ //same as loop in arduino
background(150, 0 , 150); // background color of window (r, g, b) or (0 to 255)
//lets give title to our window fill(0, 255, 0); //text color (r, g, b) textFont(font); text("CONTROL", 80, 30); // ("text", x coordinate, y coordinat) if ( port.available() > 0) { // If data is available, val = port.readStringUntil('\n');
} println(val); }
//lets add some functions to our buttons //so whe you press any button, it sends perticular char over serial port
void increase(){ port.write('r'); }
void decrease(){ port.write('b'); }
the value prints here
but here the value does not print
Hi, are you trying to assign the value of variable val to controller temp? To apply such changes you need to update the value for temp when val changes, which in the code that you provide is not the case. Also, val is of type String, however the value of a numberbox should be a number (int, float).
you could for example try the following when reading from the serialport:
if ( port.available() > 0) { // If data is available,
val = port.readStringUntil('\n');
float floatVal = float(val);
cp5.get("temp").setValue(floatVal);
}
and a simplified working sketch
// using ControlP5 library version 2.2.6
// installed via Library Manager
import controlP5.*;
String val;
ControlP5 cp5;
void setup() {
size(300, 450);
cp5 = new ControlP5(this);
cp5.addNumberbox("temp")
.setPosition(100, 160)
.setSize(120, 70)
.setStringValue(val) // doesnt have an effect on a numberbox
;
}
void draw() {
background(150, 0, 150);
if (keyPressed) {
val = random(100)+"";
float floatVal = float(val);
cp5.get("temp").setValue(floatVal);
}
}