E21 Computer Engineering Fundamentals
October 6, 2026
Modify the code so that it follows the state transition diagram Figure 1.
from adafruit_circuitplayground.express import cpx
import time
state = 1
while True:
time.sleep(0.5)
# Define what each state looks like
if state == 1:
cpx.pixels[5] = (0,0,50)
elif state == 2:
cpx.pixels[5] = (0,50,0)
# Define transitions
if state == 1 and cpx.button_a:
state = 2
continue
if state == 2 and cpx.button_a:
state = 1
continue


Interpret the following state-transition diagram. What kind of system could it represent?

Inputs:
1: Power Button Pressed0: Power Button not pressed
Helpful even when there’s no input
Traffic lights

FSM for U.S. traffic lights and U.K. traffic lights

01 arrows)10 arrows)00)
It is customary to use binary bits to label each input
00: Reset01: Increment10: DecrementIf there are \(p\) inputs, need \(k\) bits to encode these inputs where \(2^k \ge p > 2^{k-1}\)
It is also customary to use binary bits to label each state
000: \(S_0\)001: \(S_1\)010: \(S_2\)011: \(S_3\)100: \(S_4\)101: \(S_5\)Run the following code to implement a FSM for a 4-step combination lock on the Circuit Playground Express.
Press some combination of A and B to unlock your device. All four lights green = unlocked!
from adafruit_circuitplayground.express import cpx
import time
state = 1
def wait_till_release_A():
while cpx.button_a:
print("release button to complete transition")
time.sleep(0.5)
def wait_till_release_B():
while cpx.button_b:
print("release button to complete transition")
time.sleep(0.5)
while True:
time.sleep(0.05)
# Define what each state looks like
if state == 1:
# Locked state
for k in range(0,4):
cpx.pixels[k] = (50,0,0)
elif state == 2:
# One correct entry
for k in range(1,4):
cpx.pixels[k] = (50,0,0)
cpx.pixels[0] = (0,50,0)
elif state == 3:
# Two correct entry
for k in range(2,4):
cpx.pixels[k] = (50,0,0)
for k in range(0,2):
cpx.pixels[k] = (0,50,0)
elif state == 4:
# Three correct entry
cpx.pixels[3] = (50,0,0)
for k in range(0,3):
cpx.pixels[k] = (0,50,0)
elif state == 5:
# Four correct entry -- unlocked !
for k in range(0,4):
cpx.pixels[k] = (0,50,0)
if state == 1:
if cpx.button_a:
state = 2
print("Button A pressed! moving from state 1 to state 2")
wait_till_release_A()
elif cpx.button_b:
state = 1
print("Button B pressed! moving from state 1 to state 1")
wait_till_release_B()
if state == 2:
if cpx.button_a:
state = 1
print("Button A pressed! moving from state 1 to state 2")
wait_till_release_A()
elif cpx.button_b:
state = 3
print("Button B pressed! moving from state 1 to state 1")
wait_till_release_B()
if state == 3:
if cpx.button_a:
state = 1
print("Button A pressed! moving from state 1 to state 2")
wait_till_release_A()
elif cpx.button_b:
state = 4
print("Button B pressed! moving from state 1 to state 1")
wait_till_release_B()
if state == 4:
if cpx.button_a:
state = 5
print("Button A pressed! moving from state 1 to state 2")
wait_till_release_A()
elif cpx.button_b:
state = 1
print("Button B pressed! moving from state 1 to state 1")
wait_till_release_B()
Draw the state transition diagram for this
ABBA opens the lock.

E21 • Fall 2026 • Lecture 11 • October 6, 2026 • ↩︎