Arduino and Feather Based Solutions

Why Use an Arduino or Feather?

We were able to create a very useful solution based on the Adafruit EZ-Key Bluetooth BLE device but you might want to add additional capability. If you use an Arduino combined with a Bluetooth BLE friend you can add additional capability to the device. And a better solution yet is to use an Adafruit Feather 32u4 Bluefruit BLE or Adafruit Feather M0 Bluefruit BLE. The Feather boards are small, inexpensive, and have the advantage of a built-in Lipo battery charger.

By using an Arduino or Feather platform, you have the option of adding other capabilities to the device. For example I have built a device that not only does BLE switch control of my iPhone, I can switch it into an additional mode where it serves as an infrared remote for my TV, cable box, Blu-ray etc. While we will not go into any details of how to create that complicated of a device in this tutorial, we do provide a multimode example sketch that illustrates the concept. Using a programmable platform gives you lots of options to use the device for more than just iOS switch control.

In our simple solution using the EZ-Key device, we were able to program 6 functions using just three switches by taking advantage of the long press feature. However if we allow for the possibility of pushing two or more buttons simultaneously, we can add even more functionality to the device. The use of more than one button simultaneously is called "chording" because it is like playing a chord on a piano by hitting more than one key at a time. By using a combination of short and long presses and short and long cords we can provide many more capabilities.

NOTE: Depending upon the physical capabilities of your user and the location and types of switches that you use, it may be impossible or impractical to use a chord system.

Required Libraries and Source Code

There are three versions of the source code for this project. All of them are available in a GitHub repository in the link below. The three versions are:

  • Simple: This is a simple version of the code that duplicates the functionality of the EZ-Key device we described earlier. You would use it if you are using a feather board to take advantage of the built-in battery charger but needed no other functionality.
  • Multimode: This version shows how you can switch out of iOS Switch Control mode and use the feather board for some other functionality.
  • Corded: This version shows how you can get more functionality by allowing for the pressing of multiple switches simultaneously.

All of the examples in this tutorial have been tested with both 32u4 and M0 feather devices. We recommend that you start with implementing the "simple" version first and then move on to the other versions if you need additional functionality.

You will need the Adafruit BLE library called "Adafruit_BluefruitLE_nRF51" available in the link below. All three sketches require the "BluefruitConfig.h" file that comes with all of the example sketches from that library. For more information on how to install this library visit this link.

Also to make the source code easier to read and understand, we have moved some of the common code from our three sketches into a separate file called "BluefruitRoutines.h".

Here are the links to the Adafruit BLE library and to our GitHub repository for this tutorial.

Here is the source code for the "BluefruitRoutines.h" file that is common to all three sample sketches. It creates an instance of the ble object and initializes the device. It renames it as "iOS Switch Control" so when you go to pair it with your iOS device it will be easily identifiable. This file also includes some debugging routines and error message routines. At the beginning of each sketch there is a "#define MY_DEBUG 1" which you can change to "0" to turn off debugging. We recommend you initially compile with debugging turned on and then once you have everything working properly, recompiling with debugging turned off. 

NOTE: Debugging requires use of the serial monitor. If you do not have the serial monitor open, the sketch may not work if you have enabled debugging. The serial monitor is not necessary if debugging has been turned off.
/* This is all the blueprint specific code for all of our
 *  iOS switch control example code. We have moved it here to make
 *  the main source code more legible.
 */

#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "BluefruitConfig.h"
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

//Debug output routines
#if (MY_DEBUG)
  #define MESSAGE(m) Serial.println(m);
  #define FATAL(m) {MESSAGE(m); while (1);}
#else
  #define MESSAGE(m) {}
  #define FATAL(m) while (1);
#endif

void initializeBluefruit (void) {
  //Initialize Bluetooth
  if ( !ble.begin(MY_DEBUG))
  {
    FATAL(F("NO BLE?"));
  }
  //Rename device
  if (! ble.sendCommandCheckOK(F( "AT+GAPDEVNAME=iOS Switch Access" )) ) {
    FATAL(F("err:rename fail"));
  }
  //Enable HID keyboard
  if(!ble.sendCommandCheckOK(F( "AT+BleHIDEn=On" ))) {
    FATAL(F("err:enable Kb"));
  }
  //Add or remove service requires a reset
  if (! ble.reset() ) {
    FATAL(F("err:SW reset"));
  }

}
This guide was first published on Feb 13, 2017. It was last updated on Nov 16, 2018. This page (Arduino and Feather Based Solutions) was last updated on Feb 09, 2017.