Fumbling Around with Arduino and BLE

I’ve been messing around with the little microcontroller for a few weeks now, on and off, and as cool as buttons, switches, and knobs are, it’s easy to imagine how convenient it would be to have a wireless interface for the Arduino. With all the wearables and peripherals using BLE these days (Bluetooth Low Energy), I ordered a breakout without much of a gameplan other than to fumble my way towards connecting my iPhone to the Arduino.

I picked up the Adafruit nRF8001 Bluefruit LE and followed their super clear, n00b friendly, walkthrough of how to hook everything up to an Arduino UNO. You can see my ugly first-time solder job below. Luckily it still works, but I definitely charred the board. (I really should have bought some junk components to practice on first!)

Screen Shot 2015-11-27 at 11.53.02 PM

Once I had everything connected, I downloaded Adafruit’s free Bluefruit LE iOS app (with an Apple Watch counterpart – w00t!) to connect my iPhone to the Adafruit BLE breakout/Arduino UNO. The app has three main modes of interaction:

  • UART is like a chatbox that allows you to send strings between the iOS device and the Arduino’s Serial Monitor.
  • Pin I/O provides an interface for you to control and monitor live changes to the Arduino pins.
  • Controller exposes sensor data (ie accelerometer) and provides two touch-based interfaces.

The Controller mode was particularly interesting to me because one of the two touch-based interfaces is a color picker. With that in mind, I decided that for this first test, I would try and keep things simple and control an RGB LED with the already-provided color picker.

Using the example sketch: echoDemo, I was able to successfully advertise and connect with the iPhone, but then I hit a little speed bump.

RGB_byteReadOut.png

 

The app was written so that data submitted from the color picker is formatted with the prefix “!C”. The printout spits out each byte cast as characters so I thought: “Nice! All I need to do is parse the obscure sequence: ?k^ and interpret these characters into individual RGB values to send to the LED!” Well, it turns out that was a really flawed and needlessly convoluted approach and all I had to do was cast the raw bytes directly to integers. That took me a lot longer to realize than I’d like to admit but hey, lesson learned. Also, it turns out someone had already specifically written a sketch to parse messages sent from Bluefruit LE. (Strangely, you won’t find this linked amongst all of Adafruit’s great documentation. It’s super useful if you plan on using Bluefruit LE as your primary interface.)

So, with access to the individual RGB values, I wrote a little function to gently transitions the LED to its new colors over a variable time. You can find that code snippet at the end of this page. Thanks for making it this far!

IMG_4380.jpg
Success! = {69,255,22}

 

int currentColor[3] = {0,0,0};

void setColor(int red, int green, int blue, int tweenTime)
{
  int r,g,b;

  //get the absolute value of the difference between the desired red and the current red.
  int redDelta = abs(currentColor[1] - red);
  r = currentColor[1];
  while (r != red) {
    if (r < red) {
      //if the desired red is larger than the current, increment the current until it reaches the desired value.
      r++;
    } else if (r > red) {
      //if the desired red is less than the current, decrease the current until it reaches the desired value.
      r--; 
    }
    analogWrite(redPin, r);
    delay(tweenTime);
  } //end while

  //do the same for green
  int greenDelta = abs(currentColor[2] - green);
  g = currentColor[2];
  while (g != green) {
    if (g < green) {
      g++;
    } else if (g > green) {
      g--;
    } // end while
    analogWrite(greenPin, g);
    delay(tweenTime);
  } //end while

  //do the same for blue
  int blueDelta = abs(currentColor[3] - blue);
  b = currentColor[3];
  while (b != blue) {
    if (b < blue) {
      b++;
    } else if (b > blue) {
      b--;
    }
    analogWrite(bluePin, b);
    delay(tweenTime);
  } //end while

  //print to verify that the current colors are now the desired colors
  Serial.print("* Color Set To: ");
  Serial.print(r); Serial.print(", ");
  Serial.print(g); Serial.print(", ");
  Serial.println(b);

  //set currentColor to the new color
  currentColor[1] = r;
  currentColor[2] = g;
  currentColor[3] = b;
}
Advertisements

One thought on “Fumbling Around with Arduino and BLE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s