Why won't my rectangle stay where it is when I press button?

Hopefully the title is descriptive enough to get my point across. I’ve been working on building an rov for a while and wanted a rectangle in my GUI to indicate whether or not the LEDs are on. I’m also using a PS4 controller and the game control plus library with everything pinned on my arduino mega. I have LedOn is the square button on my controller and LedOff is the X button. What I want is to press square and have the Leds turn on, then press X to have them turn off. I have that down, it works fine, but what I want now is a small rectangle in my GUI that indicates whether they are on by turning red and green and saying On or Off. My code below hopefully shows where the problem is, when I pressed the square button the LED turns on and stays on like I want but the box only appears when the button is pressed. I want the box to stay there all the time, what am I missing? Thanks!

This is just the part where I turn my LEDs on and off, and where I’ve put the box that I want to appear. I didn’t post the entire code because its hundreds of lines long.

 if (LedOn == true) {
      arduino.digitalWrite(6, Arduino.HIGH);
      fill(0, 100, 0);
      rect(10, 210, 175, 100, 5);
      fill(255);
      textSize(30);
      text("LED ON", 95, 267.5);
    }
    if(LedOff == true) {
      arduino.digitalWrite(6, Arduino.LOW);
      fill(100, 0, 0);
      rect(10, 210, 175, 100, 5);
      fill(255);
      textSize(30);      
      text("LED OFF", 95, 267.5);      
    }
    else {
      fill(100, 0, 0);
      rect(10, 210, 175, 100, 5);
      fill(255);
      textSize(30);
      text("LED OFF", 95, 267.5);
    }
  }
1 Like

This would happen when the code section would be in keyPressed function which is called only once

I don’t know where your code section is located but it belongs into draw()

The else-block is not really necessary

Instead of using 2 variables, consider using one, named LED;

Then you can just say true or false, meaning on or off

1 Like