The Messy Real World — Day 3 of 4
'F' → Fullscreen
Leave everything from 2.8 and 2.9 wired. Create a new MicroPython program named 2-10-alarm.
3V3 and GND, with A2 connected to the point between them.from machine import Pin, ADCimport time
light_sensor = ADC(Pin(42))
while True: print(light_sensor.read_u16()) time.sleep(0.2)read_average() from 2.9 so the reading holds still.Put your threshold nearer the covered value than the bright one. An alarm that fires when someone walks past the window is worse than one that needs a proper blocking.
from machine import Pin, ADCimport time
light_sensor = ADC(Pin(42))
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
while True: print("light:", read_average(light_sensor, 10)) time.sleep(0.3)GPIO 23.beep(speaker, hz, seconds) that sets a pitch, turns the sound on, waits, then silences it.The resistor matters. A speaker connected straight to a pin will try to draw far more current than the pin can give.
from machine import Pin, ADC, PWMimport time
light_sensor = ADC(Pin(42))speaker = PWM(Pin(23))
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def beep(speaker, hz, seconds): speaker.freq(hz) speaker.duty_u16(32768) time.sleep(seconds) speaker.duty_u16(0)
beep(speaker, 440, 0.3)beep(speaker, 660, 0.3)beep(speaker, 880, 0.3)DARK.from machine import Pin, ADC, PWMimport time
light_sensor = ADC(Pin(42))speaker = PWM(Pin(23))
DARK = 12000ALARM_HZ = 880
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def beep(speaker, hz, seconds): speaker.freq(hz) speaker.duty_u16(32768) time.sleep(seconds) speaker.duty_u16(0)
while True: light = read_average(light_sensor, 10) if light < DARK: print("Beam broken!") beep(speaker, ALARM_HZ, 0.2) time.sleep(0.05)The trick is a variable that remembers. armed starts True, becomes False when the beam breaks, and only the button puts it back.
from machine import Pin, ADC, PWMimport time
leds = [ Pin(28, Pin.OUT), Pin(29, Pin.OUT), Pin(30, Pin.OUT), Pin(31, Pin.OUT), Pin(32, Pin.OUT),]light_sensor = ADC(Pin(42))speaker = PWM(Pin(23))button = Pin(33, Pin.IN, Pin.PULL_DOWN)
DARK = 12000ALARM_HZ = 880
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def beep(speaker, hz, seconds): speaker.freq(hz) speaker.duty_u16(32768) time.sleep(seconds) speaker.duty_u16(0)
def all_on(leds): for led in leds: led.value(1)
def all_off(leds): for led in leds: led.value(0)
armed = Trueall_off(leds)print("Armed.")
while True: light = read_average(light_sensor, 10) if armed: if light < DARK: print("Beam broken!") armed = False else: all_on(leds) beep(speaker, ALARM_HZ, 0.2) all_off(leds) time.sleep(0.1) if button.value() == 1: armed = True print("Re-armed.") time.sleep(0.05)Your finished program should behave like this:
Armed.Beam broken!Re-armed.Beam broken!Answer the following questions before submitting your work.
DARK. Describe how you actually chose yours, and why the same number would not work in a different room.armed variable, the alarm stops as soon as the sensor is uncovered. Explain how one True/False variable changes the behavior, and why that is what an alarm should do.Submit the required files to the appropriate dropbox.