ControlP5 contol toggles

please format code with </> button * homework policy * asking questions

I’m trying to make a panel of toggles that can be affected/changed by buttons, like setting multiple toggles’ state from false to true using one button, while still being able to use individual toggles.

Here is how I am making each toggle:

cp5.addToggle("one")    
    .setPosition(100, 100)  
    .setSize(200, 200)    
    .setFont(font)
    .setValue(false)
    .setColorBackground(selectColor)
    .setColorForeground(basicColor)
    .setColorActive(toggleColor);

Here is how I am checking each toggle:

void one(boolean theFlag){
  if (theFlag == true)
  {
    port.write('A');
  }
  
  if (theFlag == false)
  {
    port.write('a');
  }
}

I want to be able to change the state of several toggles with one button, not just setting it to a default state.

When I try cp5.getController("one").setValue(cp5.getController("one").getDefaultValue()); I can set each toggle to false, as this is the default state, but when I try `cp5.getController(“one”).setValue(true)’ it says it won’t take a boolean, despite the default value returning a boolean.

How do I change these toggles “remotely?”

Hi @PaladinBobcat ,

Welcome to the forum! :slight_smile:

So, if I understood correctly, the following code is what you are trying to achieve.
So you can toggle the toggle buttons individually and when you click on the setter button, it sets all the toggle buttons to true state!

import controlP5.*;

ControlP5 cp5;
Toggle togglers[];
Button button;

int col = color(255);

boolean toggleValue = false;

void setup() {
  size(400, 400);
  smooth();

  cp5 = new ControlP5(this);

  togglers = new Toggle[3];
  // create a toggle and change the default look to a (on/off) switch look
  togglers[0] = cp5.addToggle("toggle1")
    .setPosition(40, 100)
    .setSize(50, 20)
    .setValue(true)
    .setMode(ControlP5.SWITCH)
    ;
  togglers[1] = cp5.addToggle("toggle2")
    .setPosition(40, 200)
    .setSize(50, 20)
    .setValue(true)
    .setMode(ControlP5.SWITCH)
    ;
  togglers[2] = cp5.addToggle("toggle3")
    .setPosition(40, 300)
    .setSize(50, 20)
    .setValue(true)
    .setMode(ControlP5.SWITCH)
    ;

  button = cp5.addButton("setter")
    .setValue(0)
    .setPosition(300, 200)
    .setSize(50, 19)
    ;
}


void draw() {
  background(0);
}

void mousePressed() {
  if (button.isPressed()) {
    for (int i = 0; i < 3; i++) {
      togglers[i].setValue(true);
    }
  }
}

Hope it helps guide you!

1 Like