UIFlow Guide
该程序控制PWM信号和方向切换。通过按钮A的单击和双击,切换运行状态和方向标志位。运行标志run_flag
为1时,PWM信号恢复并驱动设备;方向标志dir_flag
为0或1时,分别控制引脚23的输出高低以改变方向。ADC读取值用于监测输入信号,RGB灯颜色指示运行状态。
from m5stack import *
from m5ui import *
from uiflow import *
from easyIO import *
import machine
dir_flag = None
run_flag = None
adc_val = None
def buttonA_wasDoublePress():
global dir_flag, run_flag, adc_val, adc0, PWM0
if dir_flag == 0:
dir_flag = 1
else:
dir_flag = 0
pass
btnA.wasDoublePress(buttonA_wasDoublePress)
def buttonA_wasPressed():
global dir_flag, run_flag, adc_val, adc0, PWM0
if run_flag == 0:
rgb.setColorAll(0x33ff33)
run_flag = 1
else:
rgb.setColorAll(0x000000)
run_flag = 0
pass
btnA.wasPressed(buttonA_wasPressed)
digitalWrite(21, 1)
digitalWrite(22, 0)
dir_flag = 0
run_flag = 0
adc0 = machine.ADC(33)
adc0.width(machine.ADC.WIDTH_12BIT)
adc0.atten(machine.ADC.ATTN_11DB)
adc_val = 0
rgb.setColorAll(0x000000)
PWM0 = machine.PWM(19, freq=12000, duty=0, timer=0)
PWM0.pause()
PWM0.duty(50)
while True:
adc_val = adc0.read()
if run_flag == 1:
PWM0.resume()
else:
PWM0.pause()
if dir_flag == 1:
digitalWrite(23, 1)
else:
digitalWrite(23, 0)
print(adc_val)
wait_ms(2)
stepmotor = Stepmotor('Full')
stepmotor.disbale()
stepmotor.enable()
stepmotor.get_status()
stepmotor.get_voltage()
stepmotor.move_circles(0, 0)
stepmotor.move_steps(0, 0)
stepmotor.reset()