Execution not continuing after button interrupt.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
AlanE
Posts: 2
Joined: Mon Jun 22, 2020 7:41 pm

Execution not continuing after button interrupt.

Post by AlanE » Mon Jun 22, 2020 7:48 pm

[update]
Issue resolved, lack of planning and lack of attention to detail were the culprits.
-------------------------------------


I am just learning micropython on the pyboard. I have a rowing machine and i am building a new computer for it to give me some of the data that the cheap one it came with does not have, namely strokes/minute.


there is a simple switch on the machine that registers when the seat goes back and forth so I am simulating that with the built in usr switch on the pyboard. I found some code on line that i am trying to repurpose. The problem i am having is that once I press the button it increments the counter but then it seems to stop. I am sure the code looks a mess but I am trying to work out the basics before I refactor it.

Code: Select all

from machine import Pin, Timer
from pyb import delay


import micropython
micropython.alloc_emergency_exception_buf(100)


#counter to be incremented each time a stroke is detected
stroke_count = 0

def on_pressed(timer):
    global stroke_count
    stroke_count += 1

def debounce(pin):
    # Start or replace a timer for 200ms, and trigger on_pressed.
    stroke_calc_timer.init(mode=Timer.ONE_SHOT, period=50, callback=on_pressed)

def calc_stroke_enable(timer):
    print("calc_stroke reached")
    global calc_stroke
    calc_stroke = True


# Register a new hardware timer.
# create a timer that will tell the sytem it is time to calcualte the
# stroke count
stroke_calc_timer = Timer(-1)
# initialise it
stroke_calc_timer.init(mode=Timer.PERIODIC, period=5000, callback=calc_stroke_enable)

#10 second timer
tenSecond_timer = Timer(-1)

#flag to alert main that the time interval is up to so calculate stroke rate
calc_stroke = False


# Setup the button input pin with a pull-up resistor.
button = Pin('X17', Pin.IN, Pin.PULL_UP)

# Register an interrupt on rising button input.
button.irq(debounce, Pin.IRQ_RISING)

def main():
    global calc_stroke
    global stroke_count
    if calc_stroke == True:
        print("count is true")
        print("strokes " + str(stroke_count))
        calculated_stroke = stroke_count * 6
        stroke_count = 0 # reset for next interval
        calc_stroke = False # reset to wait for another 10s
        print("calculated stroke = " + str(calculated_stroke))

while True:
    main()
    delay(50)
output is as follows

Code: Select all

icroPython v1.12-537-gecd782631 on 2020-06-15; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>>
MPY: sync filesystems
MPY: soft reboot
calc_stroke reached
count is true
strokes 0
calculated stroke = 0
calc_stroke reached
count is true
strokes 0
calculated stroke = 0
As soon as i press the button the output stops.

Is there something that has to be done after the interrupt happens to pass control back to the main program or am I fundamentally misunderstanding how the interrupts and timers should be used?

Thank you for any assistance.
Alan
Last edited by AlanE on Fri Jun 26, 2020 6:40 pm, edited 2 times in total.

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

Re: Execution not continuing after button interrupt.

Post by dhylands » Mon Jun 22, 2020 8:31 pm

Why does your on_pressed call main? on_pressed is your ISR callback routine. When it runs the timer interrupts are disabled. When you turn around and call main from the callback, main is now running with timer interrupts disabled.

AlanE
Posts: 2
Joined: Mon Jun 22, 2020 7:41 pm

Re: Execution not continuing after button interrupt.

Post by AlanE » Mon Jun 22, 2020 9:25 pm

Sorry, i left that in there by accident, i thought maybe you needed to call something after your handler to go back to the main program, I was throwing things at the wall to see what would stick so I tried that and then removed it. Not sure how it ended up in there. I will edit the post and take it out for clarity.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Execution not continuing after button interrupt.

Post by SpotlightKid » Mon Jun 22, 2020 9:40 pm

What are you replying to?

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

Re: Execution not continuing after button interrupt.

Post by dhylands » Mon Jun 22, 2020 10:51 pm

SpotlightKid wrote:
Mon Jun 22, 2020 9:40 pm
What are you replying to?
Sorry - my fault - that was a moderated post and I forgot to click approve before replying.

Post Reply