short button press VS long button press

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jakefreese
 
Posts: 20
Joined: Sun Jan 12, 2014 10:23 am

short button press VS long button press

Post by jakefreese »

I have been trying to figure out how to differ from a long button press to a short. This is on a Trinket M0

I was thinking before the while loop to setup the events of a short or long press.

This thing is pretty simple.

Short button press changes on board dot led color (red, green, blue)
Long button press increase dim level 0.1 per 0.5sec to 1, then back to 0.2

I eventually want to make this shutdown and go to low power after 30 minutes and wake back up on a button press.

trying to create the events for short and long button press is what really has me hung up at the moment. That and the color change off the short press.....

On the color change I thought that it would be able to count through the list. Some previous revision I had it run through the three once I pushed the button, but I could not get it to increment through each push.

Does anyone have any ideas on how to approach this ? Thanks!!

Code: Select all

import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogOut, AnalogIn
import touchio
import adafruit_dotstar as dotstar
import time

dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
dot.brightness = 0.2

def countup():
    global counter
    counter1 = (counter1 + 1) % len(LEDC)
    coutner2 = (counter2 + 0.1) % len(dot.brightness)
counter1 = 0
counter2 = 0.2
LEDC = ["RED, GRN, BLU"]

RED = (255, 0, 0)
GRN = (0, 255, 0)
BLU = (0, 0, 255)

analog1in = AnalogIn(board.D0)
aout = AnalogOut(board.D1)

button = DigitalInOut(board.D2)
button.direction = Direction.INPUT
button.pull = Pull.UP

touch1 = touchio.TouchIn(board.D3)
touch2 = touchio.TouchIn(board.D1)

countup()


while True:
    if touch1.value:  # short button press <0.5 sec
        counter1 += 1
        print(counter1)
        print(LEDC[counter1])
        #  I am trying to get this to cycle through the colors ( RED, GRN, BLU)
        LEDC[counter]()
        dot[0] = counter1
        time.sleep(0.2)
        
        if touch1.value:  # long button press >1 sec
            counter2 += 1   # I think this needs be be different counter for dim
            print(DIM[counter2])
            DIM[counter2]()
            DIM(dot.brightness)     
            # increase dim 0.1 for each 0.5 sec button is pushed

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

Re: short button press VS long button press

Post by dastels »

The debounder module can help. It can give you transitions (when something goes from false->true or true->false) as well as how long it's been in it's previous and current state. See https://github.com/adafruit/Adafruit_Ci ... _Debouncer and https://learn.adafruit.com/debouncer-li ... ns-sensors (this covers the basics and was written before the time information was added).

Dave

User avatar
jakefreese
 
Posts: 20
Joined: Sun Jan 12, 2014 10:23 am

Re: short button press VS long button press

Post by jakefreese »

Thank you Dave,

I will work through those and see if I can get this figured out.

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

Return to “Adafruit CircuitPython”