Build audio and proximity devices with the Touch Board and electric paint
Arduino Support
Although you can program the Touch Board as a regular Leonardo from the Arduino IDE (version 1.5.6 or newer) [6], you still have to install the drivers for the board [7]. To start, you'll need to download the bare-conductive-arduino
plugin
$ git clone https://github.com/BareConductive/bare-conductive-arduino.git
and copy the Bare_Conductive_Arduino
directory to your Arduino's hardware
folder. Start the IDE with the arduino
command and make sure it identifies your board by visiting Tools | Port (Figure 3). While you're there, mark the checkbox so your sketches go to the Touch Board when you upload. The type of board should also show up in Tools | Board. Make sure you select it before uploading sketches.
Just to make sure everything is working, blink an LED on the board by visiting File | Examples | 01.Basics and choosing the Blink sketch. Upload it and check that the orange LED on the Touch Board starts blinking with a frequency of one second. If you are on Linux, and you run into trouble, check out the "Linux Problems" box.
Linux Problems
If you are working from Linux, you might hit a hitch when trying to load your sketches onto the board. No matter what you try, you will get the following errors:
avrdude: ser_open(): can't open device "/dev/ttyACM0": Device or resource busy avrdude: ser_send(): write error: Bad file descriptor
The errors are the result of a program called ModemManager interfering with the board. As its name suggests, it is trying to identify the Touch Board as a modem, which ties it up when it is reset; this, in turn, triggers the resource busy error. Unless you use mobile broadband devices with your computer (e.g., Bluetooth-paired telephones), you won't need it any time soon, so you can use your package manager to remove ModemManager from your system (search for "modemmanager").
However, if you do use this software, with broadband mobile phones for example, you can always just disable it for now [8].
If you want to take full advantage of the Touch Board, you'll also need the MPR121 library. This library allows you to program the capacitative features of the Touch Board.
Download it with:
$ git clone https://github.com/BareConductive/mpr121.git
And copy the MPR121
directory to your Arduino libraries
folder.
You will also need the SparkfunMP3 Player Shield Arduino library [9], because it will allow you to play MP3 clips using the sound chip on the Touch Board. Get it with:
$ git clone https://github.com/madsci1016/Sparkfun-MP3-Player-\ Shield-Arduino-Library.git
Again, copy the SdFat
and SFEMP3Shield
directories to your Arduino libraries
folder.
Restart your Arduino IDE and check that the libraries have been installed by opening Sketch | Import Library. At the bottom of the list, you should see the three libraries you just installed. If you have any trouble with the libraries, check out the official Arduino tutorial [10].
Touchy-Feely
This time, I want to program one of the touch sensors to switch on and off the LED that was used previously with the Blink sketch. A very basic way of doing this would be to upload the code in Listing 1. (A more complete version of this code, by the way, can be found online [11].)
Listing 1
Light Switch
01 #include <MPR121.h> 02 #include <Wire.h> 03 04 #define switchElectrode 11 05 #define outputPin 13 06 07 void setup(){ 08 09 pinMode(outputPin, OUTPUT); 10 digitalWrite(outputPin, LOW); 11 12 Wire.begin(); 13 14 MPR121.begin(0x5C); 15 16 MPR121.setInterruptPin(4); 17 MPR121.updateTouchData(); 18 } 19 20 void loop(){ 21 if(MPR121.touchStatusChanged()){ 22 MPR121.updateTouchData(); 23 if(MPR121.isNewTouch(switchElectrode)){ 24 digitalWrite(outputPin, !digitalRead(outputPin)); 25 } 26 } 27 }
Lines 1 and 2 pull in the MPR121 library, which allows you to use the capacitative features of the Touch Board, as well as the Wire library and a standard (preinstalled) Arduino library that allows you to communicate with I2C/TWI devices. If you don't know what that means, don't worry. All you need to know is that these are standards for communication between devices. The Hover board I wrote about in the last issue [5] also used the I2C protocol.
Line 4 defines which sensor is going to act as the light switch and, on line 5, the I/O pin to which you're going to connect the LED. If you use 13, as in the example, you don't have to connect an external LED because pin 13 also controls the onboard L LED.
In line 9, you set the pin mode to OUTPUT
then push a LOW
signal to switch off the LED (line 10). Next, initialize the Wire and MPR121 subsystems so you can communicate with the touch sensors (lines 12 and 13). The 0x5C
is the MPR121 I2C address on the Bare Touch board.
To know which interrupt to choose every time a sensor is touched, you need to pass on the interrupt pin used by the Touch Board to the MPR121 subsystem. For the Touch Board, that's pin 4 (line 15). Line 16 sets the initial data update.
In the loop, you poll the MPR121 subsystem to see whether the touch status has changed (i.e., whether any of the touch-sensitive electrodes has been touched; line 20). If one has been touched, the program checks in line 22 whether it is the electrode that happens to be the one that was set in line 4 (in this case, electrode 11). If it is, line 23 writes to I/O pin 13 the opposite of its current state; that is, if its state is HIGH
, you push out LOW
, and if its state is LOW
, you push out HIGH
. Therefore, if the LED is on, it is turned off, and if the LED is off, it is turned on.
Before you upload the sketch, press the board's Reset button; otherwise, you might get a board busy error. Once uploaded, press sensor E11 to switch the LED on and off (see also the "Return to Sender" box).
Return to Sender
If you want to reinstate the original program that came with your Touch Board, visit the Bare Conductive GitHub site [12] and download the script there. You can then flash it to the Touch Board like any other sketch.
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 6
(incl. VAT)