Class: Arduino

Inherits:
Object
  • Object
show all
Defined in:
lib/arduino.rb

Overview

The main Arduino class. Allows managing connection to board and getting & setting pin states.

Instance Method Summary collapse

Constructor Details

#initialize(port, baudrate = 115200) ⇒ Arduino

initialize port and baudrate



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/arduino.rb', line 16

def initialize(port, baudrate=115200)
    puts "initialized"
    data_bits = 8
    stop_bits = 1
    parity = SerialPort::NONE

    @serial = SerialPort.new port, baudrate
    @serial.read_timeout = 2
    @serial.sync
    
    @port = port
    @outputPins = []
    @pinStates = {}
end

Instance Method Details

#analogRead(pin) ⇒ Object

Read from an analog pin



107
108
109
110
111
# File 'lib/arduino.rb', line 107

def analogRead(pin)
    sendData('4')
    sendPin(pin)
    getData()
end

#analogWrite(pin, value) ⇒ Object

Write to an analog pin



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/arduino.rb', line 94

def analogWrite(pin, value)
    sendData('3')
    fullHexValue = value.to_s(base=16)
    hexValue = hexValue[2..fullHexValue.length]
    if(hexValue.length==1)
        sendData('0')
    else
        sendData(hexValue[0])
    end
    sendData(hexValue[1])
end

#closeObject

close serial connection to connected board



121
122
123
124
125
126
127
128
129
# File 'lib/arduino.rb', line 121

def close
    # stops executing arduino code
    @serial.write '5'.chr  
    # resets the arduino board (not on windows)   
    @serial.dtr=(0) 
    # close serial connection
    @serial.close
    p "closed"
end

#getState(pin) ⇒ Object

Get state of a digital pin. Returns true if high and false if low.



86
87
88
89
90
91
# File 'lib/arduino.rb', line 86

def getState(pin)
    if @pinStates.key?(pin.to_s)
        return @pinStates[pin.to_s]
    end
    return false
end

#isHigh?(pin) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/arduino.rb', line 73

def isHigh?(pin)
  if getState(pin)
      return true
  else
      return false
  end
end

#isLow?(pin) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/arduino.rb', line 58

def isLow?(pin)
    if !getState(pin)
        return true
    else
        return false
    end
end

#output(*pinList) ⇒ Object

Set output pins. This is a must.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/arduino.rb', line 37

def output(*pinList)
    sendData(pinList.length)
    if pinList.class==Array
        @outputPins = pinList
        pinList.each do |pin|
            sendPin(pin)
        end
    else
        raise ArgumentError, "Arguments must be a list of pin numbers"
    end
    puts "return pinlist"
    return pinList
end

#saveState(pin, state) ⇒ Object



81
82
83
# File 'lib/arduino.rb', line 81

def saveState(pin, state)
    @pinStates[pin.to_s] = state
end

#setHigh(pin) ⇒ Object

Set a pin state to high



67
68
69
70
71
# File 'lib/arduino.rb', line 67

def setHigh(pin)
    saveState(pin, true)
    sendData('1')
    sendPin(pin)
end

#setLow(pin) ⇒ Object

Set a pin state to low



52
53
54
55
56
# File 'lib/arduino.rb', line 52

def setLow(pin)
    saveState(pin, false)
    sendData('0')
    sendPin(pin)
end

#to_sObject

Print information about connected board



32
33
34
# File 'lib/arduino.rb', line 32

def to_s
    "Arduino is on port #{@port} at #{@serial.baud} baudrate"
end

#turnOffObject

set all pins to low



114
115
116
117
118
# File 'lib/arduino.rb', line 114

def turnOff
    @outputPins.each do |pin|
        setLow(pin)
    end
end