From 49ec77c5ca769f861881ddefd6607ce77b2073eb Mon Sep 17 00:00:00 2001 From: Ron Rossi <73606166+KK1L@users.noreply.github.com> Date: Wed, 13 Sep 2023 18:15:36 -0400 Subject: [PATCH] Create Balls-n-Noise_DualCore An example to show use of both cores of the Pico as well as a potentiometer to control program execution. --- .../pico_explorer/Balls-n-Noise_DualCore | 245 ++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 micropython/examples/pico_explorer/Balls-n-Noise_DualCore diff --git a/micropython/examples/pico_explorer/Balls-n-Noise_DualCore b/micropython/examples/pico_explorer/Balls-n-Noise_DualCore new file mode 100644 index 000000000..9ad5c77d1 --- /dev/null +++ b/micropython/examples/pico_explorer/Balls-n-Noise_DualCore @@ -0,0 +1,245 @@ +This example combines balls_demo.py and noise.py by using both cores and reading a potentiometer input to change the speed of the display and song +# We're creating 100 balls with their own individual colour and 1 BG colour +# for a total of 101 colours, which will all fit in the custom 256 entry palette! + +# This example shows you how you can use Pico Explorer's onboard buzzer as a speaker to play different notes and string them together into a bleepy tune. +# It uses code written by Avram Piltch - check out his Tom's Hardware article! https://www.tomshardware.com/uk/how-to/buzzer-music-raspberry-pi-pico +# You'll need to connect a jumper wire between GPO and AUDIO on the Explorer Base to hear noise. + +# Create a buzzer on pin 0 +# Don't forget to wire GP0 to AUDIO! + +# Analogue Dial on a Pimoroni Pico Explorer with Pico Graphics +# 10 K Ohm potentiometer center wiper connection on ADC(0) = GP26 +# Don't forget to wire 3V3 and GND to the potentiometer limit connections + +import time +import random +from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER, PEN_P8 +from pimoroni import Buzzer +import machine +import _thread +from time import sleep + +display = PicoGraphics(display=DISPLAY_PICO_EXPLORER, pen_type=PEN_P8) +display.set_backlight(1.0) + +WIDTH, HEIGHT = display.get_bounds() + +potentiometer = machine.ADC(26) # Install potentiometer + +class Ball: + def __init__(self, x, y, r, dx, dy, pen): + self.x = x + self.y = y + self.r = r + self.dx = dx + self.dy = dy + self.pen = pen + + +# initialise shapes +balls = [] +for i in range(0, 100): + r = random.randint(0, 10) + 3 + balls.append( + Ball( + random.randint(r, r + (WIDTH - 2 * r)), + random.randint(r, r + (HEIGHT - 2 * r)), + r, + (14 - r) / 2, + (14 - r) / 2, + display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), + ) + ) + +BG = display.create_pen(40, 40, 40) + +BUZZER = Buzzer(0) + +BLACK = display.create_pen(0, 0, 0) +GREEN = display.create_pen(0, 255, 0) + +# this handy list converts notes into frequencies, which you can use with the explorer.set_tone function +tones = { + "B0": 31, + "C1": 33, + "CS1": 35, + "D1": 37, + "DS1": 39, + "E1": 41, + "F1": 44, + "FS1": 46, + "G1": 49, + "GS1": 52, + "A1": 55, + "AS1": 58, + "B1": 62, + "C2": 65, + "CS2": 69, + "D2": 73, + "DS2": 78, + "E2": 82, + "F2": 87, + "FS2": 93, + "G2": 98, + "GS2": 104, + "A2": 110, + "AS2": 117, + "B2": 123, + "C3": 131, + "CS3": 139, + "D3": 147, + "DS3": 156, + "E3": 165, + "F3": 175, + "FS3": 185, + "G3": 196, + "GS3": 208, + "A3": 220, + "AS3": 233, + "B3": 247, + "C4": 262, + "CS4": 277, + "D4": 294, + "DS4": 311, + "E4": 330, + "F4": 349, + "FS4": 370, + "G4": 392, + "GS4": 415, + "A4": 440, + "AS4": 466, + "B4": 494, + "C5": 523, + "CS5": 554, + "D5": 587, + "DS5": 622, + "E5": 659, + "F5": 698, + "FS5": 740, + "G5": 784, + "GS5": 831, + "A5": 880, + "AS5": 932, + "B5": 988, + "C6": 1047, + "CS6": 1109, + "D6": 1175, + "DS6": 1245, + "E6": 1319, + "F6": 1397, + "FS6": 1480, + "G6": 1568, + "GS6": 1661, + "A6": 1760, + "AS6": 1865, + "B6": 1976, + "C7": 2093, + "CS7": 2217, + "D7": 2349, + "DS7": 2489, + "E7": 2637, + "F7": 2794, + "FS7": 2960, + "G7": 3136, + "GS7": 3322, + "A7": 3520, + "AS7": 3729, + "B7": 3951, + "C8": 4186, + "CS8": 4435, + "D8": 4699, + "DS8": 4978 +} + +# put the notes for your song in here! +song = ["AS6", "A6", "AS6", "P", "AS5", "P", "AS5", "P", "F6", "DS6", "D6", "F6", "AS6", "A6", "AS6", "D7", "C7", "AS6", "C7", "P", "C6", "P", "C6", "P", "C6", "AS5", "A5", "C6", "F6", "P", "F6", "P", "G6", "A6", "AS6", "A6", "G6", "F6", "G6", "F6", "DS6", "D6", "DS6", "D6", "C6", "AS5", "AS5", "A5", "G5", "F5", "G5", "AS5", "A5", "C6", "AS5", "D6", "C6", "DS6", "D6", "P", "AS5", "P", "AS5"] + +def clear(): # this function clears Pico Explorer's screen to black + display.set_pen(BLACK) + display.clear() + display.update() + + +def playtone(frequency): # this function tells your program how to make noise + BUZZER.set_tone(frequency) + + +def bequiet(): # this function tells your program how not to make noise + BUZZER.set_tone(-1) + + +def playsong(song): # this function plays your song + a = 0 # this variable keeps track of the visualiser bars + for i in range(len(song)): + pot = potentiometer.read_u16()/256 + pot = int(pot * 256.0 /255.0) - 1 + pot = int(pot *100 / 255) + if pot > 100: + pot = 100 + if pot < 1: + pot = 1 + + pausetime = 0.1 + pot * 0.002 + + if (song[i] == "P"): + bequiet() + else: + playtone(tones[song[i]]) + a += 7 + if a >= 240: # clears the screen if the green bars reach the right hand edge + clear() + a = 0 + time.sleep(pausetime) # change this number if you want to alter how long the notes play for + bequiet() + +sLock = _thread.allocate_lock() + +def Core0Task(): + while True: + display.set_pen(BG) + display.clear() + + pot = potentiometer.read_u16()/256 + pot = int(pot * 256.0 /255.0) - 1 + pot = int(pot *100 / 255) + if pot > 100: + pot = 100 + if pot < 1: + pot = 1 + + pausetime = 0.001 * pot + + for ball in balls: + ball.x += ball.dx + ball.y += ball.dy + + xmax = WIDTH - ball.r + xmin = ball.r + ymax = HEIGHT - ball.r + ymin = ball.r + + if ball.x < xmin or ball.x > xmax: + ball.dx *= -1 + + if ball.y < ymin or ball.y > ymax: + ball.dy *= -1 + + display.set_pen(ball.pen) + display.circle(int(ball.x), int(ball.y), int(ball.r)) + + display.set_pen(250) + display.set_font("bitmap8") + display.text(str(int(100-pot+1))+"%",5,5,scale=5) + + display.update() + time.sleep(pausetime) + +def Core1Task(): + while True: + playsong(song) + +_thread.start_new_thread(Core1Task, ()) + +Core0Task()