问题描述
我对微型python有一个疑问,即如何在微型python中创建和调用函数或与函数有关的任何其他想法 我的代码抛出错误,提示错误 NameError:未定义名称'my_func'
import time
from machine import Pin
led = Pin(2,Pin.OUT)
btn = Pin(4,Pin.IN,Pin.PULL_UP)
while True:
if not btn.value():
my_func()
while not btn():
pass
def my_func():
led(not led())
time.sleep_ms(300)
解决方法
一般情况下,我的工作是:先导入函数,然后再导入其余流程
稍微修改您的代码以将LED对象传递给功能
import time
from machine import Pin
def my_func(myLed):
myLed.value(not myLed.value()) # invert boolean value
time.sleep_ms(300)
led = Pin(2,Pin.OUT)
btn = Pin(4,Pin.IN,Pin.PULL_UP)
while True:
if not btn.value():
my_func(led)
while not btn():
pass