cmpt_401_portfolio

Lab Assignment 5

Oct. 18, 2023

Assignment project 1:

Description:

Create a system where pressing a joystick button displays a message on screen, and then having the ability to move that message in all four direction using the directional input from the joystick.

List of hardware components:

Component Amount
ESP32-S3-WROOM 1
GPIO Extension Board 1
Project Board 1
4-Digit 7-Segment Display 1
74HC595 Chip 1
Passive Buzzer 2
NPN Transistor 2
Resistor - 1K Ω 2
Resistor - 220 Ω 8
Wire M-F 5
Wire M-M 25

Circuit diagram:

Photo of breadboard:

Breadboard view:

Source code:

from machine import Pin, ADC, PWM
from my74HC595 import Chip74HC595
import time

xVal=ADC(Pin(14))                       # connect joystick pins
yVal=ADC(Pin(13))
zVal=Pin(12,Pin.IN,Pin.PULL_UP)

xVal.atten(ADC.ATTN_11DB)               # analog to digital conversion for y and x
yVal.atten(ADC.ATTN_11DB)
xVal.width(ADC.WIDTH_12BIT)
yVal.width(ADC.WIDTH_12BIT)

pB1 = PWM(Pin(9),2000)                  # connect buzzer pins
pB2 = PWM(Pin(10),1000)

pB1.deinit()                            # turn off initial sound
pB2.deinit()

comPin = [47,35,36,21]
plane = [                               # animation states of 7 segment displays
    [[0x9f, 0x9e, 0xff, 0xff],[0xff, 0x9f, 0x9e, 0xff],[0xff, 0xff, 0x9f, 0x9e],[0x9e, 0xff, 0xff, 0x9f]],
    [[0xc6, 0x86, 0xff, 0xff],[0xff, 0xc6, 0x86, 0xff],[0xff, 0xff, 0xc6, 0x86],[0x86, 0xff, 0xff, 0xc6]],
    [[0xaf, 0xa7, 0xff, 0xff],[0xff, 0xaf, 0xa7, 0xff],[0xff, 0xff, 0xaf, 0xa7],[0xa7, 0xff, 0xff, 0xaf]],
    [[0xf6, 0xf6, 0xff, 0xff],[0xff, 0xf6, 0xf6, 0xff],[0xff, 0xff, 0xf6, 0xf6],[0xf6, 0xff, 0xff, 0xf6]]
]

pos = [1,1,False]                       # helper variable

def switchFrame(p):
    print("yVal:",yVal.read(),"xVal:",xVal.read())
    moveTo = p
    started = p[2]
    if yVal.read() < 1000:              # if pointing up
        if not started:
            started = True
            pB1.init()
        pB1.freq(262)                   # play C(4)
        if p[0] != 0:                   # move animation up
            moveTo = [p[0]-1,p[1]]
        else:
            moveTo = [3,p[1]]
    elif yVal.read() > 3000:            # if pointing down
        if not started:
            started = True
            pB1.init()
        pB1.freq(392)                   # play G(4)
        if p[0] != 3:                   # move animation down
            moveTo = [p[0]+1,p[1]]
        else:
            moveTo = [0,p[1]]
    else:
        started = False
        pB1.deinit()
    if xVal.read() < 1000:              # if pointing left
        if not started:
            started = True
            pB2.init()
        pB2.freq(220)                   # play A(3)
        if p[1] != 0:                   # move animation left
            moveTo = [p[0],p[1]-1]
        else:
            moveTo = [p[0],3]
    elif xVal.read() > 3000:            # if pointing right
        if not started:
            started = True
            pB2.init()
        pB2.freq(349)                   # play F(4)
        if p[1] != 3:                   # move animation right
            moveTo = [p[0],p[1]+1]
        else:
            moveTo = [p[0],0]
    else:
        started = False
        pB2.deinit()                    # if not pointing, turn off buzzer
    moveTo = [moveTo[0],moveTo[1],started]
    print("moveTo:",moveTo)
    return moveTo

def led_display(p):
    for i in range(0,4):
        chns=Pin(comPin[i],Pin.OUT)
        chns.value(1)
        chip.shiftOut(0,p[pos[0]][pos[1]][i])   # display LED
        time.sleep_ms(1)
        chns.value(0)
        chip.shiftOut(0,0xff)

chip = Chip74HC595(38,39,40)            # identify 74hc595 chip

MAX = 20                                # subsequent refresh rate
counter = 0                             # and its counter

try:
    while True:
        if counter == MAX:
            pos = switchFrame(pos)
            counter = 0
        else:
            counter += 1
        led_display(plane)
except Exception as e:
    print("Error:", e)

Video:

 

Back to top

Assignment project 2:

Description:

Create a system where pressing one button begins an animation and plays music continueously. When another button is pressed, pause the animation and music, until it is restarted by the first button again.

List of hardware components:

Component Amount
ESP32-S3-WROOM 1
GPIO Extension Board 1
Project Board 2
NPN Transistor 2
Passive Buzzer 2
Big Push Button 2
74HC595 IC Chip 2
Wire M-M 49
Resistor - 220 Ω 8
Resistor - 10k Ω 4
Resistor - 1K Ω 2

Circuit diagram:

Photo of breadboard:

Breadboard view:

Source code:

from machine import Pin, PWM
from my74HC595 import Chip74HC595
import time

# initialize buttons and buzzers
btn_a=Pin(3,Pin.IN,Pin.PULL_UP)
btn_b=Pin(46,Pin.IN,Pin.PULL_UP)
pB1 = PWM(Pin(13),2000) # for treble
pB2 = PWM(Pin(14),1000) # for bass

pB1.deinit()            # turn off initial sound
pB2.deinit()            #  "
    
def C(oct):             # freq for C
    if oct == 0:
        return 16
    if oct == 1:
        return 33
    if oct == 2:
        return 65
    if oct == 3:
        return 131
    if oct == 4:
        return 262
    if oct == 5:
        return 523
    if oct == 6:
        return 1047
    if oct == 7:
        return 2093
    if oct == 8:
        return 4186
def D(oct):             # freq for D
    if oct == 3:
        return 147
    if oct == 4:
        return 294
    if oct == 5:
        return 587
def D_(oct):            # freq for D#
    if oct == 5:
        return 622
def E(oct):             # freq for E
    if oct == 3:
        return 165
    if oct == 4:
        return 330
    if oct == 5:
        return 659
def F(oct):             # freq for F
    if oct == 3:
        return 175
    if oct == 4:
        return 349
    if oct == 5:
        return 699
def F_(oct):            # freq for F#
    if oct == 3:
        return 185
    if oct == 4:
        return 370
    if oct == 5:
        return 740
def G(oct):             # freq for G
    if oct == 2:
        return 98
    if oct == 3:
        return 196
    if oct == 4:
        return 392
    if oct == 5:
        return 784
def G_(oct):            # freq for G#
    if oct == 3:
        return 208
    if oct == 4:
        return 415
def A(oct):             # freq for A
    if oct == 3:
        return 220
    if oct == 4:
        return 440
    if oct == 5:
        return 880
def A_(oct):            # freq for A#
    if oct == 3:
        return 233
    if oct == 4:
        return 466
def B(oct):             # freq for B
    if oct == 3:
        return 247
    if oct == 4:
        return 494

marioNotes = [          # the first 14 stansas from "Super Mario Bros: Main Theme"
    [E(5),E(3)],[],[0,0],[E(5),E(3)],[],[0,0],
    [],[],[],[E(5),E(3)],[],[0,0],
    [],[],[],[C(5),E(3)],[],[0,0],
    [E(5),E(3)],[],[],[],[],[0,0],
    
    [G(5),G(3)],[],[],[],[],[0,0],
    [],[],[],[],[],[],
    [G(4),G(2)],[],[],[],[],[0,0],
    [],[],[],[],[],[],
    # 48
    [C(5),G(3)],[],[],[],[],[0,0],
    [],[],[],
    [G(4),E(3)],[],[],[],[],[0,0],
    [],[],[],
    [E(4),C(3)],[],[],[],[],[0,0],
    
    [],[],[],
    [A(4),F(3)],[],[],[],[],[0,0],
    [B(4),G(3)],[],[],[],[],[0,0],
    [A_(4),F_(3)],[],[0,0],
    [A(4),F(3)],[],[],[],[],[0,0],
    
    [G(4),E(3)],[],[],[0,0],
    [E(5),C(4)],[],[],[0,0],
    [G(5),E(4)],[],[],[0,0],
    [A(5),F(4)],[],[],[],[],[0,0],
    [F(5),D(4)],[],[0,0],[G(5),E(4)],[],[0,0],
    
    [],[],[],
    [E(5),C(4)],[],[],[],[],[0,0],
    [C(5),A(3)],[],[0,0],[D(5),B(3)],[],[0,0],
    [B(4),G(3)],[],[],[],[],[0,0],
    [],[],[],
    # 144
    [0,C(3)],[],[],[],[],[0,0],
    [G(5),0],[],[0,0],[F_(5),G(3)],[],[0,0],
    [F(5),0],[],[0,0],[D_(5),0],[],[],
    [1,C(4)],[],[0,1],[E(5),1],[],[0,0],
    
    [0,F(3)],[],[0,1],[G_(4),1],[],[0,0],
    [A(4),0],[],[0,0],[C(5),C(4)],[],[0,0],
    [0,C(4)],[],[0,1],[A(4),1],[],[0,0],
    [C(5),F(3)],[],[0,1],[D(5),1],[],[0,0],
    
    [0,C(3)],[],[],[],[],[0,0],
    [G(5),0],[],[0,0],[F_(5),G(3)],[],[0,0],
    [F(5),0],[],[0,0],[D(5),0],[],[1,0],
    [1,G(3)],[],[0,0],[E(5),C(4)],[],[0,0],
    
    [],[],[],[C(6),G(4)],[],[1,1],
    [],[],[0,0],[C(6),G(4)],[],[0,0],
    [C(6),G(4)],[],[],[],[],[0,0],
    [0,G(3)],[],[],[],[],[0,0],
    
    [0,C(3)],[],[],[],[],[0,0],
    [G(5),0],[],[0,0],[F_(5),G(3)],[],[0,0],
    [F(5),0],[],[0,0],[D_(5),0],[],[1,0],
    [1,C(4)],[],[0,1],[E(5),1],[],[0,0],
    
    [0,F(3)],[],[],[G_(4),1],[],[0,0],
    [A(4),0],[],[0,0],[C(5),C(4)],[],[0,0],
    [0,C(4)],[],[],[A(4),1],[],[0,0],
    [C(5),F(3)],[],[0,1],[D(5),1],[],[0,0],
    
    [0,C(3)],[],[],[],[],[0,0],
    [D_(5),G_(3)],[],[],[],[],[0,0],
    [],[],[],[D(5),A_(3)],[],[],
    [],[],[0,0],[],[],[],
    
    [C(5),C(4)],[],[],[],[],[0,0],
    [],[],[],[0,G(3)],[],[0,0],
    [0,G(3)],[],[],[],[],[0,0],
    [0,C(3)],[],[],[],[],[0,0]
]

incre = 0       # counting helper

def Play(inc):  # play song in array format
    print(inc,marioNotes[inc])
    if len(marioNotes[inc]) == 2:
        if not marioNotes[inc][0] == 0 and not marioNotes[inc][0] == 1:
            pB1.init()
            pB1.freq(marioNotes[inc][0])
        elif not marioNotes[inc][0] == 1:
            pB1.deinit()
        if not marioNotes[inc][1] == 0 and not marioNotes[inc][1] == 1:
            pB2.init()
            pB2.freq(marioNotes[inc][1])
        elif not marioNotes[inc][1] == 1:
            pB2.deinit()

def setBlank(): # clear LED matrix
    blank=[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    for j in range(100):
        cols=0x01
        for i in range(8):
            chip.disable()
            chip.shiftOut(1,blank[i])
            chip.shiftOut(1,~cols)
            time.sleep_us(500)
            cols<<=1
            chip.enable()

lvlDesign = [   # animtion of entire mario level  -  frame
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, # 1
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, # 2
    0x90, 0x80, 0x80, 0x90, 0x90, 0x92, 0x90, 0x90, # 3
    0x80, 0xC0, 0x80, 0x80, 0x80, 0xE0, 0x80, 0x80, # 4
    0x80, 0xE0, 0x80, 0x80, 0x80, 0xE0, 0x80, 0x80, # 5
    0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, # 6
    0x80, 0x90, 0x90, 0x90, 0x82, 0x82, 0x82, 0x82, # 7
    0x82, 0x82, 0x82, 0x02, 0x00, 0x80, 0x80, 0x82, # 8
    0x82, 0x82, 0x92, 0x80, 0x80, 0x80, 0x80, 0x90, # 9
    0x90, 0x80, 0x80, 0x80, 0x80, 0x90, 0x80, 0x80, #10
    0x92, 0x80, 0x80, 0x90, 0x80, 0x80, 0x80, 0x80, #11
    0x90, 0x80, 0x82, 0x82, 0x82, 0x80, 0x80, 0x80, #12
    0x82, 0x92, 0x92, 0x82, 0x80, 0x80, 0xC0, 0xE0, #13
    0xF0, 0x80, 0x80, 0xF0, 0xE0, 0xC0, 0x80, 0x80, #14
    0x80, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0x00, 0x00, #14
    0xF0, 0xE0, 0xC0, 0x80, 0x80, 0x80, 0xC0, 0x80, #15
    0x80, 0x90, 0x90, 0x90, 0x90, 0x80, 0x80, 0x80, #16
    0x80, 0x80, 0xC0, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, #17
    0xFE, 0xFE, 0x80, 0x80, 0x80, 0x80, 0xFF, 0x81, #18
    0x80, 0x80, 0xF0, 0xFC, 0x9C, 0xFC, 0xF0, 0x80, #19
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, #20
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80  #21
]

chip = Chip74HC595(38,39,40,41)     # chip id

paused = False      # pause button helper

replay1 = True      # song repitition part helper 1
replay2 = True      # song repitition part helper 2

try:
    setBlank()                                              # start blank
    while True:
        if not btn_a.value():                               # wait for start button
            pause = True
            while True:
                for i in range(168):                        # start animation
                    Play(incre)                             # play particular note
                    incre += 1
                    if incre == 144 and replay1:            # song control 1
                        replay1 = False
                        incre = 48
                    if incre == 336 and replay2:            # song control 2
                        replay2 = False
                        incre = 144
                    elif incre == 336 and not replay2:      # song control 3
                        replay1 = True
                        replay2 = True
                        incre = 48
                    for k in range(5):                      # continue animation sequence
                        cols=0x01
                        for j in range(i,8+i):              # continue animation sequence
                            chip.disable()
                            chip.shiftOut(1,lvlDesign[j])
                            chip.shiftOut(1,~cols)
                            cols<<=1
                            chip.enable()
                            if not btn_b.value():           # listen for pause button
                                setBlank()                  # turn things off
                                pB1.deinit()
                                pB2.deinit()
                                incre = 0
                                while pause:
                                    if not btn_a.value():   # wait for start again
                                        pause = False       # reset everything
                                        replay1 = True
                                        replay2 = True
                                        incre = 0
                                    time.sleep_ms(20)
                                break                       # break loop for restart
                            time.sleep_us(1000)
                        if not pause:
                            break                           # break loop for restart
                    if not pause:
                        break                               # break loop for restart
                if not pause:
                    break                                   # break loop for restart
                time.sleep_ms(20)
        time.sleep_ms(20)
except:
    pB1.deinit()
    pB2.deinit()

Video:

Follow this youtube link for the video: https://youtu.be/ln-ammSRafk