1- #!/usr/bin/env python
2- from Arduino import Arduino
3- import time
4-
5- def Blink (led_pin ,baud , port = "" ):
6- """
7- Blinks an LED in 1 sec intervals
8- """
9- board = Arduino (baud , port = port )
10- while True :
11- board .digitalWrite (led_pin ,"LOW" )
12- print board .digitalRead (led_pin ) #confirm LOW (0)
13- time .sleep (1 )
14- board .digitalWrite (led_pin ,"HIGH" )
15- print board .digitalRead (led_pin ) #confirm HIGH (1)
16- time .sleep (1 )
17-
18- def softBlink (led_pin ,baud , port = "" ):
19- """
20- Fades an LED off and on, using
21- Arduino's analogWrite (PWM) function
22- """
23- board = Arduino (baud , port = port )
24- i = 0
25- while True :
26- i += 1
27- k = i % 510
28- if k % 5 == 0 :
29- if k > 255 :
30- k = 510 - k
31- board .analogWrite (led_pin ,k )
32-
33- def adjustBrightness (pot_pin ,led_pin ,baud , port = "" ):
34- """
35- Adjusts brightness of an LED using a
36- potentiometer
37- """
38- board = Arduino (baud , port = port )
39- while True :
40- time .sleep (0.01 )
41- val = board .analogRead (pot_pin )/ 4
42- print val
43- board .analogWrite (led_pin ,val )
44-
45-
46- def PingSonar (pw_pin ,baud , port = "" ):
47- """
48- Gets distance measurement from Ping)))
49- ultrasonic rangefinder connected to pw_pin
50- """
51- board = Arduino (baud , port = port )
52- pingPin = pw_pin
53- while True :
54- duration = board .pulseIn (pingPin , "HIGH" )
55- inches = duration / 72. / 2.
56- cent = duration / 29. / 2.
57- print inches ,"inches"
58- time .sleep (0.1 )
59-
60- def LCD (tx ,baud ,ssbaud ,message , port = "" ):
61- """
62- Prints to two-line LCD connected to
63- pin tx
64- """
65- board = Arduino (baud , port = port )
66- board .SoftwareSerial .begin (0 ,tx ,ssbaud )
67- while True :
68- board .SoftwareSerial .write (" test " )
69-
70-
71-
72-
73- if __name__ == "__main__" :
74- Blink (13 ,9600 )
75- #LCD(5,9600,9600," test ")
76- #adjustBrightness(5,11,9600)
1+ #!/usr/bin/env python
2+ from Arduino import Arduino
3+ import time
4+
5+ def Blink (led_pin ,baud , port = "" ):
6+ """
7+ Blinks an LED in 1 sec intervals
8+ """
9+ board = Arduino (baud , port = port )
10+ while True :
11+ board .digitalWrite (led_pin ,"LOW" )
12+ print board .digitalRead (led_pin ) #confirm LOW (0)
13+ time .sleep (1 )
14+ board .digitalWrite (led_pin ,"HIGH" )
15+ print board .digitalRead (led_pin ) #confirm HIGH (1)
16+ time .sleep (1 )
17+
18+ def softBlink (led_pin ,baud , port = "" ):
19+ """
20+ Fades an LED off and on, using
21+ Arduino's analogWrite (PWM) function
22+ """
23+ board = Arduino (baud , port = port )
24+ i = 0
25+ while True :
26+ i += 1
27+ k = i % 510
28+ if k % 5 == 0 :
29+ if k > 255 :
30+ k = 510 - k
31+ board .analogWrite (led_pin ,k )
32+
33+ def adjustBrightness (pot_pin ,led_pin ,baud , port = "" ):
34+ """
35+ Adjusts brightness of an LED using a
36+ potentiometer
37+ """
38+ board = Arduino (baud , port = port )
39+ while True :
40+ time .sleep (0.01 )
41+ val = board .analogRead (pot_pin )/ 4
42+ print val
43+ board .analogWrite (led_pin ,val )
44+
45+
46+ def PingSonar (pw_pin ,baud , port = "" ):
47+ """
48+ Gets distance measurement from Ping)))
49+ ultrasonic rangefinder connected to pw_pin
50+ """
51+ board = Arduino (baud , port = port )
52+ pingPin = pw_pin
53+ while True :
54+ duration = board .pulseIn (pingPin , "HIGH" )
55+ inches = duration / 72. / 2.
56+ cent = duration / 29. / 2.
57+ print inches ,"inches"
58+ time .sleep (0.1 )
59+
60+ def LCD (tx ,baud ,ssbaud ,message , port = "" ):
61+ """
62+ Prints to two-line LCD connected to
63+ pin tx
64+ """
65+ board = Arduino (baud , port = port )
66+ board .SoftwareSerial .begin (0 ,tx ,ssbaud )
67+ while True :
68+ board .SoftwareSerial .write (" test " )
69+
70+
71+
72+
73+ if __name__ == "__main__" :
74+ Blink (13 ,9600 )
75+ #LCD(5,9600,9600," test ")
76+ #adjustBrightness(5,11,9600)
7777 #softBlink(11,9600)
0 commit comments