Need help with this: rainbow animation start only if shaking and being

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dannybalanta
 
Posts: 12
Joined: Thu Dec 31, 2020 7:38 pm

Need help with this: rainbow animation start only if shaking and being

Post by dannybalanta »

Hi everyone, I need some help with the rainbow demo code of the circuit playground bluefruit

I need to activate rainbow demo when CPB is shaked and lights turned off when not shaked, but i can't figure how to do it. What shake function needs my code?

Thanks in advance.

Code: Select all

import time
import board
import neopixel

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
OFF = (0, 0, 0)

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False)

# choose which demos to play
# 1 means play, 0 means don't!
rainbow_demo = 1
rainbow_cycle_demo = 1

def rainbow_cycle(wait):
    for j in range(255):
        for i in range(10):
            rc_index = (i * 256 // 10) + j * 5
            pixels[i] = wheel(rc_index & 255)
        pixels.show()
        time.sleep(wait)

def rainbow(wait):
    for j in range(255):
        for i in range(len(pixels)):
            idx = int(i + j)
            pixels[i] = wheel(idx & 255)
        pixels.show()
        time.sleep(wait)
		
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)

while True:
    if rainbow_cycle_demo:
       rainbow_cycle(0.05)
     
    if rainbow_demo:
       rainbow(0.05)

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need help with this: rainbow animation start only if shaking and being

Post by dastels »

The adafruit_lis3dh module provides a shake detection function. E.g.

Code: Select all

while True:
    if lis3dh.shake(shake_threshold=15):
        print("Shaken!")
You can also access it through the adafruit_circuitplayground module: https://learn.adafruit.com/circuitpytho ... le-2989813

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”