0

Reversing button trigger state
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Reversing button trigger state

by michaelw3 on Sat May 28, 2022 11:30 am

I suspect that I'm missing something super obvious here, but I can't quite seem to run it down.

My goal is to trigger a countdown timer with lights and sound. Everything works, except the external trigger button state is reversed. Right now the countdown triggers if I do not push the button. If the button is pushed, the timer will not start. I want the opposite behavior (pushing the button triggers the countdown) The code is below, but before I get to it I'll describe the two things I have tried.

1. I tried to uncomment out line 22 and comment out line 23 so that it became
Code: Select all | TOGGLE FULL SIZE
Pull.DOWN
instead of
Code: Select all | TOGGLE FULL SIZE
Pull.UP
. When I made that change the timer would not trigger at all.

2. I tried to change
Code: Select all | TOGGLE FULL SIZE
if button.value:
to both
Code: Select all | TOGGLE FULL SIZE
if button.value = FALSE:
and if
Code: Select all | TOGGLE FULL SIZE
button.value = False:
. Both of those threw up line errors.

Thank you for any help!

Code: Select all | TOGGLE FULL SIZE
# Circuit Playground NeoPixel
import time
import board
import neopixel
import digitalio

#stuff specific to the sound part
import array
import math

####Light'n button stuff ######

#set up the pixels
pixels = neopixel.NeoPixel(board.A2, 30, brightness=0.8, auto_write=False)

#sets the button variable
#button = digitalio.DigitalInOut(board.BUTTON_A)
#button = digitalio.DigitalInOut(board.A5)
button = digitalio.DigitalInOut(board.A5)

#makes pushing the button activate it
#button.switch_to_input(pull=digitalio.Pull.DOWN)
button.switch_to_input(pull=digitalio.Pull.UP)


led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

#color variables
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)
BLACK = (0, 0, 0)

color_chase_wait_time = 0.02

#####Audio stuff########

try:
    from audiocore import RawSample
except ImportError:
    from audioio import RawSample

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

FREQUENCY = 440  # 440 Hz middle 'A'
SAMPLERATE = 8000  # 8000 samples/second, recommended!

# Generate one period of sine wav.
length = SAMPLERATE // FREQUENCY
sine_wave = array.array("H", [0] * length)
for i in range(length):
    sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15)

# Enable the speaker
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.direction = digitalio.Direction.OUTPUT
speaker_enable.value = True

audio = AudioOut(board.SPEAKER)
sine_wave_sample = RawSample(sine_wave)

def yellow_flash(FLASH_TIME):
    pixels.fill(BLACK)
    pixels.show()
    time.sleep(FLASH_TIME)

    pixels.fill(YELLOW)
    pixels.show()
    time.sleep(FLASH_TIME)



#####rest of code######

def color_chase(color, wait):
    for i in range(30):
        pixels[i] = color
        time.sleep(wait)
        pixels.show()
    #time.sleep(1)

while True:
    if button.value: #if button is pushed

        #green during the first ~40 seconds? (need to update time for all of this)
        pixels.fill(GREEN)
        pixels.show()
        led.value = True
        time.sleep(45)


        #yellow flashing for ~10 seconds?
        pixels.fill(YELLOW)
        pixels.show()
        time.sleep(2)

        yellow_flash(.5)
        yellow_flash(.5)
        yellow_flash(.5)
        yellow_flash(.5)
        yellow_flash(.5)
        yellow_flash(.5)
        yellow_flash(.5)
        yellow_flash(.5)



        #color chase for last ~5 seconds?
          # Increase the number to slow down the color chase
        color_chase(YELLOW, color_chase_wait_time)
        color_chase(CYAN, color_chase_wait_time)
        color_chase(BLUE, color_chase_wait_time)
        color_chase(PURPLE, color_chase_wait_time)
        color_chase(BLACK, color_chase_wait_time)
        color_chase(RED, color_chase_wait_time)

        #will beep for the numer of seconds in time.sleep()
        audio.play(sine_wave_sample, loop=True)
        time.sleep(2)
        audio.stop()

        #red time's up before next start
        pixels.fill(RED)
        pixels.show()
        time.sleep(3)

    else:
        pixels.fill(BLACK)
        pixels.show()
        led.value = False
    time.sleep(0.01)

michaelw3
 
Posts: 56
Joined: Sun Mar 10, 2013 6:58 pm

Re: Reversing button trigger state

by dastels on Sat May 28, 2022 11:44 am

It doesn't really matter, does it? You just have to be clear on what button state/value reflects being pressed vs not being pressed.

That said, it does seem a little backwards and the reason is that early MCUs (e.g. the '328 in the Arduino UNO) only had pullups. So you enabled the pullup and had the switch connect the pin to ground when pushed. It sort of stuck around. For CircuitPython it isn't relevant, but if you want to write C++ code that is backward compatible to those early MCUs, it matters.

As for your code, use
Code: Select all | TOGGLE FULL SIZE
if not button.value:


The line
Code: Select all | TOGGLE FULL SIZE
if button.value = False:
should be
Code: Select all | TOGGLE FULL SIZE
if button.value == False:
but since it's a boolean you should skip the comparison and just use the value itself, as shown above. The result of a comparison is True/False and that's what the value already is.

Dave

dastels
 
Posts: 10114
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reversing button trigger state

by Franklin97355 on Sat May 28, 2022 11:48 am

How do you have the button wired?

Franklin97355
 
Posts: 22663
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Reversing button trigger state

by michaelw3 on Sat May 28, 2022 11:54 am

THANK YOU!

That worked perfectly.

michaelw3
 
Posts: 56
Joined: Sun Mar 10, 2013 6:58 pm

Please be positive and constructive with your questions and comments.