CMPT401_TWU

Lab Report 5

18-10-2023

Jonah Watts

Table Of Contents:

  1. Lab 14.1: Joystick
  2. Lab 15.1: Flowing Water Light
  3. Lab 16.1: 7-Segment Display
  4. Lab 16.2: 4 Digit 7-Segment Display
  5. Lab 16.3: LED Matrix
  6. Assignment Project #1
  7. Assignment Project #2

Lab 14.1: Joystick

Lab 15.1: Flowing Water Light

Lab 16.1: 7-Segment Display

Lab 16.2: 4 Digit 7-Segment Display

Lab 16.3: LED Matrix


Assignment Project #1

Task Description:

The goal of this assignment is to create a joystick that will be able to control a message on a 4 number 7 segment display. For this project I used the word “HI” to move across the screen. As the joystick is pressed in a direction, the message is moved in that direction, and a different tone is played depending on the direction.

Hardware Components:

Circut Diagrams

Photo(s) of the breadboard:

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 5, Project 1
# For CMPT 401
#---------------------------------------------------------
import time
from my74HC595 import Chip74HC595
from machine import Pin, ADC, PWM
import time

comPin=[25,26,27,14]
#array of mesage to display
num =[ 
    [[0x89, 0xf9, 0xff, 0xff],[0xff, 0x89, 0xf9, 0xff], [0xff, 0xff, 0x89, 0xf9], [0xf9, 0xff, 0xff, 0x89]], #0
    [[0xe3, 0xfb, 0xff, 0xff],[0xff, 0xe3, 0xfb, 0xff], [0xff, 0xff, 0xe3, 0xfb], [0xfb, 0xff, 0xff, 0xe3]], #1
    [[0xdc ,0xfd, 0xff, 0xff],[0xff, 0xdc, 0xfd, 0xff], [0xff, 0xff, 0xdc, 0xfd], [0xfd, 0xff, 0xff, 0xdc]], #2
]
xPosition = 0
yPosition = 0

xVal=ADC(Pin(36))
yVal=ADC(Pin(39))
zVal=Pin(12,Pin.IN,Pin.PULL_UP)

xVal.atten(ADC.ATTN_11DB)
yVal.atten(ADC.ATTN_11DB)
xVal.width(ADC.WIDTH_12BIT)
yVal.width(ADC.WIDTH_12BIT)

#Function to display the LED
def led_display():
    for i in range(0,4):
        chns=Pin(comPin[i],Pin.OUT)
        chip.shiftOut(0,num[yPosition][xPosition][i])
        chns.value(1)
        time.sleep_ms(1)
        chns.value(0)

notes=[2000,3000,1000,5000]
passiveBuzzer=PWM(Pin(23),2000)
passiveBuzzer.init()
#Pin(15)-74hc595.ds, Pin(2)-74hc595.st_cp, Pin(4)-74hc595.sh_cp
chip = Chip74HC595(15,2,4)
refresh = 0
try:
    while True:
        led_display()
        #only sample buzzer and joystick every 40ms
        if refresh == 40:
            refresh = 0

            #change depending on joystick position
            if xVal.read() < 1000:
                xPosition = (xPosition-1)%4
                passiveBuzzer.freq(notes[0])
                passiveBuzzer.duty(512)
            elif xVal.read() > 3000:
                xPosition = (xPosition+1)%4
                passiveBuzzer.freq(notes[1])
                passiveBuzzer.duty(512)
            elif yVal.read() < 1000:
                yPosition = (yPosition-1)%3
                passiveBuzzer.freq(notes[2])
                passiveBuzzer.duty(512)
            elif yVal.read() > 3000:
                yPosition = (yPosition+1)%3
                passiveBuzzer.freq(notes[3])
                passiveBuzzer.duty(512)
            else:
                passiveBuzzer.duty(0)
        refresh += 1

except Exception as e:
    print("Error:", e)b 

Video of program in action


Assignment Project #2:

Task Description:

The task of this assignment is to control an animation and sound with the 74HC595 chip. For this lab, when the green button is pressed, the animaiton (of pac-man) is played, as well as the pac-man theme song is played. When the red button is pressed, the music stops, and the animation is frozen in place. This is until the green button is once again pressed and the process wil repeat itself.

Hardware Components

Circut Diagrams

Photo(s) of the breadboard

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 5, Project 2
# For CMPT 401
#---------------------------------------------------------
# Sources Used:
#       https://producelikeapro.com/blog/wp-content/uploads/2022/01/Understanding-Note-Frequency-Charts-And-Why-You-Should-Be-Using-One_2.jpg
import time
from machine import Pin,PWM
from my74HC595 import Chip74HC595

numdata = [
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00,0xfe,0xfe,0x88,0x88,0x88,0xf8,0x70,   # P
    0x00,0x7e,0x7e,0xc8,0x88,0xc8,0x7e,0x7e,  # A
    0x00,0x38,0x7c,0xc6,0x82,0x82,0xc6,0x44,  # C
    0x00,0x00,0x18,0x18,0x18,0x00,0x00,       # - 
    0x00,0xfe,0xfe,0x70,0x38,0x70,0xfe,0xfe,  # M
    0x00,0x7e,0x7e,0xc8,0x88,0xc8,0x7e,0x7e,  # A
    0x00,0xfe,0xfe,0x60,0x30,0x18,0xfe,0xfe,  # N
    0x00,0x1c,0x22,0x41,0x49,0x55,0x22,0x00,0x00,  # Pac
    0x3f,0x4e,0xcf,0xfe,0xcf,0x4e,0x3f,0x00,  #	 Ghost
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x00,0x18,0x18,0x00,0x3f,0x4e,0xcf,0xfe,  # Dot + Ghost
    0xcf,0x4e,0x3f,0x00,0x1c,0x22,0x41,0x41, # Ghost + Pac
    0x55,0x22,0x00,0x0c,0x0c,0x00,0x0c,0x0c,  # Dots + Pac
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x3f,0x4e,0xcf,0xfe,0xcf,0x4e,0x3f,0x00,  # Ghost
    0x3f,0x4e,0xcf,0xfe,0xcf,0x4e,0x3f,0x00,  # Ghost
    0x3f,0x4e,0xcf,0xfe,0xcf,0x4e,0x3f,0x00,  # Ghost
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x00,0x1c,0x22,0x41,0x49,0x55,0x22,0x00,0x00,  # Pac
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x18,0x18,0x00,0x18,0x18,0x00,0x18,0x18,  # Dots
    0x3f,0x4e,0xcf,0xfe,0xcf,0x4e,0x3f,0x00,  # Ghost    
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # " "
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # " "
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # " "
]

#First create an array for the diffearnt notes in teh selected key
notes={ 
        "B6":1975,
        "C":2093,
        "C#":2217,
        "D":2349,
        "D#":2489,
        "E":2637,
        "F":2793,
        "F#":2959,
        "G":3135,
        "G#":3322,
        "A":3520,
        "A#":3729,
        "B":3951,
        "C8":4186,
        "C#8":4435,
        "D8":4699,
        "D#8":4978,
        "E8":5274,
        "F8":5587,
        "F#8":5919,
        "G8":6271,
        "G#8":6644,
        "A8":7040,
        "A#8":7458,
        "B8":7902,
        }

# Write the songs as arrays
pacmanJingle = [
     "B6","B","F#","D#",
     "B", "F", "E", "NA",
     "C", "C8", "G", "E",
     "C8", "G", "E", "NA",

     "B6","B","F#","D#",
     "B", "F", "E","NA",

     "E", "E", "F",
     "F", "F#", "G",
    "G", "G#", "A",
    "NA", "NA", "NA", "NA",
    "NA", "NA", "NA", "NA",
]

'''
Pin(15)        -    74HC595(1).ds,
Pin(2)         -    74HC595(1).st_cp,
Pin(4)         -    74HC595(1).sh_cp,
Pin(5)         -    74HC595(1).oe

74HC595(1).q7' -    74HC595(2).ds,
Pin(2)         -    74HC595(2).st_cp,
Pin(4)         -    74HC595(2).sh_cp,
Pin(5)         -    74HC595(2).oe
'''
# Itit all of the peices
chip = Chip74HC595(15,2,4,5)
button1=Pin(14,Pin.IN,Pin.PULL_UP)
button2=Pin(12,Pin.IN,Pin.PULL_UP)
passiveBuzzer=PWM(Pin(27),2000)
passiveBuzzer.init()
passiveBuzzer.duty(0)
current_note = 0
last_screen = 0
try:
    while True:
        # If the button is pressed, play the jingle
        if button2.value() == 0:
            chip.clear()
            for i in range(215):
                last_screen = i
                for k in range(5):
                    cols=0x01
                    for j in range(i,8+i):
                        chip.disable()
                        chip.shiftOut(0,numdata[j])
                        chip.shiftOut(1,~cols)
                        cols<<=1
                        chip.enable()
                        time.sleep_us(700)
                #Play noise
                if i%2 == 0:
                    if pacmanJingle[current_note] != "NA":
                        passiveBuzzer.duty(512)
                        passiveBuzzer.freq(notes[pacmanJingle[current_note]])
                    else:
                        passiveBuzzer.duty(0)
                    current_note = (current_note+1)%len(pacmanJingle)
                else:
                    passiveBuzzer.duty(0)
                if button1.value() == 0:
                    chip.enable()
                    passiveBuzzer.duty(0)
                    current_note = 0
                    break
        else:
            #Display 
            for k in range(5):
                cols=0x01
                for j in range(last_screen,8+last_screen):
                    chip.disable()
                    chip.shiftOut(0,numdata[j])
                    chip.shiftOut(1,~cols)
                    cols<<=1
                    chip.enable()
                    time.sleep_us(700)

except Exception as e:
    print("Error:", e)

Video of program in action