2014年4月3日 星期四

用Python寫Arduino sketches.

其實也可以使用 Python 程式語言搭配眾多的 Python UI 來控制 Arduino,
熟悉 Python 的人都知道它的好處, 
其中 eMail Notifier 只是一個不錯的例子 http://www.robofun.net/forum/vie ... &extra=page%3D1
其它的我就不多說了.

Python 和 Arduino 它們之間也是透過 Firmata 方式連繫的,
所以 Arduino 端必須要先 upload 「Standard Firmata」這支程式,
下列是一個能讓 pin 13 燈閃亮的範例.

相關資料:
Python http://www.python.org/
pyFirmata https://bitbucket.org/fab/pyfirmata/src/96116e877527?at=default

程式碼:

#inoBlink.py
import pyfirmata

PIN = 13 # Pin 13 is used
DELAY = 1 # A 1 seconds delay

# Adjust that the port match your system, see samples below:
# On Linux: /dev/tty.usbserial-A6008rIF, /dev/ttyACM0,
# On Windows: \\.\COM1, \\.\COM2
PORT = '\\.\COM3'

# Creates a new board
board = pyfirmata.Arduino(PORT)

# Loop for blinking the led
while True:
    board.digital[PIN].write(1) # Set the LED pin to 1 (HIGH)
    board.pass_time(DELAY)
    board.digital[PIN].write(0) # Set the LED pin to 0 (LOW)
    board.pass_time(DELAY)

沒有留言:

張貼留言