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

t75

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

Blocks in :

  1. t76 is a block included in coding box library. It reads the status value of the AD button in the coding box. Pressing is a high level (1), and releasing is a low level (0). Click on the to switch buttons.

    t77

3.6.4 Test Code

You can manually build blocks, or directly open the code file we provide: 3-6-ADKey.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 , drag t52 block and put it into , and tap t53 to add some else-ifs as follows.

t78

  1. In , drag 3 t76 and put it on the t79 of t52. Set the first to red, the second to yellow and the third to green. Tap t80 to remove “else”.

t81

  1. In , drag three t44 blocks and put them in t52. Press the red button to output “Press the red button”, yellow: “Press the yellow button” and green: “Press the green button”.

Complete code:

t82

3.6.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 it outputs Press the red button; Press the yellow button and it says Press the yellow button; Press the green button it outputs Press the green button.

t83