3.6 AD Button

3.6.1 Overview

The AD button requires only one analog pin to read multiple button states, which greatly saves IO ports. It adopts analog acquisition, and the output voltages vary from pressed buttons, so that different analog values can be obtained. We can determine which button is pressed according to these values.

3.6.2 Schematic Diagram

6-6-2

From the Schematic Diagram:

When no button is pressed, the output at signal pin IO33 is pulled down by R34 (connected to GND. So the analog value of IO33 is 0, that is, low level 0V;

When button S1(the red button) is pressed, pin IO33 is connected to VCC. So the analog value of IO33 is 4095 (voltage = 3.3V).

When button S2(the yellow button) is pressed, the voltage of IO33 is that between R32 and R34: VCCxR34/(R32+R34) ≈ 2.2V, and the analog value is about 2432;

When button S3(the green button) is pressed, the voltage of IO33 is that between R32+R33 and R34: VCCxR34/(R32+R33+R34) ≈ 1.1V, and the analog value is about 1175.

3.6.3 Code Blocks

  1. j21

    This block reads the analog value of AD button module and determines which button is pressed according to this value.

  2. j7

    This block declares variables. Its type is int by default. You can input a Name and an Assignment(initial value).

  3. j8

    This block records a value of a variable so that we can directly use it to replace the value. Before using, you only need to modify its name into the needed variable.

  4. j9

    This block assigns a value to a variable. For example, we need to assign 100 to variable val, just input the variable name and value in the block accordingly.

  5. j22

    This is a comparison block that compares the values on both sides. If the left one is greater than the right one, output 1; or else, output 0.

  6. j23

    Similarly, this is also a comparison block. If the left one is lower than the right one, output 1; or else, output 0.

  7. j24

    This is a logical block with two condition boxes. Only when both sides output 1, this block outputs 1. One or both of the two sides output 0, and this block will output 0.

  8. j25

    This “if…then” block determines conditions. Put condition(s) in the diamond box to output 1 or 0 (as above introduced). If 1 is output, execute the following codes; If it is 0, do not execute.

3.6.4 Test Code

3.6.4.1 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-6-adKey.sb3

Manually build blocks:

  1. In events, find j1 block.

  2. In serial, find j12 block and set baud rate to 9600, and put it under j1.

  3. In control, find and drag block j7 under 6-4-4-1-1.

  4. In control, put j2 under

  5. In control, put j9 into j2

  6. In adKey, find j21 and put it into the variable name box in j9

    6-6

  7. In serial, place j13 under6-6

  8. In control, put j8 intoj13

  9. At last, add a delay of 0.3S for better observing results.

Complete Test Code

6-6

3.6.4.3 Test Result

After uploading code, the serial monitor will print the analog value of the module. When the red button is pressed, the value is about 4095, and the yellow is about 2432, and the green is about 1175. If no button is pressed, the value equals 0.

6-6

3.6.5 Extension

3.6.5.1 Code Flow

AD button logic table:

Button

the analog value of button being pressed

the analog value of button being released

the red button

4095

0

the yellow button

about 2432

0

the green button

about 1175

0

6-6

3.6.5.2 Test 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-6-adKey2.sb3

Manually build blocks:

Based on 3-6-adKey.sb3, we add a condition statement to determine the read analog value of AD button module so that we can tell which button is pressed.

6-6

  1. Remove block 6-6: just click Delete

  2. In control, put j25 under 6-6

  3. In operator, put j22 into the condition box of j25

6-6

  1. In control, put j8into the left part of j22, and input 3500 in the right: itme > 3500

6-6

  1. In serial, put j13 into the “if…then” block 6-6 and modify the printing content into “Red”, so as to determine whether the red button is pressed.

6-6

  1. In control, drag j25 under 6-6

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

  3. In operator, find block j22, and put j8 in its left part and modify the right part to 3000: item < 3000

  4. In operator, find block j22, and put j8 in its left part and modify the right part to 3000: item > 2000

  5. Put 6-6 into j25 respectively.

    Add a j13 under j25, and modify the printing content into “Yellow”.

6-6

  1. Duplicate image-6-6 and modify 3000 to 1500, and 2000 to 100, and printing content into “Green”:

6-6

Complete Test Code

6-6-5-2

3.6.5.3 Test Result

After uploading code, the serial monitor will print the colors of the buttons. If one of the buttons is pressed, its color will be displayed: “Red”, “Green” or “Yellow”.

6-6