Have you ever tried to use the Chrome browser when you have no network connectivity? You may be surprised to find the following in this situation.

This is the Chrome Dino game! (You can also get to it at anytime by typing chrome://dino into your Chrome URL bar.) To play this side-scroller, you press space bar to begin, and then use the space bar to jump over obstacles in the form of cacti and pterodactyls.

The Proximity Trinkey comes with the APDS9960 proximity sensor built in. You can combine this proximity sensor with the Adafruit CircuitPython HID library to send HID commands to your computer using the sensor as an input. This means you can write CircuitPython code that makes the Proximity Trinkey send a spacebar command to your computer when your hand gets to a certain distance from the proximity sensor and play the Dino game using your Trinkey!

APDS9960 Proximity Sensor Location

The APDS9960 proximity sensor is located towards the opposite end of the board from the USB connector, between the NeoPixel LEDs.

Playing the Dino Game

The APDS9960 library is already built into CircuitPython for the Proximity Trinkey, as is the NeoPixel library. However, one dependency for the APDS9960 library is not built in, and therefore must be loaded manually onto your board. You will also need to manually load the Adafruit CircuitPython HID library.

First, you must install the library dependency and the HID library.

Then you need to update code.py.

Click the Download Project Bundle button below to download the necessary library dependency and the code.py file in a zip file. Extract the contents of the zip file. From inside the lib/ folder in the Project Bundle, copy only the entire adafruit_register/ and adafruit_hid/ folders to the /lib folder on your CIRCUITPY drive. Then, copy the code.py file to your CIRCUITPY drive.

If you try to copy the entire Project Bundle lib folder, you will run out of space on your board. Copy ONLY the entire adafruit_register/ and adafruit_hid/ folders to the /lib folder on your CIRCUITPY drive.

Your CIRCUITPY drive contents should resemble the image.

You should have in / of the CIRCUITPY drive:

  • code.py

And in the lib folder on your CIRCUITPY drive:

  • adafruit_hid/
  • adafruit_register/
"""
Proximity spacebar dino game example. Sends a space when you move your hand close to the proximity
sensor and turns the LEDs on to let you know you're in the right range. For use with the Chrome
Dino game, reachable in Chrome with chrome://dino or when you have no network connectivity.
"""
import board
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import neopixel
from adafruit_apds9960.apds9960 import APDS9960

apds = APDS9960(board.I2C())
pixels = neopixel.NeoPixel(board.NEOPIXEL, 2)
keyboard = Keyboard(usb_hid.devices)

apds.enable_proximity = True

space = False
while True:
    print(apds.proximity)
    current_proximity = apds.proximity
    if current_proximity > 100 and not space:
        pixels.fill((255, 0, 0))
        keyboard.send(Keycode.SPACE)
        space = True
    elif current_proximity < 50 and space:
        pixels.fill(0)
        space = False

Now, open the Dino game (or a text editor) and move your hand closer to the Proximity Trinkey. It will send a spacebar command to your computer (and turn on the NeoPixel LEDs red to indicate that you're close enough to activate sending). Move your hand away from the Trinkey and back again to send another spacebar. Now, jump that cactus!

First, you import the necessary modules and libraries. Then you setup the APDS9960, the NeoPixel LEDs, and the keyboard, and enable the proximity feature on the APDS9960.

Before the loop, you create a space variable to track the status of the spacebar press, and set it to False.

Inside the loop, you print the proximity value to the serial console. Then you set current_proximity equal to the proximity value.

Then you have two checks. First, you check to see if the proximity value is greater than 100 AND the space variable is False. If both conditions are valid, you do three things: turn on the NeoPixels to red, send a spacebar command to your computer, and set the space variable to True. Second, you check to see if the proximity value is less than 50 AND the space variable is True. If both conditions are valud, you do two things: turn off the NeoPixel LEDs, and set the space variable to False. That means you can begin again!

That's all there is to using Proximity Trinkey and Adafruit CircuitPython HID to play the Chrome Dino game!

This guide was first published on Sep 29, 2021. It was last updated on 2021-09-29 11:19:56 -0400.

This page (Proximity Spacebar Game) was last updated on Oct 08, 2021.

Text editor powered by tinymce.