流水灯可能是大家最经常DIY的东西,一个是因为简单容易实现,再就是效果不错。为了演示MicroPython的强大功能,展现不同的用法,下面教大家用TPYBoard开发板DIY一个小红绿灯板,一起体验python开发的魅力吧!
# main.py -- put your code here!
import pyb
led1 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
led2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
led3 = pyb.Pin("Y3",pyb.Pin.OUT_PP)
While True:
led1.value(1)
led2.value(1)
led3.value(1)
# main.py -- put your code here!
import pyb
led1 = pyb.Pin("Y1",pyb.Pin.OUT_PP)
led2 = pyb.Pin("Y2",pyb.Pin.OUT_PP)
led3 = pyb.Pin("Y3",pyb.Pin.OUT_PP)
x1 = pyb.Pin("X1",pyb.Pin.OUT_PP)
x2 = pyb.Pin("X2",pyb.Pin.OUT_PP)
x3 = pyb.Pin("X3",pyb.Pin.OUT_PP)
x4 = pyb.Pin("X4",pyb.Pin.OUT_PP)
x5 = pyb.Pin("X5",pyb.Pin.OUT_PP)
x6 = pyb.Pin("X6",pyb.Pin.OUT_PP)
x8 = pyb.Pin("X8",pyb.Pin.OUT_PP)
def six():
x1.value(1)
x2.value(1)
x3.value(1)
x5.value(1)
x6.value(1)
x8.value(1)
pyb.delay(1000)
x1.value(0)
x2.value(0)
x3.value(0)
x6.value(0)
x5.value(0)
x8.value(0)
def nine():
x1.value(1)
x2.value(1)
x3.value(1)
x4.value(1)
x5.value(1)
x8.value(1)
pyb.delay(1000)
x1.value(0)
x2.value(0)
x3.value(0)
x4.value(0)
x5.value(0)
x8.value(0)
def eight():
x1.value(1)
x2.value(1)
x3.value(1)
x4.value(1)
x5.value(1)
x6.value(1)
x8.value(1)
pyb.delay(1000)
x1.value(0)
x2.value(0)
x3.value(0)
x4.value(0)
x5.value(0)
x6.value(0)
x8.value(0)
def zero():
x2.value(1)
x3.value(1)
x4.value(1)
x5.value(1)
x6.value(1)
x8.value(1)
pyb.delay(1000)
x2.value(0)
x3.value(0)
x4.value(0)
x5.value(0)
x6.value(0)
x8.value(0)
def seven():
x3.value(1)
x4.value(1)
x8.value(1)
pyb.delay(1000)
x3.value(0)
x4.value(0)
x8.value(0)
def five():
x1.value(1)
x2.value(1)
x3.value(1)
x5.value(1)
x8.value(1)
pyb.delay(1000)
x1.value(0)
x2.value(0)
x3.value(0)
x5.value(0)
x8.value(0)
def four():
x1.value(1)
x2.value(1)
x4.value(1)
x8.value(1)
pyb.delay(1000)
x1.value(0)
x2.value(0)
x4.value(0)
x8.value(0)
def three():
x1.value(1)
x3.value(1)
x4.value(1)
x5.value(1)
x8.value(1)
pyb.delay(1000)
x1.value(0)
x4.value(0)
x3.value(0)
x5.value(0)
x8.value(0)
def two():
x1.value(1)
x3.value(1)
x4.value(1)
x5.value(1)
x6.value(1)
pyb.delay(1000)
x1.value(0)
x3.value(0)
x4.value(0)
x5.value(0)
x6.value(0)
def one():
x2.value(1)
x6.value(1)
pyb.delay(1000)
x2.value(0)
x6.value(0)
while True:
led1.value(1)
nine()
eight()
seven()
six()
five()
four()
three()
two()
one()
zero()
led1.value(0)
led2.value(1)
nine()
eight()
seven()
six()
five()
four()
three()
two()
one()
zero()
led2.value(0)
led3.value(1)
three()
two()
one()
zero()
led3.value(0)