# Toggle Button Dependency

**URL:** https://discourse.processing.org/t/toggle-button-dependency/22755
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [July 21, 2020, 3:59am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755 "2020-07-21T03:59:04Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![carlos.411](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@carlos.411](https://discourse.processing.org/u/carlos.411)
#### Post date: [July 21, 2020, 3:59am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/1 "2020-07-21T03:59:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![CodingIsLife](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@CodingIsLife](https://discourse.processing.org/u/CodingIsLife)
#### Post date: [July 21, 2020, 5:47am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/2 "2020-07-21T05:47:03Z")

</div>

> [@carlos.411](#):
>
> I have a program with some toggles running

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.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 21, 2020, 6:36am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/3 "2020-07-21T06:36:33Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 21, 2020, 10:46am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/4 "2020-07-21T10:46:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 21, 2020, 11:03am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/5 "2020-07-21T11:03:58Z")

</div>

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:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

`:)`

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [July 21, 2020, 1:34pm UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/6 "2020-07-21T13:34:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![carlos.411](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@carlos.411](https://discourse.processing.org/u/carlos.411)
#### Post date: [July 21, 2020, 10:13pm UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/7 "2020-07-21T22:13:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [July 21, 2020, 10:58pm UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/8 "2020-07-21T22:58:30Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![carlos.411](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@carlos.411](https://discourse.processing.org/u/carlos.411)
#### Post date: [July 22, 2020, 1:45am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/9 "2020-07-22T01:45:48Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Edgard142ofWelshdale](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Edgard142ofWelshdale](https://discourse.processing.org/u/Edgard142ofWelshdale)
#### Post date: [July 22, 2020, 4:36am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/10 "2020-07-22T04:36:50Z")

</div>

```auto
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:

```auto
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 == ' '`

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 22, 2020, 6:21am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/11 "2020-07-22T06:21:06Z")

</div>

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](https://stackoverflow.com/help/minimal-reproducible-example). People are not here to do the code for you.

> [@carlos.411](#):
>
> @paulgoux, could you eleborate a bit more. (I am just beginning to play around with processing so I am still figuring out things).

@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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 22, 2020, 7:40am UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/12 "2020-07-22T07:40:40Z")

</div>

> [@carlos.411](#):
>
> What I want is for this flag to be reset to the initial value when a ‘master/main’ button is pressed.

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

---

<div class="post-metadata">

### Author: ![carlos.411](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@carlos.411](https://discourse.processing.org/u/carlos.411)
#### Post date: [July 22, 2020, 4:03pm UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/13 "2020-07-22T16:03:12Z")

</div>

@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:

```auto
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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 22, 2020, 5:09pm UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/14 "2020-07-22T17:09:40Z")

</div>

@carlos.411

Your code is not complete or minimal.

Work through the examples that come with CP5:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c1a1d60cfcdbaaa4976aa8c6be8258ea32a75af3.png)

`:)`

---

<div class="post-metadata">

### Author: ![carlos.411](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@carlos.411](https://discourse.processing.org/u/carlos.411)
#### Post date: [July 22, 2020, 5:24pm UTC](https://discourse.processing.org/t/toggle-button-dependency/22755/15 "2020-07-22T17:24:50Z")

</div>

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

```auto
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
