controlP5 checkbox GUI Question
in
Programming Questions
•
2 years ago
Hey everybody,
I'm pretty new to Processing and Java and whatnot. I'm working on a project at the university level that involves an Arduino and a electro-sensitive membrane that allows variable flux when varying voltages are applied. So, I'm using the Arduino and pwm to control the voltage going to the membrane. I can communicate with the Arduino via Bluetooth and Processing. No problem. So, in my program, I have a voltage slider that when changed adjusts the value of a PWM slider. I want to write this PWM slider value to the serial port. No problem, I tested my program with an led and it worked fine. Ok. So now, I decided I needed some sort of timing control so that I could do lab tests using the Arduino controlled membrane. At this point, I wanted to keep everything as simple as possible, so I'm using 24 checkboxes (each one corresponding to a specific hour) with values 0-23. I want the PWM value to be sent when these checkbox values match the current hour in time (its kind of like an alarm, i guess). However, say I have 00:00 checked 01:00 unchecked and 02:00 checked, I want the pwm value to be 0 at 01:00 if that checkbox is not checked. I can get the checkboxes to work with the current hour, but I can't figure out how to turn a previous checkbox off when a new hour comes about. For example, if i had 14:00 checked, 15:00 unchecked, and 16:00 checked, the program works for 14:00 checkbox if the time is 14:00, however if 15:00 isn't checked, the value from 14:00 carries over into the new hour. I don't want this to happen, as this does not allow any sort of timing control. I need to select checkboxes for hours that I want higher than 0 pwm values sent to the arduino, and for the hours that aren't selected I want them to send a pwm=0 value. Also, to conserve battery life, I would like for a way to have the program only send a value to arduino at the moment the hour changes, and then store that value in the Arduino and run pwm in a loop for an hour until the next hour is reached at which a new pwm value is sent. I don't want constant pwm values being sent from processing to the arduino over bluetooth. Also, is there a way to break connection after the serial data is sent, and then reconnect on the hour every hour? I hope you can understand what I'm aiming for here. Can anyone help? My code is below. Note, there are a lot of different things commented that I was playing with that I didn't want to erase quite yet, so pay no attention to them. The main area of focus should be near the if(sendData) statement in the infinite loop. Thanks.
// serial variables
import processing.serial.*; // serial library
//Serial port;
Serial[] myPorts = new Serial[1]; // lets only use one port in this sketch
// GUI variables
import controlP5.*; // controlP5 library
ControlP5 controlP5; // create the handler to allow for controlP5 items
Textlabel txtlblWhichcom; // text label displaying which comm port is being used
Textarea commTextarea; // text area displaying what has been received on the serial port
ListBox commListbox; // list of available comm ports
//PImage bg; //defines bg as the background image
int myColorBackground = color(0,0,0);
int CheckBox;
CheckBox checkbox;
Boolean sendData;
// setup
void setup() {
size(360,500);
frameRate(30);
controlP5 = new ControlP5(this); // initialize the GUI controls
println("Available serial ports:");
println(Serial.list()); // print the comm ports to the debug window for debugging purposes
// make a listbox and populate it with the available comm ports
commListbox = controlP5.addListBox("myList",5,25,120,120); //addListBox(name,x,y,width,height)
commListbox.captionLabel().toUpperCase(false);
commListbox.captionLabel().set("SELECT A COM PORT");
for(int i=0;i<Serial.list().length;i++) {
commListbox.addItem("port: "+Serial.list()[i],i); // addItem(name,value)
}
// text label for which comm port selected
txtlblWhichcom = controlP5.addTextlabel("txtlblWhichcom","No Port Selected",150,25); // textlabel(name,text,x,y)
PFont p = createFont("MS UI GOTHIC",10); //changes the default font of all letters
controlP5.setControlFont(p);
controlP5.addBang("SWEET RELEASE",210,100,20,20); //Adds the bang button that when pressed triggers the VOLTAGE slider to 3.3V (max)
controlP5.addBang("EMERGENCY CUTOFF",80,100,20,20); //Adds the bang button that when presses triggers the VOLTAGE slider to 0.0V (min)
checkbox = controlP5.addCheckBox("Select Hours", 100,200);
checkbox.setColorForeground(color(120));
checkbox.setColorActive(color(255));
checkbox.setColorLabel(color(128));
checkbox.setItemsPerRow(2);
checkbox.setSpacingColumn(80);
checkbox.setSpacingRow(10);
//add items to the checkbox
checkbox.addItem("00:00",0);
checkbox.addItem("01:00",1);
checkbox.addItem("02:00",2);
checkbox.addItem("03:00",3);
checkbox.addItem("04:00",4);
checkbox.addItem("05:00",5);
checkbox.addItem("06:00",6);
checkbox.addItem("07:00",7);
checkbox.addItem("08:00",8);
checkbox.addItem("09:00",9);
checkbox.addItem("10:00",10);
checkbox.addItem("11:00",11);
checkbox.addItem("12:00",12);
checkbox.addItem("13:00",13);
checkbox.addItem("14:00",14);
checkbox.addItem("15:00",15);
checkbox.addItem("16:00",16);
checkbox.addItem("17:00",17);
checkbox.addItem("18:00",18);
checkbox.addItem("19:00",19);
checkbox.addItem("20:00",20);
checkbox.addItem("21:00",21);
checkbox.addItem("22:00",22);
checkbox.addItem("23:00",23);
controlP5.addSlider("PWM",0,255,10,80,10,100); //Adds the vertical slider that responds to the VOLTAGE slider (name,min,max,default,x,y,width,length)
// myTextfield = controlP5.addTextfield("texting",100,160,200,20);
//myTextfield.setFocus(true);
// controlP5.addTextfield("ALARM HOUR",70,100,100,50); //Adds the textbox
//controlP5.addTextfield("ALARM MINUTE",70,200,100,50);
Slider s = controlP5.addSlider("VOLTAGE",0,3.3,0,70,80,100,10); //Horizontal slider that controls voltage from 0 to 3.3 volts
// change sliderMode of the Slider object. The default is Slider.FIX
s.setSliderMode(Slider.FLEXIBLE);
}
//infinite loop
void draw() {
//background(0); // nanotube background
//arduino.analogWrite(9, int("PWM"));
background(myColorBackground);
int s = second(); //Values from 0-59
int m = minute(); //Values from 0-59
int h = hour(); //Values from 0-23
//line(s, 0, s, 33);
//line(m, 33, m, 66);
//line(h, 66, h, 100);
//text(h+":"+m+":"+s, 100, 20);
controlEvent(ControlEvent theEvent());
if(sendData){
text("it works",10,400);
}
}
}
void controlEvent(ControlEvent theEvent) {
sendData = false;
if (theEvent.isGroup()) {
if (theEvent.name()=="myList") {
InitSerial(theEvent.group().value());
}
void controlEvent(ControlEvent theEvent()){
int h = hour();
for(int i=0;i<theEvent.group().arrayValue().length;i++) {
int n = (int)theEvent.group().arrayValue()[i];
print(n);
if(n==1) {
if(((RadioButton)theEvent.group()).getItem(i).internalValue() == h){
sendData = true;
}
}
}
// int n = (int)theEvent.group().arrayValue()[h];
// if (n==1) text("it works",10,400);
}
if(theEvent.isController()) {
print("control event from : "+theEvent.controller().name());
println(", value : "+theEvent.controller().value());
// clicking on SWEET RELEASE sets toggle1 value to 1 (true)
if(theEvent.controller().name()=="SWEET RELEASE") {
controlP5.controller("VOLTAGE").setValue(3.3);
}
// clicking on EMERGENCY CUTOFF sets VOLTAGE to 0 V
if(theEvent.controller().name()=="EMERGENCY CUTOFF") {
controlP5.controller("VOLTAGE").setValue(0.0);
}
// dragging VOLTAGE slider changes the value of PWM slider
if(theEvent.controller().name()=="VOLTAGE") {
controlP5.controller("PWM").setValue(theEvent.controller().value() / 3.3 * 255);
}
}
}
// initialize the serial port selected in the listBox
void InitSerial(float portValue) {
println("initializing serial " + int(portValue) + " in serial.list()"); // for debugging
String portPos = Serial.list()[int(portValue)]; // grab the name of the serial port
txtlblWhichcom.setValue("COM Initialized = " + portPos);
myPorts[0] = new Serial(this, portPos, 57600); // initialize the port
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPorts[0].bufferUntil('\n');
println("done init serial");
}
I'm pretty new to Processing and Java and whatnot. I'm working on a project at the university level that involves an Arduino and a electro-sensitive membrane that allows variable flux when varying voltages are applied. So, I'm using the Arduino and pwm to control the voltage going to the membrane. I can communicate with the Arduino via Bluetooth and Processing. No problem. So, in my program, I have a voltage slider that when changed adjusts the value of a PWM slider. I want to write this PWM slider value to the serial port. No problem, I tested my program with an led and it worked fine. Ok. So now, I decided I needed some sort of timing control so that I could do lab tests using the Arduino controlled membrane. At this point, I wanted to keep everything as simple as possible, so I'm using 24 checkboxes (each one corresponding to a specific hour) with values 0-23. I want the PWM value to be sent when these checkbox values match the current hour in time (its kind of like an alarm, i guess). However, say I have 00:00 checked 01:00 unchecked and 02:00 checked, I want the pwm value to be 0 at 01:00 if that checkbox is not checked. I can get the checkboxes to work with the current hour, but I can't figure out how to turn a previous checkbox off when a new hour comes about. For example, if i had 14:00 checked, 15:00 unchecked, and 16:00 checked, the program works for 14:00 checkbox if the time is 14:00, however if 15:00 isn't checked, the value from 14:00 carries over into the new hour. I don't want this to happen, as this does not allow any sort of timing control. I need to select checkboxes for hours that I want higher than 0 pwm values sent to the arduino, and for the hours that aren't selected I want them to send a pwm=0 value. Also, to conserve battery life, I would like for a way to have the program only send a value to arduino at the moment the hour changes, and then store that value in the Arduino and run pwm in a loop for an hour until the next hour is reached at which a new pwm value is sent. I don't want constant pwm values being sent from processing to the arduino over bluetooth. Also, is there a way to break connection after the serial data is sent, and then reconnect on the hour every hour? I hope you can understand what I'm aiming for here. Can anyone help? My code is below. Note, there are a lot of different things commented that I was playing with that I didn't want to erase quite yet, so pay no attention to them. The main area of focus should be near the if(sendData) statement in the infinite loop. Thanks.
// serial variables
import processing.serial.*; // serial library
//Serial port;
Serial[] myPorts = new Serial[1]; // lets only use one port in this sketch
// GUI variables
import controlP5.*; // controlP5 library
ControlP5 controlP5; // create the handler to allow for controlP5 items
Textlabel txtlblWhichcom; // text label displaying which comm port is being used
Textarea commTextarea; // text area displaying what has been received on the serial port
ListBox commListbox; // list of available comm ports
//PImage bg; //defines bg as the background image
int myColorBackground = color(0,0,0);
int CheckBox;
CheckBox checkbox;
Boolean sendData;
// setup
void setup() {
size(360,500);
frameRate(30);
controlP5 = new ControlP5(this); // initialize the GUI controls
println("Available serial ports:");
println(Serial.list()); // print the comm ports to the debug window for debugging purposes
// make a listbox and populate it with the available comm ports
commListbox = controlP5.addListBox("myList",5,25,120,120); //addListBox(name,x,y,width,height)
commListbox.captionLabel().toUpperCase(false);
commListbox.captionLabel().set("SELECT A COM PORT");
for(int i=0;i<Serial.list().length;i++) {
commListbox.addItem("port: "+Serial.list()[i],i); // addItem(name,value)
}
// text label for which comm port selected
txtlblWhichcom = controlP5.addTextlabel("txtlblWhichcom","No Port Selected",150,25); // textlabel(name,text,x,y)
PFont p = createFont("MS UI GOTHIC",10); //changes the default font of all letters
controlP5.setControlFont(p);
controlP5.addBang("SWEET RELEASE",210,100,20,20); //Adds the bang button that when pressed triggers the VOLTAGE slider to 3.3V (max)
controlP5.addBang("EMERGENCY CUTOFF",80,100,20,20); //Adds the bang button that when presses triggers the VOLTAGE slider to 0.0V (min)
checkbox = controlP5.addCheckBox("Select Hours", 100,200);
checkbox.setColorForeground(color(120));
checkbox.setColorActive(color(255));
checkbox.setColorLabel(color(128));
checkbox.setItemsPerRow(2);
checkbox.setSpacingColumn(80);
checkbox.setSpacingRow(10);
//add items to the checkbox
checkbox.addItem("00:00",0);
checkbox.addItem("01:00",1);
checkbox.addItem("02:00",2);
checkbox.addItem("03:00",3);
checkbox.addItem("04:00",4);
checkbox.addItem("05:00",5);
checkbox.addItem("06:00",6);
checkbox.addItem("07:00",7);
checkbox.addItem("08:00",8);
checkbox.addItem("09:00",9);
checkbox.addItem("10:00",10);
checkbox.addItem("11:00",11);
checkbox.addItem("12:00",12);
checkbox.addItem("13:00",13);
checkbox.addItem("14:00",14);
checkbox.addItem("15:00",15);
checkbox.addItem("16:00",16);
checkbox.addItem("17:00",17);
checkbox.addItem("18:00",18);
checkbox.addItem("19:00",19);
checkbox.addItem("20:00",20);
checkbox.addItem("21:00",21);
checkbox.addItem("22:00",22);
checkbox.addItem("23:00",23);
controlP5.addSlider("PWM",0,255,10,80,10,100); //Adds the vertical slider that responds to the VOLTAGE slider (name,min,max,default,x,y,width,length)
// myTextfield = controlP5.addTextfield("texting",100,160,200,20);
//myTextfield.setFocus(true);
// controlP5.addTextfield("ALARM HOUR",70,100,100,50); //Adds the textbox
//controlP5.addTextfield("ALARM MINUTE",70,200,100,50);
Slider s = controlP5.addSlider("VOLTAGE",0,3.3,0,70,80,100,10); //Horizontal slider that controls voltage from 0 to 3.3 volts
// change sliderMode of the Slider object. The default is Slider.FIX
s.setSliderMode(Slider.FLEXIBLE);
}
//infinite loop
void draw() {
//background(0); // nanotube background
//arduino.analogWrite(9, int("PWM"));
background(myColorBackground);
int s = second(); //Values from 0-59
int m = minute(); //Values from 0-59
int h = hour(); //Values from 0-23
//line(s, 0, s, 33);
//line(m, 33, m, 66);
//line(h, 66, h, 100);
//text(h+":"+m+":"+s, 100, 20);
controlEvent(ControlEvent theEvent());
if(sendData){
text("it works",10,400);
}
}
}
void controlEvent(ControlEvent theEvent) {
sendData = false;
if (theEvent.isGroup()) {
if (theEvent.name()=="myList") {
InitSerial(theEvent.group().value());
}
void controlEvent(ControlEvent theEvent()){
int h = hour();
for(int i=0;i<theEvent.group().arrayValue().length;i++) {
int n = (int)theEvent.group().arrayValue()[i];
print(n);
if(n==1) {
if(((RadioButton)theEvent.group()).getItem(i).internalValue() == h){
sendData = true;
}
}
}
// int n = (int)theEvent.group().arrayValue()[h];
// if (n==1) text("it works",10,400);
}
if(theEvent.isController()) {
print("control event from : "+theEvent.controller().name());
println(", value : "+theEvent.controller().value());
// clicking on SWEET RELEASE sets toggle1 value to 1 (true)
if(theEvent.controller().name()=="SWEET RELEASE") {
controlP5.controller("VOLTAGE").setValue(3.3);
}
// clicking on EMERGENCY CUTOFF sets VOLTAGE to 0 V
if(theEvent.controller().name()=="EMERGENCY CUTOFF") {
controlP5.controller("VOLTAGE").setValue(0.0);
}
// dragging VOLTAGE slider changes the value of PWM slider
if(theEvent.controller().name()=="VOLTAGE") {
controlP5.controller("PWM").setValue(theEvent.controller().value() / 3.3 * 255);
}
}
}
// initialize the serial port selected in the listBox
void InitSerial(float portValue) {
println("initializing serial " + int(portValue) + " in serial.list()"); // for debugging
String portPos = Serial.list()[int(portValue)]; // grab the name of the serial port
txtlblWhichcom.setValue("COM Initialized = " + portPos);
myPorts[0] = new Serial(this, portPos, 57600); // initialize the port
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPorts[0].bufferUntil('\n');
println("done init serial");
}
1