~nora/rpi-pico

3a2952ffec6a6d8195e25599eaae12fa7b2af2e9 — nora 1 year, 11 months ago 72916db
Add days 1 to 9
M 01-hello.py => 01-hello.py +5 -1
@@ 1,9 1,13 @@
import time

from machine import Pin

print("Hello World!")

onboardLED = Pin(25, Pin.OUT)

onboardLED.value(1)

time.sleep(2)
onboardLED.value(0)
\ No newline at end of file

onboardLED.value(0)

A 02-leds.py => 02-leds.py +39 -0
@@ 0,0 1,39 @@
import time

from machine import Pin

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

for i in range(3):
    red.value(1)
    amber.value(1)
    green.value(1)

    time.sleep(0.5)

    red.value(0)
    amber.value(0)
    green.value(0)

    time.sleep(0.5)

while True:
    red.value(1)
    amber.value(0)
    green.value(0)

    time.sleep(0.5)

    red.value(0)
    amber.value(1)
    green.value(0)

    time.sleep(0.5)

    red.value(0)
    amber.value(0)
    green.value(1)

    time.sleep(0.5)

A 03-buttons.py => 03-buttons.py +26 -0
@@ 0,0 1,26 @@
import time

from machine import Pin

button1 = Pin(13, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(8, Pin.IN, Pin.PULL_DOWN)
button3 = Pin(3, Pin.IN, Pin.PULL_DOWN)

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

while True:
    time.sleep(0.2)

    if button1.value() == 1:
        print("Buttons 1 pressed")
        green.value(1)
    if button2.value() == 1:
        print("Button 2 pressed")
        amber.toggle()
    if button3.value() == 1:
        print("Button 3 pressed")
        red.on()
        time.sleep(2)
        red.off()

A 04-potentiometer.py => 04-potentiometer.py +31 -0
@@ 0,0 1,31 @@
import time

from machine import ADC, Pin, PWM

potentiometer = ADC(Pin(27))

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

# Pulse Width Modulation
led = PWM(Pin(20))
led.freq(1000)

while True:
    value = potentiometer.read_u16()
    print(value)

    # red
    red.value(1 if value > 20000 else 0)

    # amber
    delay = value / 65000
    amber.on()
    time.sleep(delay)
    amber.off()
    time.sleep(delay)

    # green
    led.duty_u16(value)
    #time.sleep(0.0001)

A 05-buzzer.py => 05-buzzer.py +111 -0
@@ 0,0 1,111 @@
import time

from machine import ADC, Pin, PWM

# Tones
A = 440
As = 466
B = 494
C = 523
Cs = 554
D = 587
Ds = 622
E = 659
F = 698
Fs = 740
G = 784
Gs = 830

volume = 10000

potentiometer = ADC(Pin(27))
buzzer = PWM(Pin(13))


def playtone(note, delay1, delay2):
    value = potentiometer.read_u16()
    vol = int(value / 6)
    buzzer.duty_u16(vol)
    buzzer.freq(note)
    time.sleep(delay1)
    buzzer.duty_u16(0)
    time.sleep(delay2)


playtone(E, 0.1, 0.2)
playtone(E, 0.1, 0.2)
playtone(E, 0.1, 0.5)

playtone(E, 0.1, 0.2)
playtone(E, 0.1, 0.2)
playtone(E, 0.1, 0.5)

playtone(E, 0.1, 0.2)
playtone(G, 0.1, 0.2)
playtone(C, 0.1, 0.2)
playtone(D, 0.1, 0.2)
playtone(E, 0.1, 0.2)

playtone(D, 0.1, 0.2)
playtone(F, 0.1, 0.2)
playtone(G, 0.1, 0.2)
playtone(Gs, 0.1, 0.2)
playtone(G, 0.1, 0.2)
playtone(F, 0.1, 0.2)
playtone(D, 0.1, 0.5)

playtone(C, 0.1, 0.2)
playtone(E, 0.1, 0.2)
playtone(D, 0.1, 0.5)

playtone(A, 0.1, 0.2)
playtone(D, 0.1, 0.5)

playtone(D, 0.1, 0.2)
playtone(F, 0.1, 0.2)
playtone(G, 0.1, 0.2)
playtone(Gs, 0.1, 0.2)

playtone(G, 0.1, 0.2)
playtone(F, 0.1, 0.2)
playtone(Gs, 0.1, 0.5)

playtone(Gs, 0.1, 0.1)
playtone(G, 0.1, 0.1)
playtone(F, 0.1, 0.1)
playtone(Gs, 0.1, 0.1)
playtone(G, 0.1, 0.1)
playtone(F, 0.1, 0.1)

playtone(D, 0.5, 0.5)

playtone(C, 0.1, 0.1)
playtone(D, 0.1, 0.1)
playtone(F, 0.1, 0.1)
playtone(D, 0.1, 0.2)
playtone(As, 0.1, 0.2)
playtone(As, 0.1, 0.2)
playtone(G, 0.1, 0.5)

playtone(C, 0.1, 0.1)
playtone(D, 0.1, 0.1)
playtone(F, 0.1, 0.1)
playtone(D, 0.1, 0.2)
playtone(G, 0.1, 0.2)
playtone(G, 0.1, 0.2)
playtone(F, 0.1, 0.05)
playtone(E, 0.1, 0.05)
playtone(D, 0.1, 0.5)

playtone(C, 0.1, 0.1)
playtone(D, 0.1, 0.1)
playtone(F, 0.1, 0.1)
playtone(D, 0.1, 0.1)
playtone(F, 0.1, 0.3)
playtone(G, 0.1, 0.07)
playtone(E, 0.1, 0.07)
playtone(D, 0.1, 0.1)
playtone(C, 0.1, 0.4)
playtone(C, 0.1, 0.1)
playtone(G, 0.1, 0.2)
playtone(F, 0.1, 0.2)

A 06-lightsensor.py => 06-lightsensor.py +35 -0
@@ 0,0 1,35 @@
import time

from machine import ADC, Pin, PWM

buzzer = PWM(Pin(13))
lightsensor = ADC(Pin(26))

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

while True:
    light = lightsensor.read_u16()
    lightpercent = round(light / 65535 * 100, 1)
    print(f"{lightpercent} %")

    time.sleep(1)

    if lightpercent <= 30:
        red.on()
        amber.off()
        green.off()

        buzzer.duty_u16(1000)
        buzzer.freq(500)
        time.sleep(1)
        buzzer.duty_u16(0)
    elif 30 < lightpercent < 60:
        red.off()
        amber.on()
        green.off()
    elif lightpercent >= 60:
        red.off()
        amber.off()
        green.on()

A 07-infrasensor.py => 07-infrasensor.py +46 -0
@@ 0,0 1,46 @@
import time

from machine import Pin, PWM

buzzer = PWM(Pin(13))
buzzer.duty_u16(0)

pir = Pin(26, Pin.IN, Pin.PULL_DOWN)

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

print("Warming up…")
time.sleep(10)
print("Sensor ready!")


def alarm():
    buzzer.duty_u16(1000)

    for i in range(3):
        buzzer.freq(1000)
        red.on()
        amber.on()
        green.on()

        time.sleep(1)

        buzzer.freq(300)
        red.off()
        amber.off()
        green.off()

        time.sleep(1)

    buzzer.duty_u16(0)


while True:
    time.sleep(0.01)

    if pir.value() == 1:
        print("I SEE YOU!")
        alarm()
        print("Sensor active")

A 08-tempsensor.py => 08-tempsensor.py +68 -0
@@ 0,0 1,68 @@
import time

from machine import Pin, PWM

import ds18x20
import onewire

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

buzzer = PWM(Pin(13))
buzzer.duty_u16(0)

sensor_pin = Pin(26, Pin.IN)

# Tell MicroPython we're using a DS18B20 one wire sensor
sensor = ds18x20.DS18X20(onewire.OneWire(sensor_pin))

# Look for DS18B20 sensors (each contains a unique rom code)
roms = sensor.scan()


def alarm():
    buzzer.duty_u16(1000)

    for i in range(3):
        buzzer.freq(1000)
        red.on()
        amber.on()
        green.on()

        time.sleep(1)

        buzzer.freq(300)
        red.off()
        amber.off()
        green.off()

        time.sleep(1)

    buzzer.duty_u16(0)


while True:
    time.sleep(5)

    for rom in roms:
        sensor.convert_temp()
        time.sleep(1)

        temp = sensor.read_temp(rom)
        print(temp, "°C")

        if temp <= 16:
            alarm()
        if 16 < temp < 18:
            red.value(1)
            amber.value(0)
            green.value(0)
        elif 18 < temp < 22:
            red.value(0)
            amber.value(1)
            green.value(0)
        elif temp >= 22:
            red.value(0)
            amber.value(0)
            green.value(1)

A 09-tiltsensor.py => 09-tiltsensor.py +25 -0
@@ 0,0 1,25 @@
import time

from machine import Pin, PWM

buzzer = PWM(Pin(13))
buzzer.freq(1000)

tilt = Pin(26, Pin.IN, Pin.PULL_DOWN)
tilt_count = 0
tilt_on = False

while True:
    time.sleep(0.01)

    if not tilt_on and tilt.value() == 1:
        tilt_count = tilt_count + 1
        tilt_on = True
        print("tilts =", tilt_count)

        buzzer.duty_u16(10000)
        time.sleep(0.2)
        buzzer.duty_u16(0)

    if tilt_on and tilt.value() == 0:
        tilt_on = False

A main.py => main.py +19 -0
@@ 0,0 1,19 @@
import time

from machine import Pin


button1 = Pin(13, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(8, Pin.IN, Pin.PULL_DOWN)
button3 = Pin(3, Pin.IN, Pin.PULL_DOWN)

red = Pin(18, Pin.OUT)
amber = Pin(19, Pin.OUT)
green = Pin(20, Pin.OUT)

while True:
    time.sleep(0.2)

    green.value(button1.value())
    amber.value(button2.value())
    red.value(button3.value())