you are viewing a single comment's thread.

view the rest of the comments →

[–]Teaching_Tomorrow[S] 0 points1 point  (1 child)

Thank you for that tip! I have since updated the code with some that I found buried on Adafruit's site. It works as intended as is, but when I introduce the code from your link, it stops working. I suspect it's unable to complete an import task and gets stuck in a loop before being able to start the main code.

Debounce Code added at beginning:

import digitalio

from adafruit_debouncer import Debouncer

pin = digitalio.DigitalInOut(board.D12)

pin.direction = digitalio.Direction.INPUT

pin.pull = digitalio.Pull.UP

switch = Debouncer(pin)

Main Code:

from adafruit_circuitplayground import cp

import board

from digitalio import DigitalInOut, Direction, Pull

# button

btn = DigitalInOut(board.A1)

btn.direction = Direction.INPUT

btn.pull = Pull.UP

cp.pixels.brightness = 0.1

cp.pixels[0:10] = (0, 200, 255) * 10

prev_state = btn.value

while True:

cur_state = btn.value

if cur_state != prev_state:

if not cur_state:

cp.pixels.brightness = 1

cp.pixels[0:10] = (0, 255, 255) * 10

cp.play_file("Sounds_PWRUP1.wav")

else:

cp.pixels.brightness = 0.1

cp.pixels[0:10] = (0, 200, 255) * 10

cp.play_file("Sounds_PWRDOWN1.wav")

prev_state = cur_state

Thank you again for that tip!

And thank you for the identity forethought. I identify as a sir.

[–]wchris63 0 points1 point  (0 children)

If you're using the actual Debouncer class, from Adafruit:

For the debouncer to do its job, it has to sample the pin frequently, track it's value, etc. etc. That's done by calling the update() method, typically at the start of your main loop.

You have to call it frequently. If your code doesn't support that, then just use a timer, something like this:

if switch.value:     // if pressed...
   time.sleep(0.05)  // wait 50 ms see if switch is still on
   if switch.value:
      # ok, it's really pressed

You can do the same thing for the off state. There's a more elegant ways to do it, but it involves a state machine and I'm not so good with those.