3.19 Button Control LED

3.19.1 Overview

In this project, we control the ON/OFF of the LED via an AD button. The LED will light up if we press the button and it goes off when we press the button again.

3.19.2 Code Flow

t150

3.19.3 Code Blocks

Blocks in :

  1. t149 is used to determine whether two values are equal. If yes, it returns true; otherwise, it returns false.

For more details, please visit Blocks Reference | MicroBlocks Wiki

3.19.4 Test Code

You can manually build blocks, or directly open the code file we provide: 3-19-Button Control LED.ubp. If you have any questions about how to open code files or upload code, please back to 1.9 Upload Code.

Build code blocks:

  1. In , drag and to the script area, and stack them together.

t34

  1. In , declare a variable named item.

  2. t52 determines whether the red button is pressed.

t152

  1. Check if item = 0. If yes, red LED turns on and set item to 1 . If not, red LED turns off and set item to 0. At last, add a delay of 300ms.

t153

3.19.5 Test Result

Connect the coding box to the MicroBlocks via USB or Bluetooth, and click t59 to upload the code to the coding box. Press the red button and the red LED lights up; press it again and the LED goes off.