short problems or issues that frequently arise

1. Syntax

Why does the interpreter choke on this code?

# standard libraries
import time

# circuitpython included library
import simpleio


MORSE_CHARS = {
    "A":(dot, dash),
    "B":(dash, dot, dot, dot),
    "C":(dash, dot, dash, dot),
    "D":(dash, dot, dot),
    "E":(dot,),
    "F":(dot, dot, dash, dot),
    # ... and lots more
}

def morse_char(character):
    if character == " ":
    time.sleep(wspace - lspace)

    else:
        for t in MORSE_CHARS[character]:
            simpleio.tone(PIEZO_PIN, MORSE_PITCH, duration=t)
            time.sleep(dot)

2. Syntax

Why does the interpreter choke on this code?

import board
import digitalio as dio

import pwmio
from adafruit_motor import motor

# Initialize buttons as digital inputs
button1 = dio.DigitalInOut(board.GP20)
button1.direction = dio.Direction.INPUT
button2 = dio.DigitalInOut(board.GP21)
button2.direction = dio.Direction.INPUT

# DC motor setup
M1A = pwmio.PWMOut(board.GP8), frequency=1_000)
M1B = pwmio.PWMOut(board.GP9, frequency=1_000)
motor1 = motor.DCMotor(M1A, M1B

# variables to remember our status
speed = 0
last_speed = speed

3. Setup

  • What does the author of this code intend to have happen?

  • Why does it not work?

# standard libraries
import time

# circuitpython included library
import analogio


adc = analogio.AnalogIn(board.GP0)

while True:
    pot = adc.value
    position = pot / 65535

    print(position)
    time.sleep(0.2)

4. Setup

switch matrix

The below code is to setup reading a matrix keypad.

  • What does the author of this code intend to have happen? Assume the pin numbers match the wiring.

  • Why does it not work?

import board
import digitalio

col1 = digitalio.DigitalInOut(board.GP4)
col2 = digitalio.DigitalInOut(board.GP5)
col3 = digitalio.DigitalInOut(board.GP6)

row1 = digitalio.DigitalInOut(board.GP0)
row2 = digitalio.DigitalInOut(board.GP1)
row3 = digitalio.DigitalInOut(board.GP2)
row4 = digitalio.DigitalInOut(board.GP3)

while True:
    # set a column pin low
    # read the rows to see if any are low
    pass   # avoid a Python syntax error since there are no statements here otherwise

There are two missing ideas from this code.

5. Logic

  • What does the author of this code intend to have happen? Assume the pin numbers match the wiring.

  • Why does it not work?

out = compute_output(x, y, z)

#
# limit out's value to +- 1.0 range
#
if out < 1.0:
    out = 1.0

if out > -1.0:
    out = -1.0