$sudo pip install pySerial
$sudo pip install pyfirmata
$sudo pip install requests
upload firmata
# -*- coding: UTF-8 -*-
# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing
import Tkinter
# Initialize main windows with title and size
top = Tkinter.Tk()
top.title("Hello GUI")
top.minsize(200, 30)
# Label widget
helloLabel = Tkinter.Label(top,
text="Hello World! It is a course這是一個課程")
helloLabel.pack()
# Start and open the window
top.mainloop()
>>> import pyfirmata
>>> pin = 13
>>> port = '/dev/cu.usbmodem1411'
>>> board = pyfirmata.Arduino(port)
>>> board.digital[pin].write(1)
>>> board.digital[pin].read()
1
>>> board.digital[pin].write(0)
>>> board.digital[pin].read()
0
1LED_Blink.py
#!/usr/bin/python
import pyfirmata
from time import sleep
pin = 13
port = '/dev/cu.usbmodem1411'
board = pyfirmata.Arduino(port)
while True:
board.digital[pin].write(1)
sleep(1)
board.digital[pin].write(0)
sleep(1)
board.exit()
Maxde-MacBook-Pro:downloads max$ python 1LED_Blink.py
跳出 Crtl+C
^CTraceback (most recent call last):
File "1LED_Blink.py", line 11, in <module>
sleep(1)
KeyboardInterrupt
Maxde-MacBook-Pro:downloads max$
2console.py
#!/usr/bin/python
import pyfirmata
from time import sleep
pin = 13
port = '/dev/cu.usbmodem1411'
board = pyfirmata.Arduino(port)
while True:
board.digital[pin].write(1)
print(board.digital[pin].read())
sleep(1)
board.digital[pin].write(0)
print(board.digital[pin].read()) //Print out LED status
sleep(1)
board.exit()
^CTraceback (most recent call last):
File "2console.py", line 14, in <module>
sleep(1)
KeyboardInterrupt
4helloGUI.py 圖形化介面
#!/usr/bin/python# -*- coding: UTF-8 -*-
# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing
import Tkinter
# Initialize main windows with title and size
top = Tkinter.Tk()
top.title("Hello GUI")
top.minsize(200, 30)
# Label widget
helloLabel = Tkinter.Label(top,
text="Hello World! It is a course這是一個課程")
helloLabel.pack()
# Start and open the window
top.mainloop()
=====7csvWriter.py=====
#!/usr/bin/python
# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing
import csv
data = [[1, 2, 3],['a','b','c'],['Python','for','Arduino']]
with open('PythonforArduino.csv', 'wb') as f:
w = csv.writer(f)
for row in data:
w.writerow(row)
=====7csvReader.py=====
沒有留言:
張貼留言