from microbit import *
counter, old = 0, 0
while True:
x = old % 5
y = old // 5
display.set_pixel(x, y, 0)
x = counter % 5
y = counter // 5
display.set_pixel(x, y, 9)
sleep(200)
old = counter
counter = counter + 1
if counter > 24:
counter = 0
from microbit import*defsetLED(number,value): x = number %5 y = number //5 display.set_pixel(x, y, value)counter, old =0,0whileTrue:setLED(old, 0)setLED(counter, 9)sleep(200) old = counter counter = counter +1if counter >24: counter =0
from microbit import*defsetLED(number,value): x = number %5 y = number //5 display.set_pixel(x, y, value)counter, old =24,24whileTrue:setLED(old, 0)setLED(counter, 9)sleep(200) old = counter counter = counter -1if counter <0: counter =24
電 子輪盤
以下是電子輪盤的程式碼:
from microbit import *
def setLED(number, value):
x = number % 5
y = number // 5
display.set_pixel(x, y, value)
seq = [0, 1, 2, 3, 4, 9, 14, 19, 24, 23, 22, 21, 20, 15, 10, 5]
counter, old = 0, 0
while True:
setLED(seq[old], 0)
setLED(seq[counter], 9)
sleep(100)
old = counter
counter = counter + 1
if counter >= len(seq):
counter = 0
以下是正轉加上反轉的程式範例,不同於上面程式,這次我們利用for迴圈的方式來解決這個問題:
from microbit import*defsetLED(number,value): x = number %5 y = number //5 display.set_pixel(x, y, value)seq = [0,1,2,3,4,9,14,19,24,23,22,21,20,15,10,5]old = seq[0]whileTrue:for counter in seq:setLED(old, 0)setLED(counter, 9)sleep(100) old = counterfor counter inreversed(seq):setLED(old, 0)setLED(counter, 9)sleep(100) old = counter