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
0 -
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.
1
Howdy, Stranger!
Categories
- 119 All Categories
- 3 Community
- 3 Hello Maker!
- 103 PicoBricks - Make, Run, Split, Merge
- 1 PicoBricks IDE
- 1 Arduino IDE
- 32 Hardware Support
- 15 PicoBricks Backers Hub
- 49 MicroBlocks Support
- 3 Shipping Support
- 2 MicroPython
- 1 REX - 8 in 1
- Mechanical Parts and Parkour
- REX Main
- 1 Arduino-IDE
- REX - Shipping
- 12 Products & Technology
- 2 Raspberry Pi
- Arduino
- 10 Other