日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

micropython按键控制流水灯_【micro:bit Micropython】The LED Display(1)控制像素点

發布時間:2023/12/13 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 micropython按键控制流水灯_【micro:bit Micropython】The LED Display(1)控制像素点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用DFrobot研發的micropython編程軟件uPyCraft,下載固件(Firmware)和下載程序都非常方便。可以在DFrobot論壇中進行下載。

uPyCraft軟件運行界面

官網中的micro:bit Micropython API介紹得非常詳細,為開發人員提供了詳細的文字說明和參照。

micro:bit Micropython API 在線文檔

接下來我們就開始編寫一個個簡單的Python程序,學習如何通過代碼,點亮一個個LED點陣中的像素點。

項目活動:點亮micro:bit LED點陣中的像素點(pixels)

基礎知識:

display.set_pixel(x, y, val)

# sets the brightness of the pixel (x,y) to val (between 0 [off] and 9 [max # brightness], inclusive).

display.set_pixel(x, y, val)方法有3個參數,用以設置像素點(x,y)的亮度(brightness),值(val)在0~9之間,0為最暗(關閉LED燈),9為最亮。

開始在uPyCraft中編程:

01 導入microbit的各種屬性、方法。

from microbit import *

02 創建image對象并初始化。

image = Image()

03 點亮坐標值為(0,0)的LED,并將亮度設為9。

from microbit import * image = Image() image.set_pixel(0,0,9) display.show(image)

每個LED像素點的亮度值從暗(熄滅)到亮(最亮),范圍為0~9。

程序運行效果:

04 使用for循環,讓第0排LED燈從左到右“遍歷”(流水燈)。

from microbit import * image = Image() for i in range(0,5): image.set_pixel(i,0,9) display.show(image) sleep(500)

程序運行效果:

05 換成第0列LED燈從上到下遍歷。

from microbit import * image = Image() for i in range(0,5): image.set_pixel(0,i,9) display.show(image) sleep(500)

程序運行效果:

06 LED燈走對角線。

from microbit import * image = Image() for i in range(0,5): image.set_pixel(i,i,9) display.show(image) sleep(500)

程序運行效果:

07 所有25個LED燈從左到右、從上到下,全部遍歷。

方法一:使用兩個for循環嵌套。

from microbit import * image = Image() for i in range(0,5): for j in range(0,5): image.set_pixel(j,i,9) display.show(image) sleep(300)

方法二:使用一個for循環+商與余數算法。

from microbit import * image = Image() for i in range(0,25): image.set_pixel(i%5,i//5,9) display.show(image) sleep(300)

注://是向下取整的除法。

08 加入無限循環(while True:)。

from microbit import * image = Image() while True: for i in range(0,25): image.set_pixel(i%5,i//5,9) display.show(image) sleep(300) #清空屏幕所有像素點,并等待1秒鐘 image = Image() display.show(image) sleep(1000)

程序運行效果:

09 改為像素點逐個消失。

from microbit import * image = Image() while True: #逐個顯示 for i in range(0,25): image.set_pixel(i%5,i//5,9) display.show(image) sleep(300) #等待1秒 sleep(1000) #逐個消失 for i in range(0,25): image.set_pixel(i%5,i//5,0) display.show(image) sleep(300) #等待1秒 sleep(1000)

程序運行效果:

附錄:

08 對應的MakeCode程序:

09 對應的MakeCode程序:

總結

以上是生活随笔為你收集整理的micropython按键控制流水灯_【micro:bit Micropython】The LED Display(1)控制像素点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。