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)
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
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