The code is a relatively short CircuitPython program. You can learn about CircuitPython at this link to the Adafruit Learning System.
You can copy it from the page or download it from the GitHub link. Save it onto a drive on your local computer as code.py.
To load the code as-is, plug your Circuit Playground Board to your computer via the USB cable. You should see a new flash drive called CIRCUITPY in your list of available drives. If you don't see it, press the tiny Reset button on the Circuit Playground Express.
In the off-chance your board does not have CircuitPython preloaded (usually indicated by the board showing up as CPLAYBOOT and not CIRCUITPY), follow the instructions here to load the latest version. Now press Reset and you should see a drive named CIRCUITPY.
Drag a copy of code.py or otherwise copy the file to the CIRCUITPY drive. The program should run immediately.
If you wish to modify and interact with the code, Adafruit suggests using the free Mu editor. You can download Mu and learn more by following this guide. Mu also includes an interactive REPL to show the code is working.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT # Nintendo R.O.B. control with Accelerometer, code in CircuitPython # Using an Adafruit Circuit Playground Express board with an IR LED # Anne Barela for Adafruit Industries, MIT License, May, 2018 # Acknowledgement to info at http://atariage.com/forums/topic/177286 # -any-interest-in-nes-rob-homebrews/ and Limor Ladyada Fried import time import gc from digitalio import DigitalInOut, Direction from adafruit_circuitplayground.express import cpx import board # Commands, each 8 bit command is preceded by the 5 bit Init sequence Init = [0, 0, 0, 1, 0] # This must precede any command Up = [1, 0, 1, 1, 1, 0, 1, 1] # Move arms/body down Down = [1, 1, 1, 1, 1, 0, 1, 1] # Move arms/body up Left = [1, 0, 1, 1, 1, 0, 1, 0] # Twist body left Right = [1, 1, 1, 0, 1, 0, 1, 0] # Twist body right Close = [1, 0, 1, 1, 1, 1, 1, 0] # Close arms Open = [1, 1, 1, 0, 1, 1, 1, 0] # Open arms Test = [1, 1, 1, 0, 1, 0, 1, 1] # Turns R.O.B. head LED on print("R.O.B. Start") # Circuit Playground Express IR LED Setup IRled = DigitalInOut(board.REMOTEOUT) IRled.direction = Direction.OUTPUT # This function performs the LED flashing of a passed command # Note timing is very tight. Machine instructions are 2-5 ms or so # so that is why the time for cycles doing work is < 16.66667 ms (1/60) # Each pulse/space is 1.5 milliseconds out of 16.7 total ms (NTSC) def IR_Command(cmd): gc.collect() # collect memory now # Output initialization and then command cmd for val in Init+cmd: # For each value in initial+command if val: # if it's a one, flash the IR LED IRled.value = True # Turn IR LED turn on for 1.5 ms time.sleep(0.0015) # 1.5 ms on IRled.value = False # Turn IR LED off for 15 ms time.sleep(0.0150) # 15 ms else: time.sleep(0.0167) # 1 cycle turn off while True: # Main Loop poll switches, do commands x, y, z = cpx.acceleration # Read accelerometer print((x, y, z)) # Print for debug if x > 5 and y > -8: # Clockwise from back IR_Command(Right) # Turn Right if x < -5 and y > -8: # Counterclockwise from back IR_Command(Left) # Turn Left if x > 1 and y < -10: # Move direction of USB IR_Command(Up) # Body up if x < -1 and y < -10: # Move in direction of battery con IR_Command(Down) # Body Down if cpx.button_a: IR_Command(Open) # Button A opens arms if cpx.button_b: IR_Command(Close) # Button B closes arms if cpx.switch: # Toggle switch turns "test" on IR_Command(Test) # which is the LED on R.O.B.s head time.sleep(0.1)
What the code is doing
The code loads libraries for using various functions on the Circuit Playground Express board. Then the definitions for the R.O.B. control bit codes are defined: the Init
code all commands use and the 8 bit codes unique to various functions: Up
, Down
, Left
, Right
, Close
, Open
, and Test
(LED).
The function IR_Command
takes one of the commands above, appends the Init sequence, and turns on and off the IR LED on the Circuit Playground Express according to the timings discussed on page Light Hacking. If the bit is zero, the function delays 1/60 of a second. If the bit is a one, the LED is turned on for 1.5 milliseconds, turned off, and the program waits 15 milliseconds. 15 + 1.5 = 16.5, the other time is used by the code.
The while True:
loop (exactly like a loop()
function in Arduino) keeps polling the Circuit Playground Express inputs and outputs commands appropriately.
How each input is used will be explained in the next section.
Page last edited January 21, 2025
Text editor powered by tinymce.