Responding only if button pressed 3 times in set period of time??

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Responding only if button pressed 3 times in set period of time??

Post by brett » Tue Jan 17, 2017 3:03 am

I'm learning enough to be dangerous. I'm trying to get the esp8266 to only light the redled if the button is pressed 3 times in a certain period of time. I thought I figured out a way to make it work using a timer callback but I found that you can't change a global variable from a function. I thought if I could have the callback reset the buttonCounter variable to 0 after the time limit it would serve the purpose. I've been reading about bytearrays but it's not clicking. Any suggestions on how I could get this to work? Also if you see anything terribly wrong with the rest of the code I'm open to learning better ways of doing things.

--Brett

Code: Select all

import machine
import network
import time
from machine import Timer

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('xxxxxxxxxxxx', 'xxxxxxxxx')

button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
redled = machine.Pin(5, machine.Pin.OUT)
yelled = machine.Pin(12, machine.Pin.OUT)
grnled = machine.Pin(4, machine.Pin.OUT)

buttonCounter = 0


def helpRequest():
    from machine import Timer
    yelled(1)
    tim = Timer(-1)
    tim.init(period=60000, mode=Timer.ONE_SHOT, callback=lambda t:yelled(0))

def emergencyRequest():
    from machine import Timer
    redled(1)
    tim2 = Timer(1)
    tim2.init(period=60000, mode=Timer.ONE_SHOT, callback=lambda t:redled(0))

def buttonReset(self):
    buttonCounter = 0
    print('reset')
    print(buttonCounter)
 
while True:
    first = button.value()
    time.sleep(0.05)
    second = button.value()
    if first and not second:
        print('Button pressed!')
        buttonCounter = buttonCounter + 1
        print(buttonCounter)
        helpRequest()
        tim3 = Timer(2)
        tim3.init(period=5000, mode=Timer.ONE_SHOT, callback=buttonReset)
    if buttonCounter >= 3:
        emergencyRequest()
        buttonCounter = 0
    if wlan.isconnected() == True:
        grnled(1)
    elif wlan.isconnected() == False:
        grnled(0)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Responding only if button pressed 3 times in set period of time??

Post by dhylands » Tue Jan 17, 2017 3:08 am

You can change a global variable from a function by using the global statement:

Code: Select all

counter = 1

def foo():
    global counter
    counter += 1

print(counter)
foo()
print(counter)

brett
Posts: 14
Joined: Sun Jan 15, 2017 3:17 am

Re: Responding only if button pressed 3 times in set period of time??

Post by brett » Tue Jan 17, 2017 3:21 am

Thank you...Thank you....Thank you!!! That was so easy and works well.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Responding only if button pressed 3 times in set period of time??

Post by pythoncoder » Tue Jan 17, 2017 7:43 am

This is one of the tricky aspects of Python and can lead to bugs. Consider the following:

Code: Select all

flag = False
def foo():
	flag = True
This will compile and run, but won't do what the programmer intended. foo() will create a local variable called flag, and set it True. The global variable will be unchanged. The fix is

Code: Select all

flag = False
def foo():
	global flag
	flag = True
It is a gonk trap I've fallen into on a number of occasions :(
Peter Hinch
Index to my micropython libraries.

Post Reply