Interfacing...
Arduino and Ruby
The SerialPort gem seems to do the job.
simplest example
Here's a simple example of how to read from the arduino (now efficiently) via ruby using the SerialPort gem. You may have to change the port_str param (or use an integer for that param as port number). See the readme within the ruby-serialport download.
#simplest ruby program to read from arduino serial,
#using the SerialPort gem
#(https://rubygems.org/gems/serialport)
require "serialport"
#params for serial port
port_str = "/dev/ttyUSB0" #may be different for you
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
#just read forever
while true do
while (i = sp.gets.chomp) do # see note 2
puts i
#puts i.class #String
end
end
sp.close #see note 1
- note 1: As with the File api, you can instead use SerialPort.new() or .open() with a block to have the resource automatically closed for you (more robust).
- note 2: gets add a new line after any variable that memorize. If this is not desired you can call the method chomp on the gets result and the newline is suppressed
Firmata protocol implementation on Ruby. You can embed analog/digital read/write function in Ruby.
Install Rubygem
% gem install arduino_firmata
Install Firmata
Arduino IDE -> [File] -> [Examples] -> [Firmata] -> [StandardFirmata]
LED Blink
require "rubygems"
require "arduino_firmata"
arduino = ArduinoFirmata.connect
puts "firmata version #{arduino.version}"
loop do
arduino.digital_write 13, true
sleep 0.5
arduino.digital_write 13, false
sleep 0.5
end
Digital Read
arduino = ArduinoFirmata.connect
arduino.pin_mode 2, ArduinoFirmata::INPUT
loop do
if arduino.digital_read 2
puts "pin 2 on"
else
puts "pin 2 off"
end
sleep 0.1
end
Analog I/O
arduino = ArduinoFirmata.connect
loop do
an = arduino.analog_read 0
puts an
arduino.analog_write 11, an/4
sleep 0.1
end
Servo Motor
arduino = ArduinoFirmata.connect
loop do
angle = rand 180
puts "servo angle #{angle}"
arduino.servo_write 9, angle
sleep 1
end
Sysex Send
arduino.sysex 0x01, [13, 5, 2] # command, data_array
Sysex Receive
arduino.on :sysex do |command, data|
puts "command : #{command}"
puts "data : #{data.inspect}" # data_array
end
see tutorial
What is it
Dino is a ruby gem designed to get you up and running with your Arduino in seconds. It is an open source project and actively accepts pull requests on Github.
Gem Goal
- Maintain excellent documentation through Fritzing diagrams and working examples.
- 0 start up time: plug your Arduino into your computer burn one C++ file and everything should start working.
- Strong Domain Specific Language for working with components.
- Exhaustive support for components.
- Fun
Example
A simple blinking light program
require 'dino'
board = Dino::Board.new(Dino::TxRx.new)
led = Dino::Components::Led.new(pin: 13, board: board)
[:on, :off].cycle do |switch|
led.send(switch)
sleep 0.5
end
More Info
Dino was presented at RubyConf 2012.
The talk can be found at Confreaks - Arduino the Ruby Way, and the slides on SpeakerDeck
Arduino gem is a prototyping API for Arduino in Ruby. Helps prototype Arduino programs quickly from the computer, without the need to burn programs to the board frequently. It's a "burn once, write many programs" solution.
See also
others using the SerialPort gem: