controlP5.slider tied to controlP5.knob only wants to work right one direction

54 Views Asked by At

I've got the following Processing code using controlP5 modules:

import controlP5.*;
ControlP5 cp5;

void setup() {
  size(180,240);
  smooth();
  noStroke();
  
  cp5 = new ControlP5(this);
  
   cp5.addSlider("CCV_Control")
               .setPosition(40,40)
               .setNumberOfTickMarks(11)
               .setSliderMode(Slider.FLEXIBLE)
               .showTickMarks(false)
               .snapToTickMarks(true)
               .setSize(100,20)
               .setRange(0,100)
               .setValue(20)
               .setLabel("CCV Percent Open");
  cp5.getController("CCV_Control").getCaptionLabel().align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE).setPaddingY(2);
   cp5.addKnob("Water_Flow")
               .setRange(0,70)
               .setValue(20)
               .setPosition(40,100)
               .setRadius(50)
               .setNumberOfTickMarks(28)
               .snapToTickMarks(true)
               .setLabel("Facility Water");
}

void draw() {
  fill(140);
  rect(20,20,140,200);
}

void Water_Flow(int theValue) {
  float ccvPercentage = 0;
  if (theValue <= 2) {
    cp5.getController("Water_Flow").setColorBackground(color(200, 0, 0));
    cp5.getController("CCV_Control").setValue(0);
  } else {
    cp5.getController("Water_Flow").setColorBackground(color(0, 31, 63)); 
    ccvPercentage = (theValue*110)/70;
    cp5.getController("CCV_Control").setValue(ccvPercentage);
  }
}

void CCV_Control(int theValue) {
  float facilityWaterFlow = 0;
  if (theValue <= 2) {
     //cp5.getController("Water_Flow").setValue(0);
 } else {
    facilityWaterFlow = (theValue*70)/100;
    //cp5.getController("Water_Flow").setValue(facilityWaterFlow);
  }
  println("Water Flow: "+facilityWaterFlow+ " is CCV percent: "+theValue);
}

It works fine, but I need these two control to be tied to each other in both directions. But when I uncomment the 'getController("Water_Flow")' lines the logs are flooded with errors. Is there an easier way to tie them together?

2

There are 2 best solutions below

0
On

Here's the updated code based on apodidae's response:

void Water_Flow(int theValue) {
  float ccvPercentage = 0;
  if (theValue <= 2) {
    cp5.getController("Water_Flow").setColorBackground(color(200, 0, 0));
  } else {
    cp5.getController("Water_Flow").setColorBackground(color(0, 31, 63)); 
    ccvPercentage = (theValue*110)/70;
  }
  // temporarily turn off broadcasting for controller "CCV_Control"
  boolean broadcast = cp5.getController("Water_Flow").isBroadcast();
  cp5.getController("CCV_Control").setBroadcast(false);
  cp5.getController("CCV_Control").setValue(ccvPercentage);
  // change broadcast back to initial value
  cp5.getController("CCV_Control").setBroadcast(broadcast);
}

void CCV_Control(int theValue) {
  float facilityWaterFlow = 0;
  color knobBackground = color(0, 31, 63);
  if (theValue <= 2) {
    knobBackground = color(200, 0, 0);
 } else {
    facilityWaterFlow = (theValue*70)/100;
  }
  // temporarily turn off broadcasting for controller "Water_Flow"
  boolean broadcast = cp5.getController("Water_Flow").isBroadcast();
  cp5.getController("Water_Flow").setBroadcast(false);
  cp5.getController("Water_Flow").setValue(facilityWaterFlow);
  cp5.getController("Water_Flow").setColorBackground(knobBackground);
  // change broadcast back to initial value
  cp5.getController("Water_Flow").setBroadcast(broadcast);

  println("Water Flow: "+facilityWaterFlow+ " is CCV percent: "+theValue);
}
0
On

A slight variation in syntax which might shorten your code a little:

//https://forum.processing.org/one/topic/controlp5-error-java-lang-noclassdeffounderror-could-not-initialize-class-java-util-logging-logrecor.html

import controlP5.*;

ControlP5 cp5;

Knob myKnob;
Slider mySlider;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);
  mySlider = cp5.addSlider("slider")
    .setPosition(100, 50)
    .setRange(0, 255)
    .setHeight(24)
    .setWidth(200)
    .setValue(50);
  ;
  myKnob = cp5.addKnob("knob")
    .setRange(0, 255)
    .setValue(50)
    .setPosition(100, 140)
    .setNumberOfTickMarks(28)
    .setRadius(50)
    .setDragDirection(Knob.VERTICAL)
    ;
}

void draw() {
}

void slider(float sliderValue) {
  if (myKnob != null) {
    boolean broadcast = myKnob.isBroadcast();
    myKnob.setBroadcast(false);
    myKnob.setValue(sliderValue);
    myKnob.setBroadcast(broadcast);
  }
  println("sliderValue = ", sliderValue);
}

void knob(float knobValue) {
  if (mySlider != null) {
    boolean broadcast = mySlider.isBroadcast();
    mySlider.setBroadcast(false);
    mySlider.setValue(knobValue);
    mySlider.setBroadcast(broadcast);
  }
  println("knob value = ", knobValue);
}