-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathArduino.py
63 lines (52 loc) · 2 KB
/
Arduino.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#########################################
# Arduino.py
# description: service to connect and control to an Arduino
# categories: microcontroller, servo, control, motor
# more info @: http://myrobotlab.org/service/Arduino
#########################################
virtual = True # uncomment this to connect to a virtual arduino
#
# Connects a serial device on Windows this would COMx
# You will need MRLComm.ino loaded on the Arduino
port="COM3"
outputPin = 8
inputPin = 13
# start the services
arduino = Runtime.start("arduino","Arduino")
# start optional virtual arduino service, used for test
if ('virtual' in globals() and virtual):
virtualArduino = Runtime.start("virtualArduino", "VirtualArduino")
virtualArduino.connect(port)
#you have to replace COMX with your arduino serial port number
# arduino.connect("/dev/ttyUSB0") - Linux way
arduino.connect(port)
# give it a second for the serial device to get ready
sleep(1)
# update the GUI with configuration changes
arduino.broadcastState()
# set the pinMode of pin 8 to output (you can change the pin number if you want)
arduino.pinMode(outputPin, Arduino.OUTPUT)
# turn pin 8 on and off 5 times
print "start to play with pin output"
for x in range(0, 5):
print('digitalWrite pin {} high'.format(outputPin))
arduino.digitalWrite(outputPin,1)
sleep(1) # sleep a second
arduino.digitalWrite(outputPin,0)
print('digitalWrite pin {} low'.format(outputPin))
sleep(1) # sleep a second
print "stop to play with pin output"
# analog input pins - you can see input
# on the oscope
# analog pin range are 14-18 on uno, 54-70 on mega
# rate is the number of polling / sec
arduino.setBoardMega()
arduino.setAref("DEFAULT")
def publishPin(pins):
for pin in range(0, len(pins)):print(pins[pin].value)
arduino.addListener("publishPinArray","python","publishPin")
print "start to poll pin input"
arduino.enablePin(inputPin, 1)
sleep(5)
print "stop to poll pin input"
arduino.disablePin(inputPin)