How to make a switch in a circuit?

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

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().

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.

Thanks so much for the example

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

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

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:

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;
}