~nora/rpi-pico

9f995faee45d206084a5342beb6de1586fd5345c — nora 1 year, 4 months ago 3a2952f main
Add days 10 to 12
4 files changed, 205 insertions(+), 1 deletions(-)

A 10-beamsensor.py
A 11-oled.py
A 12-rgbled.py
M README.md
A 10-beamsensor.py => 10-beamsensor.py +89 -0
@@ 0,0 1,89 @@
import sys
import time

from machine import Pin, PWM

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

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

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

start_time = 0
timecheck = 0
score_time = 0
score_counter = 0
state = 0
target_score = 100


def start_game():
    print("Game starts after the beep!")

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

    print("GO!")
    print("-" * 30)


def end_game(win):
    red.value(0)
    amber.value(0)
    green.value(0)

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

    print("-" * 30)
    if win:
        print("YOU WIN! You took", score_time, "seconds!")
    else:
        print("YOU LOSE! Game over :(")
    print(f"The target was {target_score}, you scored {score_counter}")
    print("-" * 30)


def display_score():
    print(f"SCORE = {score_counter}/{target_score}, Time: {30 - timecheck}")

    if (target_score / 100 * 50) < score_counter < (target_score / 100 * 80):
        red.on()
        amber.off()
        green.off()
    elif (target_score / 100 * 80) < score_counter < target_score:
        red.on()
        amber.on()
        green.off()
    elif score_counter >= target_score:
        red.on()
        amber.on()
        green.on()


start_game()
start_time = time.time()

while True:
    time.sleep(0.0001)
    timecheck = time.time() - start_time

    if timecheck >= 30:
        end_game(win=(score_counter >= target_score))
        sys.exit()

    elif state == 0 and beam.value() == 0:
        state = 1
        score_counter = score_counter + 1
        if score_counter == target_score:
            score_time = timecheck
        display_score()

    elif state == 1 and beam.value() == 1:
        state = 0

A 11-oled.py => 11-oled.py +55 -0
@@ 0,0 1,55 @@
import time

from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C

button = Pin(8, Pin.IN, Pin.PULL_DOWN)

lightsensor = ADC(Pin(26))

# Set up I2C and the pins we're using for it
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)

# Short delay to stop I2C falling over
time.sleep(1)

# Define the display and size (128x32)
oled = SSD1306_I2C(128, 32, i2c)

light = 0
counter = 0
state = 0


def display(msg):
    oled.fill(0)
    oled.text(f"Light: {int(light)}%", 0, 0)
    oled.rect(100, 0, 25, 8, 1)
    oled.fill_rect(100, 0, int(light / 100 * 25), 8, 1)
    oled.text(f"* {counter:04d} *", 30, 12)
    oled.text(msg, 0, 24)
    oled.show()


while True:
    time.sleep(0.1)

    light = round((lightsensor.read_u16()) / 65535 * 100, 1)

    if state == 0:
        display(">Naughty  Nice")
        state = 1

        if button.value() == 1:
            display(">Naughty  Oh no!")
            time.sleep(2)

    elif state == 1:
        display(" Naughty >Nice")
        state = 0

        if button.value() == 1:
            display(" Yay!    >Nice")
            time.sleep(2)

    counter += 1

A 12-rgbled.py => 12-rgbled.py +60 -0
@@ 0,0 1,60 @@
import time

from machine import Pin, ADC
from neopixel import NeoPixel

button = Pin(15, Pin.IN, Pin.PULL_DOWN)
potentiometer = ADC(Pin(26))
strip = NeoPixel(Pin(28), 15)

black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
yellow = 255, 255, 0
cyan = 0, 255, 255
pink = 255, 0, 128

colors = [red, green, blue, yellow, cyan, pink]
color = white
modes = ["COLOR", "FADING", "CHASER", "FADING_AUTO", "CHASER_AUTO"]
mode = modes[0]

while True:
    time.sleep(0.2)
    if button():
        mode = modes[(modes.index(mode) + 1) % len(modes)]
        print("Mode:", mode)

    if mode == "COLOR":
        pot = round(potentiometer.read_u16() / 65535 * (len(colors) - 1))
        color = colors[pot]
        strip.fill(color)
        strip.write()
    elif mode == "FADING":
        pot = potentiometer.read_u16() / 65535
        strip.fill((round(color[0] * pot), round(color[1] * pot), round(color[2] * pot)))
        strip.write()
    elif mode == "CHASER":
        pot = round(potentiometer.read_u16() / 65535 * (len(strip) - 1))
        strip.fill(black)
        strip[pot] = color
        strip.write()
    elif mode == "FADING_AUTO":
        for color in colors:
            for i in range(1, 100, 1):
                strip.fill((round(color[0] * i * 0.01), round(color[1] * i * 0.01), round(color[2] * i * 0.01)))
                strip.write()
                time.sleep(0.005)
            for i in range(100, 1, -1):
                strip.fill((round(color[0] * i * 0.01), round(color[1] * i * 0.01), round(color[2] * i * 0.01)))
                strip.write()
                time.sleep(0.005)
    elif mode == "CHASER_AUTO":
        for color in colors:
            for i in range(len(strip)):
                delay = potentiometer.read_u16() / 50000
                time.sleep(delay)
                strip[i] = color
                strip.write()

M README.md => README.md +1 -1
@@ 67,7 67,7 @@ Reset the device:

Install packages from [micropython-lib](https://github.com/micropython/micropython-lib):

    mpremote mip install onewire ds18x20
    mpremote mip install ssd1306

### [pyboard.py](https://docs.micropython.org/en/latest/reference/pyboard.py.html)