#I m using a captouch brass brad as a
1 messages · Page 1 of 1 (latest)
code.py: ```
import board, time, digitalio, neopixel, random
from digitalio import DigitalInOut, Direction, Pull
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.color import RED
from twinkle import twinkle
from cap_touch import brass_brad
from ultrasonic import ping
from comet import go_comet
from blackholes import blackhole_slow, blackhole_fast, blackhole_reverse
Variables
Onboard LED
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
last_blink = time.monotonic()
Setup
print()
print("Be Kind Rewind Test")
while True:
# Board blink
if time.monotonic() - last_blink > 2.0: # every 2 seconds
last_blink = time.monotonic() # save when we do the thing
# print("blink") # do thing
led.value = True
else:
led.value = False
brass_brad()
twinkle() # Adjustments in twinkle.py.
ok so they are called by the cap touch (copying it here)
import time
import board
import touchio
from codex_fire_logs_v3 import fire_logs, markers
from blackholes import blackhole_slow, blackhole_fast, blackhole_reverse
touch_pad = board.GP28_A2
touch = touchio.TouchIn(touch_pad)
def brass_brad():
markers()
blackhole_slow()
count = 0
prev_state = touch.value
cur_state = touch.value
if cur_state != prev_state:
if not cur_state:
#print("On")
blackhole_fast()
fire_logs()
blackhole_reverse()
else:
#print("Off")
#blackhole_slow()
pass
prev_state = cur_state
markers()
so in order to not use sleep, you need to write your function with the idea that they are called repeatedly, so each succession of actions needs to be in a separate "state". It might be easier to rewrite using asyncio then if it's available, what board is it ?
raspberry pi pico
then you should probably use asyncio
Side note, I see you on Discord all the time helping people out. Can you receive a "buy you a coffee" gift card or anything like that as a thank you?
no
I'm looking up the asyncio library now
with asyncio you can mostly write code that uses sleep (asyncio.sleep) you just have to separate into different tasks the parts that are independent
why did you want to write it without sleep ?
You can see in code.py that I import other functions called twinkle() and brass_brad(). twinkle(), twinkles one neopixel strip and brass_brad() animates another neopixel strip. I'm struggling to get the twinkle to not freeze up while the brass_brad is running.
I figured I needed to avoid sleep so I wouldn't cause anymore freezing issues.
so I can't test the code, there might be mistakes, but here is an idea:
import asyncio
def blackhole_slow():
# print("Slow spin")
blackhole_1.throttle = 0.1
blackhole_2.throttle = 0.6
async def blackhole_fast():
blackhole_1.throttle = 0.3
blackhole_2.throttle = 1.0
await asyncio.sleep(10)
blackhole_1.throttle = -0.3
blackhole_2.throttle = -1.0
await asyncio.sleep(10)
blackhole_slow()
async def brass():
blackhole_slow()
past_touch = touch.value
blackhole_task = None
while True:
current_touch = touch.value
if current_touch and current_touch != past_touch
if blackhole_task and not blackhole_task.done:
blackhole_task = asyncio.create_task(blackhole_fast())
current_touch = past_touch
asyncio.sleep(0.05)
async def twinkle_task():
while True:
# do whatever twinkle does
print("twinkle")
# for example using the adafruit_led_animation library
twinkle_animation.animate()
asyncio.sleep(0.05)
async def main():
# wait for all tasks to finish (which they don't)
asyncio.gather(
asyncio.create_task(brass())
asyncio.create_task(twinkle_task())
)
asyncio.run(main())
now I don't actually know how the motor libraries work I'm not used to it
the brass task starts the motors at a slow spin, then wait in a loop for a touch to be detected, if it is it starts a new blackhole_fast task
blackhole_task here does your "fast" and "reverse" then returns to "slow"
it also only starts a new fast is there isn't one already running
all this time twinkle runs in "parallel"
now to change how things run you can cancel tasks, start new tasks, or change shared (or global) variables
I'll try this out. I really appreciate your help and this is the MOST helpful Discord channel I'm on. It's amazing.
and for the blink you can add:
def blink():
while True:
led.value = not led.value
await asyncio.sleep(2)
and
asyncio.create_task(blink())
in the gather() call in main()
the blink is just there so that I can physically see that the board is up and running.
yeah, I do that too
it's the "print" of microcontrollers
(though we can also use print of course)
now, what twinkle() does needs to be async compatible too, so it doesn't run alone
in asyncio, you have tasks that run each on their turn, you need to call await asyncio.sleep() even if with 0 to give a turn to the other tasks
twinkle is a moded version of Todbot's fire animation. # Twinkle for Be Kind Rewind
fire_with_ulab.py -- simple Neopixel fire animation using ulab (numpy)
10 Oct 2022 - @dreamy karma / Tod Kurt
part of https://github.com/todbot/circuitpython_led_effects
I'm on that page now! haha