22from Arduino import Arduino
33import time
44
5- class ArduinoExample (object ):
6-
7- def __init__ (self , baud ,port = "" ,timeout = 2 ):
8- self .board = Arduino (baud = baud , port = port , timeout = timeout )
9-
10- def execute (self ):
11- pass
12-
13- class Blink (ArduinoExample ):
5+ def Blink (led_pin ,baud ):
146"""
15- Blinks an LED off and on, using
16- Arduino's digitalWrite function
17- """
18- def __init__ (self , led_pin , baud ,port = "" ,timeout = 2 ):
19- super (Blink , self ).__init__ (baud , port = port , timeout = timeout )
20- self .led_pin = led_pin
21-
22- def execute (self ):
23- self .board .digitalWrite (self .led_pin ,"LOW" )
24- print self .board .digitalRead (self .led_pin ) #confirm LOW (0)
7+ Blinks an LED in 1 sec intervals
8+ """
9+ board = Arduino (baud )
10+ while True :
11+ board .digitalWrite (led_pin ,"LOW" )
12+ print board .digitalRead (led_pin ) #confirm LOW (0)
2513time .sleep (1 )
26- self . board .digitalWrite (self . led_pin ,"HIGH" )
27- print self . board .digitalRead (self . led_pin ) #confirm HIGH (1)
14+ board .digitalWrite (led_pin ,"HIGH" )
15+ print board .digitalRead (led_pin ) #confirm HIGH (1)
2816time .sleep (1 )
2917
30- class SoftBlink ( ArduinoExample ):
18+ def softBlink ( led_pin , baud ):
3119"""
3220 Fades an LED off and on, using
3321 Arduino's analogWrite (PWM) function
3422 """
35- def __init__ (self , led_pin , baud ,port = "" ,timeout = 2 ):
36- super (SoftBlink , self ).__init__ (baud , port = port , timeout = timeout )
37- self .led_pin = led_pin
38- self .i = 0
39-
40- def execute (self ):
41- self .i += 1
42- k = self .i % 510
23+ board = Arduino (baud )
24+ i = 0
25+ while True :
26+ i += 1
27+ k = i % 510
4328if k % 5 == 0 :
4429if k > 255 :
4530k = 510 - k
46- self . board .analogWrite (self . led_pin ,k )
31+ board .analogWrite (led_pin ,k )
4732
48- class AdjustBrightness ( ArduinoExample ):
33+ def adjustBrightness ( pot_pin , led_pin , baud ):
4934"""
5035 Adjusts brightness of an LED using a
5136 potentiometer
5237 """
53- def __init__ (self , led_pin , pot_pin ,baud ,port = "" ,timeout = 2 ):
54- super (AdjustBrightness , self ).__init__ (baud , port = port ,
55- timeout = timeout )
56- self .led_pin = led_pin
57- self .pot_pin = pot_pin
58-
59- def execute (self ):
38+ board = Arduino (baud )
39+ while True :
6040time .sleep (0.01 )
61- val = self . board .analogRead (self . pot_pin )/ 4
41+ val = board .analogRead (pot_pin )/ 4
6242print val
63- self . board .analogWrite (self . led_pin ,self . val )
43+ board .analogWrite (led_pin ,val )
6444
65- class PingSonar (ArduinoExample ):
45+
46+ def PingSonar (pw_pin ,baud ):
6647"""
6748 Gets distance measurement from Ping)))
6849 ultrasonic rangefinder connected to pw_pin
69- """
70- def __init__ (self , pw_pin ,baud ,port = "" ,timeout = 2 ):
71- super (PingSonar , self ).__init__ (baud , port = port , timeout = timeout )
72- self .pw_pin = pw_pin
73-
74- def execute (self ):
75- duration = self .board .pulseIn (self .pw_pin , "HIGH" )
50+ """
51+ board = Arduino (baud )
52+ pingPin = pw_pin
53+ while True :
54+ duration = board .pulseIn (pingPin , "HIGH" )
7655inches = duration / 72. / 2.
7756cent = duration / 29. / 2.
7857print inches ,"inches"
7958time .sleep (0.1 )
80-
81- class LCD (ArduinoExample ):
59+
60+ def LCD (tx , baud , ssbaud , message ):
8261"""
8362 Prints to two-line LCD connected to
8463 pin tx
8564 """
86- def __init__ (self , tx ,ssbaud ,baud ,port = "" ,timeout = 2 ):
87- super (LCD , self ).__init__ (baud , port = port , timeout = timeout )
88- self .tx = tx
89- self .ssbaud = ssbaud
65+ board = Arduino (baud )
66+ board .SoftwareSerial .begin (0 ,tx ,ssbaud )
67+ while True :
68+ board .SoftwareSerial .write (" test " )
69+
9070
91- def execute (self , message ):
92- self .board .SoftwareSerial .write (message )
9371
9472
9573if __name__ == "__main__" :
96- app = Blink ( 9600 )
97- while True :
98- app . execute ( )
74+ #LCD(5, 9600,9600," test " )
75+ adjustBrightness ( 5 , 11 , 9600 )
76+ #softBlink(11,9600 )
0 commit comments