CMPT401_TWU

Lab Report 6

30-10-2023

Jonah Watts

Table Of Contents:

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

Assignment Project #1

Task Description:

The task of this assignment is to make a robot that will wave and play a sad song when the room gets dark, indicating the user has left. The LED will also flash. When the room is lit back up, the robot will stop and reset.

Hardware Components:

Circut Diagrams

Photo(s) of the breadboard:

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 6, Project 1
# For CMPT 401
#---------------------------------------------------------
  
from machine import Pin,PWM,ADC
from myservo import myServo
import time

pwm =PWM(Pin(14,Pin.OUT),1000)
adc=ADC(Pin(35))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_10BIT)
servo=myServo(33)
passiveBuzzer=PWM(Pin(13),2000)
passiveBuzzer.init()

#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
sadSong = [
        "F", "A", "F", "E", 
        "E", "E", "E", "E", 
        "F", "A", "F", "D",
        "D","D",
        "D", "F", "D", "C",
         "C", "C",
        "D","A","G",
        "G","G",
        "D","A","G",
        "G","G",
        "F","F","D","D","D","D",
        "NA","NA","NA","NA",

        "F", "G", "F", "E",
        "E","E","E","E",
        "E", "G", "E", "C",
        "C","C",
        "C", "E", "C", "A#","A#","A#",
        "A#","C", "B",  "A#","A#","A#",
        
        "A","A","A#","A#","C","C","D","E",
        "C","C","D","D","E","E"


]

i = 0
print(len(sadSong))
def playTone():
    if sadSong[i] == "NA":
        passiveBuzzer.duty(0)
    else:
        
        passiveBuzzer.duty(512)
        passiveBuzzer.freq(notes[sadSong[i]])

try:
    while True:
        adcValue=adc.read()
        pwm.duty(adcValue)
        if adcValue >= 300:
            pwm.duty(adcValue)
            servo.myServoWriteAngle(int(140))
            time.sleep_ms(250)
            playTone()
            i += 1
            time.sleep_ms(250)
            playTone()
            i += 1
            pwm.duty(0)
            servo.myServoWriteAngle(int(160))
            time.sleep_ms(250)
            playTone()
            i += 1
            time.sleep_ms(250)
            playTone()
            i += 1
            pwm.duty(900)
            if i >= 80:
                i = 0
        else:
             i = 0
             passiveBuzzer.duty(0)

except Exception as e:
    pwm.deinit()
    servo.deinit()
    print("Error:", e)

Video of program in action


Assignment Project #2:

Task Description:

The task of this assignment is to make a temperature sensitive fan. The fan will turn on when the temperature is above 26 degrees Celsius, and turn off when the temperature is below 26 degrees Celsius. A message will also be displayed on an LCD screen to tell the user if the temperature is good or not.

Hardware Components

Circut Diagrams

Photo(s) of the breadboard

Source Code

The Source code can be downloaded here here

#---------------------------------------------------------
# Jonah Watts 
# Assignment 6, 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
import math
from machine import I2C, Pin, ADC
from I2C_LCD import I2cLcd

relay = Pin(12, Pin.OUT)        
i2c = I2C(scl=Pin(14), sda=Pin(13), freq=400000)
adc=ADC(Pin(35))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)
devices = i2c.scan()
relay.value(0)

if len(devices) == 0:
    print("No i2c device !")
else:
    for device in devices:
        print("I2C addr: "+hex(device))
        lcd = I2cLcd(i2c, device, 2, 16)

try:
    lcd.move_to(0, 0)
    while True:
        adcValue=adc.read()
        voltage=adcValue/4095*3.3
        Rt=10*voltage/(3.3-voltage)
        tempK=(1/(1/(273.15+25)+(math.log(Rt/10))/3950))
        tempC=tempK-273.15
        print("ADC value:",adcValue,"\tVoltage :",voltage,"\tTemperature :",tempC)
        time.sleep_ms(1000)

        if (tempC > 26):
            relay.value(1)
            lcd.clear()
            lcd.putstr("Cool yourself   down!")
        else:
            relay.value(0)
            lcd.clear()
            lcd.putstr("Good temperature!")
except:
    pass

Video of program in action