File tree 3 files changed +39
-18
lines changed
3 files changed +39
-18
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
4
4
Gem ::Specification . new do |s |
5
5
s . name = "arduino"
6
- s . version = "0.3.2 "
6
+ s . version = "0.3.3 "
7
7
s . date = %q{2011-01-01}
8
8
s . platform = Gem ::Platform ::RUBY
9
9
s . authors = [ "Akash Manohar" ]
Original file line number Diff line number Diff line change 1
1
require "./lib/arduino"
2
2
3
3
#specify the port as an argument
4
- myBoard = Arduino . new ( '/dev/ttyUSB0' )
4
+ board = Arduino . new ( '/dev/ttyUSB0' )
5
5
6
6
#declare output pins
7
- myBoard . output ( 13 )
7
+ board . output ( 13 )
8
8
9
9
#perform operations
10
10
10 . times do
11
- myBoard . setHigh ( 13 )
11
+ board . setHigh ( 13 )
12
+ puts "High" if board . getState ( 13 )
12
13
sleep ( 1 )
13
- myBoard . setLow ( 13 )
14
+ board . setLow ( 13 )
15
+ puts "Low" if !board . getState ( 13 )
14
16
sleep ( 1 )
15
17
end
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ def initialize(port, baudrate=115200)
18
18
@serial . sync
19
19
@port = port
20
20
@outputPins = [ ]
21
+ @pinStates = { }
21
22
end
22
23
23
24
# Print information about connected board
@@ -37,27 +38,51 @@ def output(*pinList)
37
38
else
38
39
raise ArgumentError , "Arguments must be a list of pin numbers"
39
40
end
40
- end
41
-
42
- # Get state of a digital pin. Returns true if high and false if low.
43
- def getState ( pin )
44
- sendData ( '2' )
45
- sendPin ( pin )
46
- return formatPinState ( getData ( ) )
41
+ return pinList
47
42
end
48
43
49
44
# Set a pin state to low
50
45
def setLow ( pin )
46
+ saveState ( pin , false )
51
47
sendData ( '0' )
52
48
sendPin ( pin )
53
49
end
54
50
51
+ def isLow? ( pin )
52
+ if !getState ( pin )
53
+ return true
54
+ else
55
+ return false
56
+ end
57
+ end
58
+
55
59
# Set a pin state to high
56
60
def setHigh ( pin )
61
+ saveState ( pin , true )
57
62
sendData ( '1' )
58
63
sendPin ( pin )
59
64
end
60
65
66
+ def isHigh? ( pin )
67
+ if getState ( pin )
68
+ return true
69
+ else
70
+ return false
71
+ end
72
+ end
73
+
74
+ def saveState ( pin , state )
75
+ @pinStates [ pin . to_s ] = state
76
+ end
77
+
78
+ # Get state of a digital pin. Returns true if high and false if low.
79
+ def getState ( pin )
80
+ if @pinStates . key? ( pin . to_s )
81
+ return @pinStates [ pin . to_s ]
82
+ end
83
+ return false
84
+ end
85
+
61
86
# Write to an analog pin
62
87
def analogWrite ( pin , value )
63
88
sendData ( '3' )
@@ -109,10 +134,4 @@ def getData
109
134
cleanData = @serial . readlines ( )
110
135
cleanData = cleanData . join ( "" ) . gsub ( "\n " , "" ) . gsub ( "\r " , "" )
111
136
end
112
-
113
- def formatPinState ( pinValue )
114
- return true if pinValue =="1"
115
- return false #if pinValue=="0"
116
- end
117
-
118
137
end
You can’t perform that action at this time.
0 commit comments