2020年4月30日 星期四

利用MicroPython線上模擬器實作小夜燈


模擬器提供的ADC範例程式:
# Using the ADC (Analogue to Digital Converter)
# Make sure you have the ADC checkbox marked!

import machine
import pyb

# The slider is connected to pin Y4, try adjusting it
y4 = machine.Pin('Y4')

adc = pyb.ADC(y4)

print(adc.read())

模擬器提供的LED範例程式:

# four LEDS numbered 1 to 4

import time
import pyb

for i in range(1000):
    pyb.LED((i%4) + 1).toggle()
    time.sleep_ms(100)

組合後的程式:
# Using the ADC (Analogue to Digital Converter)
# Make sure you have the ADC checkbox marked!

import machine
import pyb
import time

# The slider is connected to pin Y4, try adjusting it
y4 = machine.Pin('Y4')

led = pyb.LED(4)

while True:
  adc = pyb.ADC(y4)

  print(adc.read())

  if adc.read() > 128:
    led.on()
  else:
    led.off()
  time.sleep_ms(100)


改成PIN LED

程式如下:(要特別注意PIN宣告的地方)

import machine
import pyb
import time

# The slider is connected to pin Y4, try adjusting it
y4 = machine.Pin('Y4')

led = machine.Pin('Y12')

while True:
  adc = pyb.ADC(y4)

  print(adc.read())

  if adc.read() > 128:
    led.on()
  else:
    led.off()
  time.sleep_ms(100)

沒有留言:

張貼留言