運作原理

Untitled

內建LED閃爍控制(數位輸出)

microBlock積木程式

Untitled

MicroPython程式

物件.方法() 例如:汽車.前進()、汽車.左轉()、汽車.右轉() … MicroPython的LED控制使用物件的寫法:p2.on()、p2.off(),GPIO2為內建LED

from machine import Pin
from time import sleep

p2 = Pin(2, Pin.OUT)

while True:
	p2.on()
	sleep(1)
	p2.off()
	sleep(1)
	p2.value(1)
	sleep(5)
	p2.value(0)
	sleep(5)

參考資料:https://docs.micropython.org/en/latest/esp32/quickref.html#pins-and-gpio

按鈕輸入值是什麼?(數位輸入)

microBlock積木程式

Untitled

MicroPython程式

將p36(按鈕)的值顯示在終端機

from machine import Pin

while True:
  p36 = Pin(36, Pin.IN)
  print(p36.value())

以下兩個程式可以用id()查看變數的識別碼,想想為什麼會有這樣的結果,其實Python的變數賦值像是把變數當成標籤,綁在數字物件。

from machine import Pin

while True:
  p36 = Pin(36, Pin.IN)
  print(id(p36), p36.value())
from machine import Pin

while True:
  p36 = Pin(36, Pin.IN).value()
  print(id(p36), p36)

按鈕控制LED

microBlock積木程式