I would like to ask you for help with "Global Function".
What I need:
I need a program to react on impulse (machine.IN) from PWM (also generated by ESP32) to count impulse and when the count reaches a specific amount (like 100, 10, 32000 or different) then break while cycle in "Blink.py".
what I use:
Board: ESP32 (LED on pin 2)
Micropython Firmware:
VS Code:
Version: 1.43.0 (user setup)
Commit: 78a4c91400152c0f27ba4d363eb56d2835f9903a
Date: 2020-03-09T19:47:57.235Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.18363
I prepared a simplified example of my design:
Designed structure:
My codes:
Boot.py:
Code: Select all
import machine
import utime
LED_PWM = machine.PWM(machine.Pin(2, machine.Pin.OUT))
LED_PWM.deinit()
LED_PWM.init()
utime.sleep(0.1)
Counter = 0
Main.py
Code: Select all
def Count(pin):
global Counter
Counter = Counter + 1
return(Counter)
import machine
LED_Count = machine.Pin(26, machine.Pin.IN)
LED_Count.irq(handler=Count, trigger=machine.Pin.IRQ_FALLING)
import Blink
Blink.Blink()
- meaning of this code is to Run external function "Blink" one-time-only (in real project call of "Blink.py" will be multiple times)
Blink.py
Code: Select all
def Blink():
import machine
import math
import Wait_Function
import LED_Start
import LED_Stop
Counter = 0
LED_PWM = machine.PWM(machine.Pin(2, machine.Pin.OUT))
while True:
print(str(Counter))
Wait_Function.Wait_Time()
if Counter<10:
LED_Start.LED_Run(5, LED_PWM)
else:
LED_Stop.LED_Stop(LED_PWM)
break
1) Assign Value into Global variable "Counter"
2) Run while cycle till the "Counter" reach 10
LED_Start.py
Code: Select all
def LED_Run(speed, LED_PWM):
LED_PWM.deinit()
LED_PWM.init()
LED_PWM.freq(speed)
LED_PWM.duty(512)
LED_Stop.py
Code: Select all
def LED_Stop(LED_PWM):
LED_PWM.deinit()
Wait_Function.py
Code: Select all
def Wait_Time():
import utime
Counter = 0
while Counter < 5:
utime.sleep(1)
Counter = Counter + 1
Result of my code:
- While look in "Blink.py" is running forever because "Counter =0" (not updating)
- nevertheless, if I add "print(str(Counter))" into Main.py "function Count" it prints increasing number with every impulse (1, 2, 3, 4, 5, ....)
Don't you know where is problem? what I do badly?
Thank you for any answer I appreciate
Jan Vaško