CMPT401_TWU

Lab Report 3 - Buzzers, Serial Communication, and AD/DA Converters

30-09-2023

Jonah Watts

Table Of Contents:

  1. Assignment Project #1
  2. Assignment Project #2

Assignment Project #1

Task Description:

To Create a program that will play a song depending on which button you press. One button will play row row row your boat with a flashing LED, and the other will play Mary Had a little lamb with a static LED.

Hardware Components:

Circut Diagrams

Photo(s) of the breadboard:

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 1, Project 2
# For CMPT 401
# Sources Used: 
#         - https://www.youtube.com/watch?v=QAbn-7Ai6UU&ab_channel=TechToTinker
#         - https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.pinterest.com%2Fpin%2F470696598527464928%2F&psig=AOvVaw1y9Y3rIx5X1gOErwx_Ds50&ust=1696106994914000&source=images&cd=vfe&ved=0CBEQjRxqFwoTCNjgv4nZ0IEDFQAAAAAdAAAAABAE
#         - https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.true-piano-lessons.com%2Fmary-had-a-little-lamb.html&psig=AOvVaw3zjJozSvAii070M3NDQ3hi&ust=1696125130794000&source=images&cd=vfe&ved=0CBEQjRxqFwoTCPCEltGc0YEDFQAAAAAdAAAAABAE
#-------------------------------------------------------
from machine import Pin,PWM
import math
import time

#Setup Pins
led1=Pin(5,Pin.OUT)
button1=Pin(4,Pin.IN,Pin.PULL_UP)
button2=Pin(0,Pin.IN,Pin.PULL_UP)
passiveBuzzer=PWM(Pin(13),2000)

#First create an array for the diffearnt notes in teh selected key
notes=[0,2093,2217,2349,2489,2637,2793,2959,3135,3322,3520,3729,3951,4186]

# Write the songs as arrays
rowRowRow = [
    1,1,1,
    1,1,1,
    1,1,3,
    5,5,5,
    5,5,3,
    5,5,6,
    8,8,8,
    8,8,6,
    13,13,13,
    8,8,8,
    5,5,5,
    1,1,1,
    8,8,6,
    5,5,3,
    1,1,1
]
maryHadALittleLamb = [
    5,3,1,3,5,5,5,
    3,3,3,5,8,8,
    5,3,1,3,5,5,5,
    5,3,3,5,3,1
]

#Then create a function to play the notes, and keep led on while playing
def PlayMary():
    led1.value(1)
    for x in maryHadALittleLamb:
        passiveBuzzer.init()
        if x != 0:
            passiveBuzzer.freq(notes[x])
        time.sleep_ms(230)
        passiveBuzzer.deinit()
    led1.value(0)

def PlayRow():
    tracker = 0
    for x in rowRowRow:
        #Blink led every 3 notes
        if tracker%3 == 0:
            led1.value(1)
        else:
            led1.value(0)
        tracker += 1
        #Play note
        passiveBuzzer.init()
        if x != 0:
            passiveBuzzer.freq(notes[x])
        time.sleep_ms(250)
        passiveBuzzer.deinit()    
    led1.value(0)

#Play notes if button is pressed
try:
    passiveBuzzer.deinit()
    led1.value(0)
    while True:
        
        if not button1.value():
            passiveBuzzer.init()
            PlayMary()
            passiveBuzzer.deinit()
        elif not button2.value():
            passiveBuzzer.init()
            PlayRow()
            passiveBuzzer.deinit()

except:
    passiveBuzzer.deinit()

Video of program in action


Assignment Project #2:

Task Description:

The goal of this assignment is to create a program that will communicate with another ESP32. When the button is pressed on one ESP32, the other ESP32 will play a song and flash an LED. When the button is pressed on the other ESP32, the first ESP32 will play a different song and flash an LED but in a different way. This project was completed with Curtis Esau, with his project being found on his lab report site.

Hardware Components

Circut Diagrams

Photo(s) of the breadboard

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 1, Project 2
# For CMPT 401
# Sources Used: 
#         - https://www.youtube.com/watch?v=QAbn-7Ai6UU&ab_channel=TechToTinker
#         - https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.pinterest.com%2Fpin%2F470696598527464928%2F&psig=AOvVaw1y9Y3rIx5X1gOErwx_Ds50&ust=1696106994914000&source=images&cd=vfe&ved=0CBEQjRxqFwoTCNjgv4nZ0IEDFQAAAAAdAAAAABAE
#         - https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.true-piano-lessons.com%2Fmary-had-a-little-lamb.html&psig=AOvVaw3zjJozSvAii070M3NDQ3hi&ust=1696125130794000&source=images&cd=vfe&ved=0CBEQjRxqFwoTCPCEltGc0YEDFQAAAAAdAAAAABAE
#-------------------------------------------------------
from machine import Pin,PWM,UART
import math
import time


usart_flag=0

myUsart = UART(1, baudrate=115200, bits=8, parity=0, rx=5, tx=2, timeout=10) 
myUsart.write(str("\r\nESP32 initialization completed!\r\n")+\
      str("Please input some characters,\r\n")+\
      str("select \"Newline\" below and click send button. \r\n"))

#Setup Pins
led1=Pin(32,Pin.OUT)
button1=Pin(4,Pin.IN,Pin.PULL_UP)
button2=Pin(0,Pin.IN,Pin.PULL_UP)
passiveBuzzer=PWM(Pin(13),2000)

#First create an array for the diffearnt notes in teh selected key
notes=[0,2093,2217,2349,2489,2637,2793,2959,3135,3322,3520,3729,3951,4186]
maryHadALittleLamb = [
    5,3,1,3,5,5,5,
    3,3,3,5,8,8,
    5,3,1,3,5,5,5,
    5,3,3,5,3,1
]

#Then create a function to play the notes, and keep led on while playing
def PlayMary():
    tracker = 0
    led1.value(1)
    for x in maryHadALittleLamb:
        #Toggle the LED
        if tracker%3 == 0:
            led1.value(1)
        else:
            led1.value(0)
        tracker+=1

        #Play the note
        passiveBuzzer.init()
        if x != 0:
            passiveBuzzer.freq(notes[x])
        time.sleep_ms(230)
        passiveBuzzer.deinit()
    led1.value(0)

#Play notes if button is pressed
try:
    passiveBuzzer.deinit()
    led1.value(0)
    while True:
        #if i receive a message from the other esp32, activate flag and read buffer
        if myUsart.any():
            usart_buffer=myUsart.read()
            usart_flag=1
        #if flag is activated, play the song
        if usart_flag==1:
            usart_flag=0
            passiveBuzzer.init()
            PlayMary()
            passiveBuzzer.deinit()
        #if button is pressed, send a message to the other esp32
        if not button1.value():
            myUsart.write("ButtonPressed")
            time.sleep_ms(200)

except:
    passiveBuzzer.deinit()

Video of program in action