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 Test Code

3.19.2.1 Code Flow

6-19-2-1

3.19.2.2 Build Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-19-tableLamp.sb3

Manually build blocks:

  1. In events, find j1 block.

  2. In control, find and place j2 block under j1.

6-1-4-1-1

  1. In variable, find j7 and put it under j1, name the variable to “item“ and assign to “1” as its initial value.

6-19

  1. In control, put j25 into j2.

  2. In operator, find and put j22 into the condition box of j25

6-19

  1. In , find j8 and put it in the left part of j22, and set the right part to value 3500: AD key analog value > 3500

6-19

  1. In control, find and put block into 6-19.

  2. In operator, put j29 into the condition box of

  3. In variable, put j8 into the left part of the j29, and modify the right part into “1”: variable item = 1

6-19

  1. In , drag and put in “if” of block 6-19

  2. In variable, put j8 under

  3. Duplicate and put into “else” of block 6-19, and set LED to output “LOW” and set item variable by “1”.

  4. Add a delay of 0.3s below .

Complete Test Code

6-19-2-2-8

3.19.2.3 Test Result

After uploading code, press the red button and the red LED lights up; press it again and the LED goes off.