BBC micro:bit VKey Voltage Keypad (MicroPython)

Introduction

The Vkey Voltage Keypad is a keypad made by Sparkfun and costs around £10. That's quite expensive for a keypad but there is a twist. This keypad outputs an analogue voltage that changes depending on which button is pressed. You get 12 extra buttons and only need to use one analogue pin to read them.

Connecting

There are only 3 connections to make here. Programming

This is Luca's code. He worked out all of the individual voltages using the Block Editor code on this page, wrote them down and then coded the MicroPython version you see here.

from microbit import *

def nleds(value):
img = Image('00000:'*5)
sp =  img.set_pixel
counter = 0
for row in range(0,5):
for col in range(0,5):
if counter<value:
sp(col,row,9)
else:
sp(col,row,0)
counter += 1
return img

while True:
if reading > 50 and reading < 80:
num = 12
elif reading > 110 and reading < 150:
num = 11
elif reading > 170 and reading < 210:
num = 10
elif reading > 230 and reading < 270:
num = 9
elif reading > 300 and reading < 340:
num = 8
elif reading > 360 and reading < 400:
num = 7
elif reading > 420 and reading < 460:
num = 6
elif reading > 480 and reading < 520:
num = 5
elif reading > 550 and reading < 590:
num = 4
elif reading > 610 and reading < 650:
num = 3
elif reading > 670 and reading < 710:
num = 2
elif reading > 730 and reading < 770:
num = 1
else:
num = 0
display.show(nleds(num))

The number of LEDs that are lit on the display matches the number of the button pressed.

Adapting Luca's code, we can add a buzzer on pin 0. Connect the + of the buzzer to pin 0 and the - of the buzzer to GND. The notes chosen here are from a blues scale and should be a nice selection to improvise a tune with - if you are in any way musical.

There are 13 notes in the list. The 0 in position 0 is there as a place holder. All of the other notes match the position of the button that is going to trigger them. This reduces the number of statements that we have to write.

from microbit import *
import music

notes = [0,262,294,311,349,370,440,466,523,587,622,698,740]

def nleds(value):
img = Image('00000:'*5)
sp =  img.set_pixel
counter = 0
for row in range(0,5):
for col in range(0,5):
if counter<value:
sp(col,row,9)
else:
sp(col,row,0)
counter += 1
return img

while True:
if reading > 50 and reading < 80:
num = 12
elif reading > 110 and reading < 150:
num = 11
elif reading > 170 and reading < 210:
num = 10
elif reading > 230 and reading < 270:
num = 9
elif reading > 300 and reading < 340:
num = 8
elif reading > 360 and reading < 400:
num = 7
elif reading > 420 and reading < 460:
num = 6
elif reading > 480 and reading < 520:
num = 5
elif reading > 550 and reading < 590:
num = 4
elif reading > 610 and reading < 650:
num = 3
elif reading > 670 and reading < 710:
num = 2
elif reading > 730 and reading < 770:
num = 1
else:
num = 0
display.show(nleds(num))
if num==0:
music.stop()
else:
music.pitch(notes[num], -1)
sleep(20)

Challenges

1. Make a calculator using the keypad. You have a couple of extra buttons free to use as operators. You can also bring the A and B buttons into play.
2. Use the keypad as a controller for a Whack-A-Mole game. Periodically and randomly light up individual LEDs on the matrix in a 3x4 grid. If the player hits the button that matches the position of a lit LED, they hit the mole. Make it so the moles appear and disappear at odd times (use running time to decide when to do this).
3. Use the number pad in any project that requires the user to be able to type in a number with more than one digit.
4. Map the numbers to letters (like on what now seems like a terribly old-fashioned mobile phone). This would give you a decent method for text entry. Start by simply scrolling what the user enters and then work up to something more involved.