#Blink Animation - Debounced button

1 messages · Page 1 of 1 (latest)

azure quarry
#

Hi all,
first of all i want let you know, that i´m a beginner in coding. So sorry, if there is a bad question but i need some help to move forward with my "simple" project.
I have a neopixel strip (10 LEDs) connected with pin D12 and a pushbutton with D9. While the pushbutton is pressed, pixels shall blink in RED. If not pressed, pixels shall light up in GREEN.
The code is simple and it works fine:

import board
import neopixel
from digitalio import DigitalInOut, Pull
import adafruit_led_animation.animation.blink as blink_animation
import adafruit_led_animation.color as color

while True:
    if button_in.value:
        pixels.fill(color.GREEN)
        pixels.show()
    else:
       blink.animate()`
```py

Now i want to modify the pushbutton: If the button is pressed once, pixels shall light up in GREEN. If the button is pressed again, the stripe shall blink as long as the button is pressed again. I tried it with debouncing and the following code is based on the code of the video from Prof G. --> https://www.youtube.com/watch?v=tjg4R-P88q0&list=PL9VJ9OpT-IPSsQUWqQcNrVJqy4LhBjPX2&index=43YouTubeJohn GallaugherHelp My Pico Button Doesn't Work! Fix multiple press reporting w/Bl...

Makers often learn button basics, only to see their buttons fail when seemingly properly set up and coded. This can be incredibly frustrating. This lesson shows how button events are registered, why problems occur, and how to solve them with a while / pass blocking statement, and with debouncing techniques using the adafruit_debouncer library. W...

▶ Play video
#
import board
import neopixel
import digitalio
import time 
from adafruit_led_animation.sequence import AnimationSequence
import adafruit_led_animation.animation.blink as blink_animation
import adafruit_led_animation.color as color
from adafruit_debouncer import Button

pixels = neopixel.NeoPixel(board.D12, 30, brightness=0.05)
blink = blink_animation.Blink(pixels, 0.5, color.GREEN)

animations = False

button_input = digitalio.DigitalInOut(board.D9)
button_input.switch_to_input(pull=digitalio.Pull.UP)
button = Button(button_input)


while True:
    button.update()
    if button.pressed:
        print("button pressed")
        if animations:
            animations = False
            print("animations turned OFF")
            pixels.fill(color.RED)
            pixels.show()
           
        else:
            animations = True
            print("Animations turned ON")
            blink.animate()
            
```py

If I turn the microcontroller on, at first the lights are black/off. 
By pushing the button it turns RED but it is not blinking. Pushing again the leds turn GREEN. Pushing again it is black/off. Then green - red - green - off - green - red - green - off and so on.

Where is the issue? Any Ideas? Thank you in advance!
forest tinsel
#

The blink animation will light the color and then turn off (over and over).

#

Do you only want to blink when the button is being held down?

forest tinsel
#

When using Debouncer, you can use Button.pressed and Button.released for the same functionality.

azure quarry
#

Thanks for your answer. What I want to do is that the animation turns on if i "clicked" the button:

  1. pushed and released = animation on (blinking)
  2. pushed and released = pixels.fill
  3. pushed and released = animation on (blinking)
    and so on

the button should work as a "click" button - by any click (pressed and released), the animation should turn off/on. If animation is turned off, pixels shall light up continuously in one color.

forest tinsel
#

Were you able to get it working?

azure quarry
#

No. It´s unbelievable, that such a simple thing is so hard to solve... 😄
Has anyone an example for "click" button?

forest tinsel
#

So, basically you need to keep up with the state which I believe you are doing with animations in your example code above.

forest tinsel
#
import board
import neopixel
import digitalio
import time 
from adafruit_led_animation.sequence import AnimationSequence
import adafruit_led_animation.animation.blink as blink_animation
import adafruit_led_animation.color as color
from adafruit_debouncer import Button

pixels = neopixel.NeoPixel(board.D12, 30, brightness=0.05)
blink = blink_animation.Blink(pixels, 0.5, color.GREEN)

animations = False

button_input = digitalio.DigitalInOut(board.D9)
button_input.switch_to_input(pull=digitalio.Pull.UP)
button = Button(button_input)

#Initialize to show all red
pixels.fill(color.RED)
pixels.show()

while True:
    button.update()
    if button.pressed:
        print("button pressed")
        if animations:
            animations = False
            print("animations turned OFF")
            pixels.fill(color.RED)
            pixels.show()           
        else:
            animations = True
            print("Animations turned ON")
            blink.animate()