Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Newbie - latching code for momentary switch?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Newbie - latching code for momentary switch?

by cfkreuzer on Thu Jul 30, 2020 9:24 am

Hi there,
I'm new to microcontrollers and coding and what not. I design and build my own guitar FX pedals and want to eventually be able to use a board to offer relay-based bypass switching, MIDI control, I2C display, and digital potentiometer control.
I'm starting with the relay switching because it seems easiest haha! I have messed around with the stock code for digital io and understand how to assign the various pins to input/output and using a basic if/else function, but I can't seem to come up with a code that would let me toggle the state of the LED with the momentary switch where a button push would turn it on indefinitely, then another button push would turn it off indefinitely, if that makes sense. I want code to latch the state of the LED with momentary switch state change.
What library/libraries do I need to explore? Would anyone be able to show me an example? I've searched dozens of Google search results searching for code but have been unsuccessful in finding an example.
Thanks!

BTW I'm using Mu editor with circuitpython on a Seeeduino XIAO that has a SAMD21G18 chip. I also have an Arduino Nano (ATmega328 ) and have tinkered around with IDE but the Mu editor and python code seems WAY easier to use, so far at least.

cfkreuzer
 
Posts: 3
Joined: Thu Jul 30, 2020 9:15 am

Re: Newbie - latching code for momentary switch?

by dastels on Thu Jul 30, 2020 10:11 am

The basic idea is to keep track of the state of the output, say in a variable led_state, which will be True or False. In the main loop you will manipulate that variable and at the end use it to set the output:
Code: Select all | TOGGLE FULL SIZE
led_output.value = led_state


Now for the switch. I would use the debouncer module (see https://learn.adafruit.com/debouncer-library-python-circuitpython-buttons-sensors).

Using a debouncer lets you detect the instant the button is pushed (as opposed to when it is being held down).

You need to create a debouncer object for the input pin:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
from adafruit_debouncer import Debouncer

#Replace these with  constants from the board module 
LED_PIN = ...
SWITCH_PIN = ...

led_output = digitalio.DigitalInOut(LED_PIN)
led_output.direction = digitalio.Direction.OUTPUT

pin = digitalio.DigitalInOut(SWITCH_PIN)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
switch = Debouncer(pin)


Then in your loop, you need to update the debouncer, and ask it if the button has just been pushed.

Code: Select all | TOGGLE FULL SIZE
while True:
    switch.update()
    if switch.fell:
        led_state = not led_state
    led_output.value = led_state


That should get you started.

Dave

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

Re: Newbie - latching code for momentary switch?

by cfkreuzer on Thu Jul 30, 2020 10:50 am

I imported that into Mu and had to shift a few things around, saying led_state wasn't a defined term because that came after its implementation, but when it checked as being without problems, it doesn't seem to be working for me. here's my code:
Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
from adafruit_debouncer import Debouncer

# Replace these with  constants from the board module
LED_PIN = board.D7
SWITCH_PIN = board.D6

led_output = digitalio.DigitalInOut(LED_PIN)
led_output.direction = digitalio.Direction.OUTPUT

pin = digitalio.DigitalInOut(SWITCH_PIN)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
switch = Debouncer(pin)

while True:
    switch.update()
    led_state = led_output.value
    if switch.fell:
        led_state = not led_state

So, what do I seem to be missing? Do I have to actually download that debouncer library or is it integrated into python? I didn't see a link for that library. Thanks again for the help! And I saw you wrote that article as well, it's great!

cfkreuzer
 
Posts: 3
Joined: Thu Jul 30, 2020 9:15 am

Re: Newbie - latching code for momentary switch?

by dastels on Thu Jul 30, 2020 11:39 am

You need to update led_output.value with led_state.

I wrote the debouncer module as well :) Well, ported it from the C++ library for Arduino.

Dave

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

Re: Newbie - latching code for momentary switch?

by cfkreuzer on Thu Jul 30, 2020 10:51 pm

What do you mean by "update"?

dastels wrote:You need to update led_output.value with led_state.

cfkreuzer
 
Posts: 3
Joined: Thu Jul 30, 2020 9:15 am

Re: Newbie - latching code for momentary switch?

by dastels on Thu Jul 30, 2020 10:53 pm

Code: Select all | TOGGLE FULL SIZE
led_output.value = led_state


Dave

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

Please be positive and constructive with your questions and comments.