Friday, 3 February 2023

TRL - Exercise2c

#Using def function #Library from gpiozero import LED from time import sleep #Setup Components / Variables tf_red_led = LED(13) tf_amber_led = LED(19) tf_green_led = LED(26) #Alorgithm #Initialize the LEDs tf_red_led.off() tf_amber_led.off() tf_green_led.off() def trafficLight(): tf_green_led.on() sleep(10) tf_green_led.off() tf_amber_led.on() sleep(5) tf_amber_led.off() tf_red_led.on() sleep(10) tf_red_led.off() while True: trafficLight()

Thursday, 2 February 2023

SF2.2 Display the Level

import machine import utime pin = [6,7,8,9,10,11,12,13] led= [] for i in range(8): led.append(None) led[i] = machine.Pin(pin[i], machine.Pin.OUT) while True: for i in range(8): led[i].toggle() utime.sleep(0.5) #SunFounder Kepler kit 2.2 Display the Level

Wednesday, 1 February 2023

TTB02A Make an LED send SOS

from machine import Pin from time import sleep blueLed=Pin(15,Pin.OUT) dot=.3 dash=2*dot while True: for i in range(1,4): blueLed.value(0) sleep(dot) blueLed.value(1) sleep(dot) for i in range(1,4): blueLed.value(0) sleep(dash) blueLed.value(1) sleep(dash) for i in range(1,4): blueLed.value(0) sleep(dot) blueLed.value(1) sleep(dot) for i in range(1,4): blueLed.value(0) sleep(1) #S.O.S represented in Morse code is #▄ ▄ ▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ ▄ #https://www.youtube.com/watch?v=pWzyb8VvxTY

TTB02 Understanding and Using Breadboards

from machine import Pin from time import sleep blueLED = Pin(15, Pin.OUT) while True: blueLED.value(0) sleep(1) blueLED.value(1) sleep(1) #https://www.youtube.com/watch?v=eGdrtikKc5U

TTB01A How Fast Can You Blink the LED and Still See It?

# This program loops through a list of sleep times to see how # fast I can blink the internal LED on a Pico W and still see # it blink # In this version, I moved the print outside of the blink loops # to avoid any delay due to running too many prints inside the blink loop # January 2023 # Lori Pfahler # import needed modules/functions import machine # utime and time are the same - the time library just points to utime library from utime import sleep # setup internal LED as an output # regular pico use GP25; on Pico W use 'LED' # for regular pico use: internalLED = machine.Pin(25, machine.Pin.OUT internalLED = machine.Pin('LED', machine.Pin.OUT) # create a list of sleep durations to test my eyes myList = [1.0, 0.90, 0.80, 0.70, 0.60, 0.50, 0.40, 0.30,0.20, 0.10, 0.05, 0.04, 0.03, 0.02, 0.019, 0.018, 0.017, 0.016, 0.015, 0.014, 0.013, 0.012, 0.011, 0.010] while True: for num in myList: # loop over numbers on myList if num >= 0.5: # blink 5 times for longer sleep times print('Can you see this speed? ', num, 'seconds') for nBlinks in range(0, 10): internalLED.toggle() sleep(num) elif num >= 0.1 and num < 0.5: # blink 10 times for medium sleep times print('Can you see this speed? ', num, 'seconds') for nBlinks in range(0, 20): internalLED.toggle() sleep(num) else: # blink 50 times for short sleep times print('Can you see this speed? ', num, 'seconds') for nBlinks in range(0, 100): internalLED.toggle() sleep(num) #https://www.youtube.com/watch?v=Bur4Ch7wvo8

TTB01 Flashing LED

from machine import Pin from time import sleep myLED=Pin('LED', Pin.OUT) myLED.off() print('LED now flashes with toggle') for x in range(1, 30, 1): myLED.toggle() sleep(1) print('LED now flashes asymmetrically') while True: myLED.value(1) sleep(1) myLED.value(0) sleep(.1) #https://www.youtube.com/watch?v=SL4_oU9t8Ss&t=0s

Sunday, 29 January 2023

4Buttons to control RGB led using Raspberry Pi Pico

from picozero import Button, RGBLED from time import sleep rgb = RGBLED(red = 1, green = 2, blue = 3) button = Button(18) option = 0 # store the current option def choice(): # call the next function and update the option global option if option == 0: rgb.color = (255, 0, 0) elif option == 1: rgb.color = (0, 255, 0) elif option == 2: rgb.color = (0, 0, 255) elif option == 3: rgb.off() # move to the next option if option == 3: option = 0 else: option = option + 1 button.when_pressed = choice # Call the choice function when the button is pressed #https://projects.raspberrypi.org/en/projects/

3Playing music with Raspberry Pi Pico

from picozero import Speaker from time import sleep #from random import randint speaker = Speaker(5) print("Play the frequency 523, middle C.") sound = [ [523, 1], [None, 0.1], [523, 4] ] speaker.play(sound) speaker.play(sound, wait=False) # don't delay the main code sleep(0.5) print("Gradually increases in frequency to create a positive sound") for i in range(2000, 5000, 100): speaker.play(i, 0.05) # short duration sleep(0.5) print("Decreases the pitch to create a bird chirping sound.") for i in range(5000, 2000, -100): speaker.play(i, 0.05) # very short duration sleep(0.5) print("Create a list of notes and durations to make a tune. You can then play the tune.") BEAT = 0.6 # The length of a note in a single beat liten_mus = [ ['d5', BEAT / 2], ['d#5', BEAT / 2], ['f5', BEAT], ['d6', BEAT], ['a#5', BEAT], ['d5', BEAT], ['f5', BEAT], ['d#5', BEAT], ['d#5', BEAT], ['c5', BEAT / 2],['d5', BEAT / 2], ['d#5', BEAT], ['c6', BEAT], ['a5', BEAT], ['d5', BEAT], ['g5', BEAT], ['f5', BEAT], ['f5', BEAT], ['d5', BEAT / 2], ['d#5', BEAT / 2], ['f5', BEAT], ['g5', BEAT], ['a5', BEAT], ['a#5', BEAT], ['a5', BEAT], ['g5', BEAT], ['g5', BEAT], ['', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT / 2], ['d6', BEAT / 2], ['c6', BEAT / 2], ['a#5', BEAT / 2], ['a5', BEAT / 2], ['g5', BEAT / 2], ['a5', BEAT / 2], ['a#5', BEAT / 2], ['c6', BEAT], ['f5', BEAT], ['f5', BEAT], ['f5', BEAT / 2], ['d#5', BEAT / 2], ['d5', BEAT], ['f5', BEAT], ['d6', BEAT], ['d6', BEAT / 2], ['c6', BEAT / 2], ['b5', BEAT], ['g5', BEAT], ['g5', BEAT], ['c6', BEAT / 2], ['a#5', BEAT / 2], ['a5', BEAT], ['f5', BEAT], ['d6', BEAT], ['a5', BEAT], ['a#5', BEAT * 1.5] ] speaker.play(liten_mus) sleep(0.5) print("Also can use a loop to play the tune, one note at a time.") for note in liten_mus: speaker.play(note) #https://projects.raspberrypi.org/en/projects/i