Salted Fish ZTMR Example-Onboard LED
Most of the development board routines start with flashing lights (controlling LED flashing). Because the procedure for flashing lights is simple and intuitive, not only can beginners quickly understand the basic methods, but they can also see the effects intuitively.
usage | Explanation |
---|---|
pyb.LED (id) | Define an LED object, id is the LED serial number, 1-4 |
led.on() | Turn on the LED |
led.off() | LED off |
led.toggle() | Flip the LED. If the original state is on, it will be turned off; the original is off, and now it will be turned on. |
led.intensity([value]) | LED brightness, value is the brightness value, 0-255, 0 is off, 255 is the brightest, only LED3 and LED4 support |
Light up the LED
import pyb
pyb.LED(4).on()
Adjust brightness
import pyb
pyb.LED(4).intensity(10)
Callout | pyb instance name | colour |
---|---|---|
D4 | LED(1) | red |
D5 | LED(2) | green |
D6 | LED(3) | yellow |
D7 | LED(4) | blue |
Marquee
import pyb
leds = [pyb.LED(i) for i in range(1,5)] #定义LED
n = 0
while True:
n = (n + 1) % 4
leds[n].toggle()
pyb.delay(50)
Round-trip marquee
import pyb
n = 1
x = 1 #定义变量
while True:
pyb.LED(n).toggle()
pyb.delay(500) #延时
pyb.LED(n).toggle()
n=n+x
if(n>3)or(n<2):
x=-x #改变方向
SOS call for help light
Set the LED according to the Morse password. In the following table, S is indicated by three rapid red lights, and O is indicated by three slow green lights.
Morse code (also translated as Morse code, Morsecode) is a signal code that is always on and off, expressing different English letters, numbers and punctuation marks through different arrangement orders. It was invented in 1837, and the inventors are controversial, it is American Samuel Morse or Alfie de Ville. Morse code is an early form of digital communication, but it is different from modern binary codes that only use zero and one or two states. Its code includes five types: dots, strokes, pauses between dots and strokes, each Short pauses between characters, medium pauses between each word, and long pauses between sentences.
# main.py -- put your code here!
import pyb
#SOS呼救灯总共要亮9次
for i in range(1,10): #把1,9依次赋值给i
if i<=3: #前3次短促红灯
pyb.LED(1).on()
pyb.delay(500)
pyb.LED(1).off()
pyb.delay(500)
elif 3<i<=6: #4~6次常亮绿灯
pyb.LED(2).on()
pyb.delay(1500)
pyb.LED(2).off()
pyb.delay(1500)
else: #剩下的次数亮红灯
pyb.LED(1).on()
pyb.delay(500)
pyb.LED(1).off()
pyb.delay(500)