Class: Arduino

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

Overview

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

Direct Known Subclasses

ArduinoMega

Instance Method Summary collapse

Constructor Details

#initialize(port, baudrate = 115200) ⇒ Arduino

initialize port and baudrate



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/arduino_mega.rb', line 20

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
  @output_pins = []
  @pin_states = {}
end

Instance Method Details

#analog_read(pin) ⇒ Object

Read from an analog pin



111
112
113
114
115
# File 'lib/arduino_mega.rb', line 111

def analog_read(pin)
  send_data('4')
  send_pin(pin)
  get_data()
end

#analog_write(pin, value) ⇒ Object

Write to an analog pin



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/arduino_mega.rb', line 98

def analog_write(pin, value)
  send_data('3')
  full_hex_value = value.to_s(base=16)
  hex_value = hex_value[2..full_hex_value.length]
  if(hex_value.length==1)
    send_data('0')
  else
    send_data(hex_value[0])
  end
  send_data(hex_value[1])
end

#closeObject

close serial connection to connected board



125
126
127
128
129
130
131
132
133
# File 'lib/arduino_mega.rb', line 125

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

#get_state(pin) ⇒ Object

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



90
91
92
93
94
95
# File 'lib/arduino_mega.rb', line 90

def get_state(pin)
  if @pin_states.key?(pin.to_s)
    return @pin_states[pin.to_s]
  end
  return false
end

#is_high?(pin) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/arduino_mega.rb', line 77

def is_high?(pin)
  if get_state(pin)
    return true
  else
    return false
  end
end

#is_low?(pin) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/arduino_mega.rb', line 62

def is_low?(pin)
  if !get_state(pin)
    return true
  else
    return false
  end
end

#output(*pin_list) ⇒ Object

Set output pins. This is a must.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/arduino_mega.rb', line 41

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

#save_state(pin, state) ⇒ Object



85
86
87
# File 'lib/arduino_mega.rb', line 85

def save_state(pin, state)
  @pin_states[pin.to_s] = state
end

#set_high(pin) ⇒ Object

Set a pin state to high



71
72
73
74
75
# File 'lib/arduino_mega.rb', line 71

def set_high(pin)
  save_state(pin, true)
  send_data('1')
  send_pin(pin)
end

#set_low(pin) ⇒ Object

Set a pin state to low



56
57
58
59
60
# File 'lib/arduino_mega.rb', line 56

def set_low(pin)
  save_state(pin, false)
  send_data('0')
  send_pin(pin)
end

#to_sObject

Print information about connected board



36
37
38
# File 'lib/arduino_mega.rb', line 36

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

#turn_offObject

set all pins to low



118
119
120
121
122
# File 'lib/arduino_mega.rb', line 118

def turn_off
  @output_pins.each do |pin|
    set_low(pin)
  end
end