Learn Another Language

Now that you've got a decent handle on programming Arduino C, there is a world of new open source programming languages out there ready to be taken on. While altogether different from the Arduino's C-based language, we're going to have a quick glance over a couple of these programming languages that work really well with the Arduino platform and have complementary design ideologies. These languages will open up possibilities for using the Arduino to generate sound and images, manipulate video, and make things even more interactive. In addition to introducing the languages here, we will also have a look at a group of Arduino sketches called Firmata that make using inputs and outputs on the Arduino a whole lot easier.

Firmata

Firmata is an Arduino firmware that is loaded onto the Arduino board to allow other programming languages to easily communicate with sensors, motors, and other devices. Rather than writing a custom sketch for the Arduino each time we need to communicate with a computer, like we did in our Serial-based projects in Chapter 10, we can use the standard communication protocols that have been written for us already in Firmata, along with code that is freely available for many of the more popular programming languages. Getting started with Firmata is easy enough, as the most recent versions are included with the standard Arduino distribution. There are several different versions of Firmata available, so it might take a little experimentation to find the version that works for your purposes. All we need to do is open a version of Firmata as shown in Figure 11-11; we're using the version called StandardFirmata_2_2_forUno_0_3. Upload this sketch to the Arduino interface board, as usual.

images

Figure 11-11. Opening Firmata

With Firmata loaded on the Arduino board, we need to find a matching object or test program for the language we want to use with it. We'll get to using Firmata with other languages in a moment, so just to test that everything works, we can use the Firmata Test Program, available from http://firmata.org/wiki/Main_Page (see Figure 11-12).

images

Figure 11-12. Firmata test program

With this simple program running, we can configure any of the I/O pins as Input, Output, PWM (for analog output), Servo, and Analog (for analog input). Once configured, we can toggle the state of digital outputs from Low to High, read the state of digital inputs, change the PWM value of PWM pins, move a servo to a different position, and read the values from our Analog inputs. Pretty nifty for a simple little test program, but it also shows some of the versatility in using Firmata with our computer in that it allows us to concentrate on learning a new language without getting bogged down in writing new Arduino code each time we need to do something.

While Firmata host software has been written for a range of software, including Visual Basic, .NET framework, C++/openFrameworks, C#, Perl, Python, Flash, and more, we will look at the use of Firmata on two of the more like-minded programming languages as a way to introduce these packages as candidates for where to go next.

Processing

Processing is the free and open-source programming language that in many ways started things off for the Arduino. Processing was designed by two MIT students, Casey Reas and Ben Fry, as a way to initially make programming visual design and graphics easier for the non-programmer through a text-based language used to generate images. Unlike Arduino, Processing is based off of Java, loosely related to C by lineage; however, like the Arduino, Processing gets its power and usability from carefully-written libraries of code. Processing has gained wide acclaim and has been used in commercials for Nike and Hewlett-Packard, music videos for Modest Mouse and Radiohead, and all manner of art installations everywhere. It is a really good language for visualizing data, creating immersive and interactive graphics, and generating moving images.

To get started using Processing with the Arduino, we need to download Processing and the Arduino Library for Processing from http://processing.org/download and www.arduino.cc/playground/Interfacing/Processing respectively. Figure 11-13 shows the arduino_output sketch included with this last download.

images

Figure 11-13. Simple Processing sketch

If Processing looks familiar, that's because the Processing development environment is the same one that the Arduino environment is built from. Things are a little different and the language will take some getting used to, but once you have a firm grasp of programming the Arduino, it's not overly difficult to get up to speed on Processing.

That is only the briefest introduction to working with Processing. For more information on using the Firmata, Arduino, Processing triumvirate check out the Arduino Playground site, as well as the (somewhat outdated) tutorial from Golan Levin at www.flong.com/blog/2010/installing-arduino-with-firmata-for-maxmsp-and-processing-in-osx. The creators of Processing, Casey Reas and Ben Fry, have also done outstanding jobs writing about the language with their books Getting Started with Processing (O'Reilly Media, 2010) and Processing: A Programming Handbook for Visual Designers and Artists (MIT Press, 2007). Both are great books to pick up if you are interested in learning more about Processing.

PureData

PureData, or PD, is quite the departure from the text-based programming of Arduino and Processing. PD is a graphical programming environment where lines of code and functions are replaced by reusable objects that are connected together with actual lines that link one object to another—kind of like the patches from an analog synthesizer. PD is particularly adept at audio, image, and video manipulation, and is a loosely related, free and open-source alternative to Max/MSP. If you are interested in using the Arduino to interface with sound and video generated on a computer, then PD would be a good choice. More information and downloads can be found at http://puredata.info.

Figure 11-14 shows a simple PD patch that reads sensor data from an analog sensor connected to pin A0 on an Arduino board with the SimpleAnalog version of Firmata loaded on it. It then uses this sensor data to control the amplitude and frequency of a band pass filter applied to a static noise signal.

images

Figure 11-14. Simple PureData patch

This simple patch demonstrates some of the basic capabilities of PD and gives you a taste for what it's like to program in a graphical language. For this patch to work, we need to download the Pduino object available from http://at.or.at/hans/pd/objects.html. For some help getting started with Pduino, check out the FLOSS Manual on PureData at http://en.flossmanuals.net/pure-data/ch061_starting-pduino. Included with the Pduino download are several example files, including arduino-test.pd, shown in Figure 11-15.

images

Figure 11-15. PureData Pduino object

This patch is a good place to start, as it shows many of the possibilities of interfacing an Arduino running Firmata and PureData. Here we can configure the various states for each of the I/O pins, toggle the states of outputs, change the PWM values or servo positions using a slider, and read the states and values of input pins. Again, the best place to read up on PD is at the FLOSS Manuals web site on PureData at http://en.flossmanuals.net/pure-data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset