A dictionary keeps values under named or numeric keys, making robot code easier to read and change. Use a key to retrieve the setting, hardware object, color, button name, or axis name that it represents. section 3.5 section 2.3 section 7.5.3
Use dictionaries to separate readable names from the values a program needs:
- Store right and left motor power adjustments under 'R' and 'L', then retrieve both the adjustment and motor by the same name. section 3.5
- Store RGB hexadecimal values under color names, then loop through the dictionary to create a multicolor LED effect. section 2.3
- Map joystick button and axis numbers to readable labels; use get for a button or axis that has no defined name. section 7.5.3
Adjust motors by name
Keep the motor object and its calibration value in separate dictionaries keyed by 'R' and 'L'. A function receiving a motor name can select the object and multiply its throttle by the matching adjustment, letting one function control either wheel. section 3.5
Listing 3.2 envforward.py: Reading configuration values from environment variables
#!/usr/bin/env python3
from adafruit_crickit import crickit
import time
import os
DC_ADJUST_R = float(os.environ.get('ROBO_DC_ADJUST_R', '1'))
DC_ADJUST_L = float(os.environ.get('ROBO_DC_ADJUST_L', '1'))
ADJUST = dict(R=DC_ADJUST_R, L=DC_ADJUST_L)
MOTOR = dict(R=crickit.dc_motor_1, L=crickit.dc_motor_2)
def set_throttle(name, value):
MOTOR[name].throttle = value * ADJUST[name]
def forward():
set_throttle('R', 0.9)
set_throttle('L', 0.9)
time.sleep(0.2)
set_throttle('R', 0)
set_throttle('L', 0)
forward()
This runnable script combines environment-based adjustment values with ADJUST and MOTOR dictionaries, then drives both motors through a shared function. section 3.5









































