# How to make a switch in a circuit?

**URL:** https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603
**Category:** Coding Questions
**Created:** [November 16, 2018, 4:51pm UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603 "2018-11-16T16:51:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![saotome](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@saotome](https://discourse.processing.org/u/saotome)
#### Post date: [November 16, 2018, 4:51pm UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603/1 "2018-11-16T16:51:39Z")

</div>

Hello.

I have this "circuit!

> void setup() {  
> size(600, 600);  
> }
> 
> void draw() {  
> ellipse (300, 150, 40, 40);  
> line (280, 150, 120, 150);  
> line (120, 150, 120, 220);  
> line (120, 220, 120, 290); //closed circuit  
> line (120, 290, 120, 380);  
> line (80, 220, 120, 270); // open circuit  
> line (320, 150, 500, 150);  
> line (500, 150, 500, 250);  
> line (500, 250, 500, 380);  
> line (500, 380, 350, 380);  
> rect (250, 350, 100, 50);  
> line (250, 380, 120, 380);  
> ellipse (500, 250, 40, 40);  
> line (270, 320, 325, 425);  
> }

I need to know hot change the switch from open to close and viceversa using space key. How I can hide closed line by the open one and the opposite.

Thanks so much

---

<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: [November 16, 2018, 4:56pm UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603/2 "2018-11-16T16:56:50Z")

</div>

Hi,

You can add a boolean variable to keep track of the state of your circuit.

Don’t forget to clear the screen before drawing it with `background()`.

```auto
boolean isOpen;

void setup() {
  size(600, 600);
  isOpen = true;
}

void draw() {
  background(200); // CLear the screen
  ellipse (300, 150, 40, 40);
  line (280, 150, 120, 150);
  line (120, 150, 120, 220);
  if (!isOpen) {
    line (120, 220, 120, 290); //closed circuit
  }
  line (120, 290, 120, 380);
  if (isOpen) {
    line (80, 220, 120, 270); // open circuit
  }
  line (320, 150, 500, 150);
  line (500, 150, 500, 250);
  line (500, 250, 500, 380);
  line (500, 380, 350, 380);
  rect (250, 350, 100, 50);
  line (250, 380, 120, 380);
  ellipse (500, 250, 40, 40);
  line (270, 320, 325, 425);
}

void mouseClicked() {
  isOpen = !isOpen;
}

```

It is currently the mouse click that will open or close the circuit. It will be your homework to make it with the space bar.

---

<div class="post-metadata">

### Author: ![saotome](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@saotome](https://discourse.processing.org/u/saotome)
#### Post date: [November 16, 2018, 5:11pm UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603/3 "2018-11-16T17:11:49Z")

</div>

Thanks so much for the example

---

<div class="post-metadata">

### Author: ![saotome](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@saotome](https://discourse.processing.org/u/saotome)
#### Post date: [November 17, 2018, 10:39am UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603/4 "2018-11-17T10:39:56Z")

</div>

I’m an absoliute begginer and I have some difficulties, the goal of this class is when the circuit closes, bulb

> ellipse (500, 250, 40, 40);

Should “light on”, ie. it will filled with white, and when circuit opens bulb turns back black. I tried with fill but has been impossible.

Many thanks

---

<div class="post-metadata">

### Author: ![saotome](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@saotome](https://discourse.processing.org/u/saotome)
#### Post date: [November 17, 2018, 10:48am UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603/5 "2018-11-17T10:48:47Z")

</div>

I have this sketch, maybe could be useful

> void setup() {  
> Vpila = 10.0; // Battery voltage in volts  
> Rl = 100.0; // Bulb resistance in Ohms  
> Rv=500.0; // Initial variable resistance in Ohms  
> DR=5.; // Incremental step of variable resistance in ohms  
> color\_alpha= 0; // Initial transparency in zero
> 
> start = false; // Closed switch  
> }  
> //  
> // In draw() animations are implemented  
> //  
> void draw() {  
> if (start) { // Close switch.  
> I = Vpila/Req; // Intensity in the circuit, Ohm’s LawLlei  
> color\_alpha = 255\*I/Imax; // Brightness is proportional to intensity using alpha transparency  
> }
> 
> //  
> // For changing the value of variable resistance we use UP for increasing resistance  
> //  
> void keyPressed() { //  
> if (key == CODED & keyCode == UP ) {  
> Rv = Rv+DR;  
> }  
> }
> 
> // Mouse click closes swith and simulation begins  
> void mouseClicked() {  
> start = true;  
> }  
> // End

---

<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: [November 17, 2018, 11:07am UTC](https://discourse.processing.org/t/how-to-make-a-switch-in-a-circuit/5603/6 "2018-11-17T11:07:32Z")

</div>

Well, you actually wrote what you should do.  
If the circuit is open: fill it black else fill it white.

You already have all you need in the previous code. You just need to add that if statement:

```auto
boolean isOpen;

void setup() {
  size(600, 600);
  isOpen = true;
}

void draw() {
  background(200); // CLear the screen
  
  fill(255);
  ellipse (300, 150, 40, 40);
  
  line (280, 150, 120, 150);
  line (120, 150, 120, 220);
  line (120, 290, 120, 380);
  
  if (isOpen) {
    line (80, 220, 120, 270); // open circuit
  } else {
    line (120, 220, 120, 290); //closed circuit
  }
  
  line (320, 150, 500, 150);
  line (500, 150, 500, 250);
  line (500, 250, 500, 380);
  line (500, 380, 350, 380);
  rect (250, 350, 100, 50);
  line (250, 380, 120, 380);
  
  if (isOpen) {
    fill(0);
  } else {
    fill(255);
  }
  ellipse (500, 250, 40, 40);
  
  line (270, 320, 325, 425);
}

void mouseClicked() {
  isOpen = !isOpen;
}

```
