CMPT401_TWU

Lab Report 4

11-10-2023

Jonah Watts

Table Of Contents:

  1. Lab 11.2: Soft Colorful Light
  2. Lab 11.3: Soft rainbow light
  3. Lab 12.1: Nightlamp
  4. Assignment Project #1
  5. Assignment Project #2

Lab 11.2: Soft Colorful Light

Lab 11.3: Soft rainbow light

Lab 12.1: Nightlamp


Assignment Project #1

Task Description:

To create a piano like instrument that will play tones on a buzzer when one of the corresponding touch sensitive wires is touched. A corresponding LED would light up that corresponds to the key being played. The touch wires will act like piano keys and light up a different light and play a higher tone when you change the pins.

Hardware Components:

Circut Diagrams

Photo(s) of the breadboard:

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 4, Project 1
# For CMPT 401
#---------------------------------------------------------
from machine import TouchPad, Pin, PWM
import time

PRESS_VAL=70    #Set a threshold to judge touch
RELEASE_VAL=200 #Set a threshold to judge release

#First create an array for the diffearnt notes in teh selected key
notes=[261,293,329,349,392,440,493]
passiveBuzzer=PWM(Pin(26),2000)

# Create array of touchpins
tp = [
    TouchPad(Pin(33,Pin.IN,Pin.PULL_UP)), 
    TouchPad(Pin(32,Pin.IN,Pin.PULL_UP)),
    TouchPad(Pin(14,Pin.IN,Pin.PULL_UP)),
    TouchPad(Pin(12,Pin.IN,Pin.PULL_UP)),
    TouchPad(Pin(13,Pin.IN,Pin.PULL_UP)),
    TouchPad(Pin(15,Pin.IN,Pin.PULL_UP)),
    TouchPad(Pin(4,Pin.IN,Pin.PULL_UP))]
#Create and array of the LEDs
led=[Pin(23,Pin.OUT),
     Pin(2,Pin.OUT),
     Pin(0,Pin.OUT),
     Pin(21,Pin.OUT),
     Pin(5,Pin.OUT),
     Pin(18,Pin.OUT),
     Pin(19,Pin.OUT)]

passiveBuzzer.init()
current_note = -1

# Trigger the pins, play the corresponding note, and light up the LED
while True:
    note_played = False  # Flag to track if a note has been played
    for i in range(7):
        if tp[i].read() < PRESS_VAL:
            passiveBuzzer.freq(notes[i])
            passiveBuzzer.duty(512)
            led[i].value(1)  # Turn on the LED
            note_played = True
    # Stop everything, elif wont work because it will shut down buzzer immediatley
    if not note_played:
        passiveBuzzer.duty(0)
        for i in range(7):
            led[i].value(0)
    time.sleep_ms(10)

Video of program in action


Assignment Project #2:

Task Description:

In this assignment we are tasked with making an alarm clock. When it is dark there is a single LED that is lit, but when it becomes light outside, the LED turns off and a buzzer will make a really annoying buzz, and the neopixel strip will flash random colors. This will continue until the user hits the button to reset the LED and alarm to defualt state, and when the light is dimmed the single LED will turn back on for the process to continue again.

Hardware Components

Circut Diagrams

Photo(s) of the breadboard

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 4, Project 2
# For CMPT 401
# Source used: https://www.youtube.com/watch?v=QAbn-7Ai6UU&ab_channel=TechToTinker
#---------------------------------------------------------
from machine import Pin,PWM,ADC
import neopixel
import time
import math
import random


adc=ADC(Pin(36))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_10BIT)

#Define everything that needs to be defined
LED = Pin(15, Pin.OUT)  # LED
pin = Pin(2, Pin.OUT)   # NeoPixel
button = Pin(34, Pin.IN, Pin.PULL_UP) #Button
np = neopixel.NeoPixel(pin, 8) #NeoPixel but as a set of 8
passiveBuzzer=PWM(Pin(26),2000) #Buzzer
passiveBuzzer.init()
PI=3.14

#Alert function to play a sound
def alert():
    for x in range(0,36):
        sinVal=math.sin(x*10*PI/180)
        toneVal=2000+int(sinVal*500)
        passiveBuzzer.freq(toneVal)
        time.sleep_ms(10)

#boolean to disarm the alarm
alarmArmed = False

while True:
    #If it is dark, activate led and arm light
    adcValue=adc.read()
    if adcValue > 200:
        LED.value(1)
        passiveBuzzer.duty(0)
        alarmArmed = True
        time.sleep(1)
    #If it is light, flash and make noise
    elif alarmArmed:
        passiveBuzzer.duty(512)
        print("button not pressed")
        LED.value(0)
        alert()
        for j in range(0,8):
            np[j]=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
            np.write()
    #If button is pressed disarm the alarm and turn off the light
    if button.value() == 0:
        alarmArmed = False
        passiveBuzzer.duty(0)
        LED.value(0)
        for j in range(0,8):
            np[j]=[0,0,0]
            np.write()
        print("button pressed")

Video of program in action