Toggle Button Dependency

Hello everyone!

I have a program with some toggles running, and I want a 'Main" Button to be able to reset the toggles to their initial position/value.

Is there a way to do this? and if so how should I go about it.

Thanks in advance.

What do you mean by this? I am assuming you have some boolean variables in your program. If you want to reset them to their initial value then, you can just store their initial values somewhere and when the main button gets pressed you can just reset them one by one.

Hi,
If you don’t give us any code to work with, it will be really hard to help you.
There are thousand of ways to code toggles…

Let’s say you have a couple of boolean variables then write a function resetMe() where you say c1=true; c2=false; etc.

Hello,

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate, filter and use them.

This is a very short list:

Resources < Click here to expand !

I encourage you to review the resources available here:

:)

a toggle function isnt as straightforward as one may think or perhaps perhaps thats just me. If you already have the toggle function then its as simple as linking a function to an event, ie mousePressed, mouseReleased etc. Further down the line you may look at using reflections if you want to access the bool using a string.

as a quick hint if you are looking to code your own toggle function then you’ll probably need three bools to achieve the desired effect, or you can increment an integer, which recycles back to 0 if greater than 2. Also you’ll want to consider how the toggle is activated, again is it on mousePressed, mouseReleased etc, as this will change how your function is coded.

Thanks everyone for your reply!

I am using cp5 to create toggle buttons.

I have a flag that when true executes a series of steps and when false executes another series of events.

What I want is for this flag to be reset to the initial value when a ‘master/main’ button is pressed.

boolean a = true;
void mousePressed(){
   a = false;
};

@paulgoux, could you eleborate a bit more. (I am just beginning to play around with processing so I am still figuring out things).

void setup()
{
      //your code here
}

void draw()
{
     //your code here
} 

void keyPressed()
{
     if(key == 'r' || key == 'R')
     {
           //reset whatever variables you want back to their default values.
           int blabla = 4;
           int yippetyYap = 7;
           boolean idfk = false;
           boolean evenMoreWackyNames = false;
           String ILovePotatos = "POTATOS";
     }
}

If you want your reset key to be a special key(not an alphanumeric character) then the if statement will look like this:

void keyPressed()
{
     if(keyCode == ENTER)
     {
            //I don't feel like typing more stupid variable names here, but you get the point.
     }
}

and one last tip that I spent an hour on myself - the space key is not keyCode == SPACE but is instead key == ' '

1 Like

I still have a hard time understanding why you can’t share your code.

It is nice to explain the main idea but it won’t replace your actual working code.

And if you don’t want to post your code because it is top secret or something, it’s fine, but at least make the effort of creating an MCVE. People are not here to do the code for you.

@paulgoux’s answer is as good as it can get without you showing the structure of your code: it shows you the logic that you should have.

So you have a flag.

And you have a cp5 button. (Or are both the same?)

Upon the main/reset button please set the flag back to false (1) and also set the cp5 button back (2). To find out the command, check the reference of the library.

Do you have difficulties with 1 or with 2?

Chrisir

@jb4x sorry about that, my code is somewhat long and there are many unnecessary lines that some of you guys could say are not necessary.

however, as you suggested here is an example code of what my program does:

void setup() {

  size (1500, 900); // size of main window x and y
  port = new Serial(this, "COM6", 9600); // CHANGE THE COM PORT VALUE TO YOUR COM (check arduino)

  cp5 = new ControlP5(this);

  fontTitle = createFont("Rockwell Bold", 50); //custom font and size
  fontHeading = createFont("Rockwell", 35); //custom font and size
  fontButton = createFont("Rockwell", 23); //custom font and size
  fontCirc = createFont("Rockwell Bold", 20); //custom font and size
  fontallOn = createFont("Rockwell", 30); //custom font and size

  int w1 = 300;
  int h1 = 150;

  cp5.addToggle("led1") //name of button
    .setSize(60, 60) //width and height
    .setPosition(w1, h1) //x and y coordinate of upper left corner
    .setFont(fontButton) //font of button
    .setCaptionLabel("1")
    .setValue(false)
    .setMode(ControlP5.SWITCH)
    ;
  cp5.addToggle("led2") //name of button
    .setSize(60, 60) //width and height
    .setPosition(w1+110, h1) //x and y coordinate of upper left corner
    .setFont(fontButton) //font of button
    .setCaptionLabel("2")
    .setValue(false)
    .setMode(ControlP5.SWITCH)    
    ;
  cp5.addButton("OFF") //name of button
    .setPosition(550, 700) //x and y coordinate of upper left corner
    .setSize(75, 75) //width and height
    .setFont(fontButton) //font of button
    ;
}

void draw() {
  background (50, 50, 50); //background color of the main window
  fill (255, 255, 255);  //color of title of window
  textFont(fontTitle); //font style of title of window
  textAlign(CENTER);
  text("LED CONTROL SYSTEM", 750, 60); //"text" and x and y coordinate of text

///////////////////////////////////////////////
  int circX = 1050;
  int circY1 = 130;
  //int circY2 = 310;

  /////////////////////////////////////
  //drawing the circles
  /////////////////////////////////////
  fill(col1);
  ellipse(circX, circY1, 35, 35);

  fill(col2);
  ellipse(circX + 50, circY1 + 25, 35, 35);

  /////////////////////////////////////
  //creating the labels for the circles
  /////////////////////////////////////
  textFont(fontCirc);
  textAlign(CENTER);
  fill(colC1);
  text("1", circX, circY1 + 7);

  fill(colC2);
  text("2", circX + 50, circY1 + 32);
}

void led1(boolean theFlag) {
  if (theFlag == true) {
    col1 = color(0, 255, 0);
    values[0] = "1";
    colC1 = color(0, 0, 0);
  } else {
    col1 = color(255, 0, 0);
    messege[0] = "-1";
    colC1 = color(255, 255, 255);
  }
}

void led2(boolean theFlag) {
  if (theFlag == true) {
    col2 = color(0, 255, 0);
    values[1] = "2";
    colC2 = color(0, 0, 0);
  } else {
    col2 = color(255, 0, 0);
    messege[1] = "-2";
    colC2 = color(255, 255, 255);
  }
}

void OFF() {
  port.write("100");

  for (int i = 0; i < 12; i++) {
    values[i] =  "null";
    println(values);
    led1Toggle.setValue(false); // this is what I am having trouble with
    col1 = color(255, 0, 0);
    col2 = color(255, 0, 0);
    colC1 = color(255, 255, 255);
    colC2 = color(255, 255, 255);
  }
}

The last few lines is where I am having a bit of trouble

@carlos.411

Your code is not complete or minimal.

Work through the examples that come with CP5:

:)

1 Like

Hey everyone I finally figured it out with the help of everyone.

void OFF() {
  port.write("100");

  for (int i = 0; i < 12; i++) {
    values[i] =  "null";
    println(values);
    cp5.getController("led1").setValue(cp5.getController("led1").getDefaultValue());
    col1 = color(255, 0, 0);
    col2 = color(255, 0, 0);
    colC1 = color(255, 255, 255);
    colC2 = color(255, 255, 255);
  }
}

so cp5.getController(“led1”).setValue(cp5.getController(“led1”).getDefaultValue()); is what I was missing, pretty simple I guess. Thanks

1 Like