Let’s program bits & atoms with Ruby
This is my second post from Flatiron School and this time I decided to combine two things I enjoy the most: writing software and sketching with hardware.
The easiest way to get started with hardware and build your first prototype is to get an Arduino board.
What is Arduino?
Arduino is currently one of the most popular open hardware boards used by the maker community to build prototypes. It was created in 2005 as a project for students at the Interaction Design Institute Ivrea in Italy. At that time program students used a “BASIC Stamp” at a cost of $100, considered expensive for students. The name “Arduino” comes from a bar in Ivrea, where some of the founders of the project used to meet. The bar, in turn, has been named after Arduin of Ivrea, who was king of Italy from 1002 to 1014.
Applications built with Arduino
Slide dress :http://www.ecouterre.com/made-from-recycled-film-slides-this-led-dress-will-light-up-your-night/
I think the Arduin king would be amazed to see how his legacy has carried on as today the homonymous board is being used in the most eclectic ways.
http://www.dyvikdesign.com/site/portfolio-jens/ref.htmlFrom wearables to citizen science projects with some quirky prototypes along the way, themaker community flourished due to this open platform which lowered the entry level to hardware. However as the projects are evolving and the community of makers, fixers, sketchers is growing we start to wonder how could we embed in a more seamless way the bits and the atoms .
From a diode ruby to a web gem
If we want to use Ruby for interacting with Arduino, let’s learn first how the communication works from a software side for this board.
All Arduino boards have at least one serial port (also known as UART) that can be used for communication between the board and a computer. It doesn’t matter what programming language is on the other device, as long as it can interface via serial. Alternatively, we can also use wired LAN and WiFi interfaces by using shields.
The first time you connect the board to your computer you can see what port it using by typing the following commands in your terminal:
$ sudo dmesg | grep -i usb
or
$ lsusb /dev/cu/USB*
(you will need to install the lsusb package which can be useful whenever you want to monitor your usb connected devices from the terminal)
Since the Arduino can communicate via its serial port, the most basic approach to controlling the Arduino would be using the serial Ruby library.
Serial Port Gem
Serial Port gem is an implementation of RS232 serial ports with several low level functionalities to control signals on the line.
Code example: Read arduino serial with Ruby
Looks familiar? This how the code would actually look if you were to use the Arduino IDE and their simplified version of C (more on that here).
Yes its seams a bit more verbose yet writing this program in Ruby allows us to customize directly the connection parameters such as BAUD rate(number of times a signal in a communications channel changes state) which can be very useful when trying to read certain type of sensors which require more “listening” time.
And it gets better as we start working at an object level.
Dino Gem
This gem uses a generic library on the Arduino, which dynamically responds to the requests from the computer over the serial connection.
This also provides much more abstraction to deal with the components involved. The Dino gem itself has a dependency on the serial port gem I mentioned above. It basically uses the same gem for serial communication while providing the programmer with useful abstractions.
After installing the dino gem, run $ dino generate-sketch serial from your terminal. This will generate a .ino sketch. Upload this sketch to Arduino using the Arduino IDE, and then from run the below code from the computer connected to the Arduino.
Example code: led blink with dino and ruby
The advantage of using Ruby
While using ruby for Arduino it is much easier to connect any api to a physical output or a sensor for a web connected monitoring tool. For example we could require a twitter stream and connect it to a couple of leds in the same program.
Example code: streaming tweets with arduino
And if you ever wondered how you could solve the Tic-Tac-Toe challenge with Arduino I challenge you to refactor this code :)
to be continued…
Ressources:
Stefania, gracias! por compartir!!!
Nice intodution ! I <3 ruby :)
Interesting.. :)