TurnipBit扩展板 | ULN2003APG驱动板 |
P5 | IN1 |
P8 | IN2 |
P11 | IN3 |
P12 | IN4 |
+5V | 5V正极 |
GND | 负极 |
TurnipBit扩展板 | 光敏传感器 |
3.3V | VCC |
GND | GND |
P0 | AO |
# 在这里添加Python代码 from microbit import * Pin_All=[pin5,pin8,pin11,pin12] #转速(ms) 数值越大转速越慢 最小值1.8ms speed=5 STEPER_ROUND=512 #转动一圈(360度)的周期 ANGLE_PER_ROUND=STEPER_ROUND/360 #转动1度的周期 isOpen=False#表示窗帘的状态 True:打开 False:关闭 def SteperWriteData(data): count=0 for i in data: Pin_All[count].write_digital(i) count+=1 def SteperFrontTurn(): global speed SteperWriteData([1,1,0,0]) sleep(speed) SteperWriteData([0,1,1,0]) sleep(speed) SteperWriteData([0,0,1,1]) sleep(speed) SteperWriteData([1,0,0,1]) sleep(speed) def SteperBackTurn(): global speed SteperWriteData([1,1,0,0]) sleep(speed) SteperWriteData([1,0,0,1]) sleep(speed) SteperWriteData([0,0,1,1]) sleep(speed) SteperWriteData([0,1,1,0]) sleep(speed) def SteperStop(): SteperWriteData([0,0,0,0]) def SteperRun(angle): global ANGLE_PER_ROUND val=ANGLE_PER_ROUND*abs(angle) if(angle>0): for i in range(0,val): SteperFrontTurn() else: for i in range(0,val): SteperBackTurn() SteperStop() while True: light=pin0.read_analog()#读取光敏传感器传递过来的模拟量数据 #数值越大说明光照度越小 if light>400: if isOpen: isOpen=False SteperRun(-360)#光照小于设定的值,关闭窗帘 else: if isOpen==False: isOpen=True SteperRun(360)#光照大于设定的值,打开窗帘 |
更多回帖