I've created a custom module to avoid the need to use the "Execute" block. It basically gives you a way to set the pin number, trigger, and callback for the IRQ. You will still create your own function using Blockly.
- Create a Variable: example "counter"
- Create a Function:
- Name it what you like, example: "count_pulses"
- Add single step from Variables: change counter by 1
- Init PWM0 with your desired pin, freq, and duty
- Load the attached file under Custom | Open *.m5b file
- Drag "PIN_IRQ" from Custom onto your workspace
- Set the pin number (just an integer, 0 for example)
- Set your trigger "machine.Pin.IRQ_RISING"
- Set the handler to your function from step 2 above "count_pulses"
- Add a label if you want and set the loop event to update a label with the current counter value
- It sounds more complicated than it actually is, check the screenshot
Your resulting code should look vaguely like this:
from m5stack import * from m5ui import * from uiflow import * import machine setScreenColor(0x222222) counter = None label0 = M5TextBox(146, 187, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0) from numbers import Number def count_pulses(): global counter counter = (counter if isinstance(counter, Number) else 0) + 1 PWM0 = machine.PWM(26, freq=25000, duty=50, timer=0) pin0.irq(trigger=machine.Pin.IRQ_RISING, handler=count_pulses) while True: label0.setText(str(counter)) wait_ms(2)
Have fun storming the castle!