EasyManua.ls Logo

Arduino Pro Mini

Arduino Pro Mini
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
communication from your applications logic. Have a look at the projects
examples to learn more about these features.
Ruby
Even dynamic languages such as Ruby give you instant access to your com-
puters serial port and to an Arduino connected to it. But before that, you
need to install the serialport gem:
maik> gem install serialport
Using it, you can connect to the Arduino in just 30 lines of code.
SerialProgramming/Ruby/analog_reader.rb
require 'rubygems'
Line 1
require 'serialport'
-
-
if ARGV.size != 1
-
puts "You have to pass the name of a serial port."
5
exit 1
-
end
-
-
port_name = ARGV[0]
-
baud_rate = 9600
10
data_bits = 8
-
stop_bits = 1
-
parity = SerialPort::NONE
-
-
arduino = SerialPort.new(
15
port_name,
-
baud_rate,
-
data_bits,
-
stop_bits,
-
parity
20
)
-
-
sleep 2
-
while true
-
arduino.write "a0\n"
25
sleep 0.01
-
line = arduino.gets.chomp
-
puts line
-
end
-
We create a new
SerialPort
object in line 15, passing it all the usual parameters.
After we sleep for two seconds, we start a loop and call
write
on the
SerialPort
object. To get the result back from the Arduino, we call
gets
, and then we print
the result to the console. Here you can see the program in action:
Appendix 3. Advanced Serial Programming 262
report erratum discuss
www.it-ebooks.info

Table of Contents

Related product manuals