Both buttons are wired exactly like the single button in the blinking lab. This lab reads them independently and keeps a running count for each, printed centered on the circle instead of tucked into a corner.
Sample Program Code
Every line gets its strip erased before the new count is written, so a shorter number never leaves a stray digit behind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 | # Lab 17: Reading Two Buttons
# Both buttons on this kit are wired the same way as the single button in
# the Blinking lab: PULL_UP inputs that read 1 when idle and 0 when
# pressed, because the other leg of each button goes to GND. This lab
# reads them independently and shows a running count for each.
#
# Text lines are centered rather than left-aligned at x=4. On a round
# screen a left margin is not a straight line -- how far in the text has
# to start depends on how far down the screen it is.
import config
from utime import sleep
display = config.init_display()
button_a, button_b = config.init_buttons()
WHITE = config.WHITE
BLACK = config.BLACK
FONT = config.SMALL_FONT
FONT_WIDTH = FONT.WIDTH
FONT_HEIGHT = FONT.HEIGHT
HALF_WIDTH = config.WIDTH // 2
TITLE_Y = 70
A_ROW_Y = 110
B_ROW_Y = 140
def centered(string, y):
x = HALF_WIDTH - (len(string) * FONT_WIDTH) // 2
# Erase the whole strip first, so a shorter number does not leave a
# digit from the longer one behind it.
display.fill_rect(0, y, config.WIDTH, FONT_HEIGHT, BLACK)
display.text(FONT, string, x, y, WHITE, BLACK)
def pressed(button):
if button.value() == 1:
return False
sleep(0.02) # debounce: let the contacts settle
return button.value() == 0
def wait_for_release(button):
while button.value() == 0:
sleep(0.01)
def show_counts(a_count, b_count):
centered("A (GP14): " + str(a_count), A_ROW_Y)
centered("B (GP15): " + str(b_count), B_ROW_Y)
display.fill(BLACK)
centered("Two Buttons", TITLE_Y)
a_count = 0
b_count = 0
show_counts(a_count, b_count)
while True:
if pressed(button_a):
a_count += 1
show_counts(a_count, b_count)
wait_for_release(button_a)
if pressed(button_b):
b_count += 1
show_counts(a_count, b_count)
wait_for_release(button_b)
sleep(0.01)
# Things to try:
#
# 1. Take the fill_rect() out of centered() and count past 9. The 1 from
# "10" sits on top of the old digit, because nothing erased it. On a
# display with no frame buffer, text does not replace -- it overprints.
#
# 2. The driver's text() takes a background color, and it really does
# paint that background behind each character. Try passing WHITE as
# the background for one row to see it.
|
Here's the starting screen, before either button is pressed:

Text Overprints — It Does Not Replace
With no frame buffer behind it, this driver has no concept of "replace what was there." Draw the string "9" where "10" used to be and the leading 1 simply stays, because nothing told the display to paint over it. centered() in this lab erases the whole text strip first, every time, specifically to guard against that.
Take that fill_rect() out and count a button past nine. Watching the old digit survive underneath the new one is a fast, memorable way to feel the difference between a buffered display and this one.
Watch Out
Forgetting to erase before you redraw text is the single most common bug people bring from the OLED kit — there, it just worked, because fill() reset everything on the next frame.