Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

CPB capacitive touch and trigger
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CPB capacitive touch and trigger

by Hethacker on Sun Jun 28, 2020 11:39 pm

Hello,
I am trying to use circuitpython to light up a cardboard sword. The program works, in that if I touch the copper tape it will turn on, but I want it to ONLY be on when I am holding the copper tape, and as soon as I let go, it will stop playing the lights. Any help is appreciated.

Code: Select all | TOGGLE FULL SIZE
from adafruit_circuitplayground.bluefruit import cpb
import time
import board
import neopixel

pixels = neopixel.NeoPixel(board.D0, 60, brightness=0.2, auto_write=False)
cpb.pixels.brightness = 0.02
# choose which demos to play
# 1 means play, 0 means don't!
#color_chase_demo = 1

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)

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)




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


while True:
    if cpb.touch_A1:
        print("Touched A1!")
        cpb.pixels.fill((BLUE))
        rainbow_cycle_demo = 1
        if rainbow_cycle_demo:
            rainbow_cycle(0.01)  # Increase the number to slow down the rainbow.
    time.sleep(0.1)
    if not cpb.touch_A1: 
        cpb.pixels.fill((OFF))
        rainbow_cycle_demo = 0

Hethacker
 
Posts: 43
Joined: Fri Apr 05, 2019 9:02 pm

Re: CPB capacitive touch and trigger

by dherrada on Tue Jun 30, 2020 1:23 pm

Add a cpb.pixels.show() after cpb.pixels.fill((OFF))

dherrada
 
Posts: 99
Joined: Thu Jan 04, 2018 4:59 pm

Please be positive and constructive with your questions and comments.