greetings - and how to solve 2.2.5 project, picobutton as toggle to turn on and off the LED?

hello,

i am thinking i want to QUERY the status of the LED light.

1 when picobricks button is pressed,

2 IF LED is on, then TURN it OFF.

3 ELSEif LED is OFF, then TURN it ON.


how do i code 2,3 in microblocks? anyone, anyone?


rangsarn

picobricks newbie uncle

======from the projects book===

2.2.5. Project Proposal

In this project, the LED turns on when the button is pressed, and the LED turns off when the button is released. You can write the necessary codes for the LED to turn on when the button is pressed once and to turn the LED off when it is pressed again.

Comments

  • Hi;

    You can find all code in GitHub. I am sending the folder where this project is located.

    https://github.com/Robotistan/PicoBricks/tree/main/Examples/Activities/Action-Reaction

  • @rangsarn

    PB button block reports its state at the instant it is executed. Thus, when the button is pressed it is true, and when released it is false.

    To do what you mentioned in #2 and #3, we need to intercept just the "press" phase of the event and wait until the "not-pressed" phase. Then we can change the state of a boolean variable to its opposite. eg: boolean var LED initialized to true.

    To implement this:

    • we define a variable LED = true
    • we use an WHEN event block and check for PB button = true (pressed)
    • WAIT until PB button is false (not pressed)
    • and change LED to not(LED) ... boolean opposite
    • Finally we can use this final state of the LED variable to set the red led status.

    See sample code below:

    Enjoy.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!