iOS to Arduino Hacking Tips

This is a post about some of the things we found out while hacking a guitar to teach music - more info on the project is at www.tabber.co
Technology has been on a fast track to helping our lives since the computer. We’ve seen software to help us with mundane paperwork and calculation type tasks, but now technology is reaching an entirely new level. The ease with which we’re able to control the physical world from something as common place as our smartphones is here!

Let’s talk a little bit about the Arduino and its insane capacity to control our physical world from a mobile device.

There are already a couple good resources on setting up the iPhone to communicate with an Arduino. I recommend going through this tutorial first:

http://makeprojects.com/Project/Connect-an-iPhone-iPad-or-iPod-touch-to-Arduino-with-the-Redpark-Serial-Cable/1130/1

Currently, one of the simplest ways to make your iPhone control a motor, array of LED lights, or a household appliance is to use serial communication. Serial communication is a standard protocol that allows you to transmit bytes over a cable link. The Red Park cable allows you to convert an iPhone port to a traditional serial communication port that you can feed into the Arduino. This gives you the ability to communicate messages between your iPhone/iPad and a physical device connected to an Arduino.

When communicating via the serial communication port, one of the more complicated tasks is managing the messages that go between the iPhone device and the Arduino. There are a range of communication protocols that you can define using the byte length characters that can be sent over serial communication. We are going to go over two basic types of communication: state-based execution and a data load sequence.

  1. state-based execution

A simple switch communication protocol is as easy as it sounds. Within your Arduino code, you will read in the serial data:

byte cmd;

cmd = Serial.read();

and then create a switch statement to execute different functions based on the “read-in” character (which indicates the desired state).

      switch(cmd){

        case ‘p’:

          //play function

          break;

        case ‘u’:

          //pause function

          break;

        case ‘r’:

          //resume function

          break;
  1. Load Sequence
    A more complex implementation of Serial communication may require you to actually transmit useful data between the iPhone and the Arduino. In this case, you would need to define a protocol to communicate that information. We will again use an implementation of the flag-based execution, but will add a character ‘l’ to define a mode to load data.
 case ‘l’:

          //load function

          break;

Next we need to define the data package that we will be sending to the Arduino. An example would be to define the number of bytes for necessary metadata and then assume the remainder of the package is the described data. An example of metadata would be to have the first 4 bytes of each data package contain the size of the data package by taking the sum of the first 4 bytes. This allows you to correctly parse the data in the Arduino code.
We should then define the load function that will actually read in data from the iPhone. This function will need to read in a series of data pieces from the iPhone. The first piece would be the meta data so you could immediately set a mode for reading the meta data as follows (mode = 1):

      if(mode ==-1){

        if(index < 4){

          dataLength = dataLength+Serial.read();

          index++;

        }

      }

This bit of code will read in the meta data while in “meta data” mode and give you the total data package length.
With the data package length, you can then easily read in the remainder of the data using a similar mode for “data read” (mode = 2):

      if(mode ==’2’){

        if(index < dataLength){

          data[index] = Serial.read();

        index++;

        }

Now that we’ve taken care of the Arduino side, let’s take a look at how the data is being sent from the iPhone. If you’ve successfully completed the Redpark tutorial, you should be familiar with the RscMgr package. The iPhone code will simply take an array of data and send it using the RscMgr command for serial write. Here is an example of code you can put in your button-click function:

         //define a temporary array

         UInt8 temp[dataLength+1];

    temp[0] = ‘l’;

    for(int i=1; i<dataLength;i++){

        temp[i+1] = (UInt8)someDataElement; //for example a digit = 0

    }


          //send data over serial connection

    for (int i = 0; i<songLength; i++) {

        txBuffer[i] = temp[i];

        if (i%128 == 0) {

            [rscMgr write:txBuffer Length:128];

            NSLog(@”wrote index:%i”,i);

        }

        else if (songLength == i)

            [rscMgr write:txBuffer Length:128];

    }

You’ll notice that the buffer size here is defined in lengths of 128 bytes. If your data package is more than 128 bytes, you will have to create an extra handler to combine the data after being sent over serial. This is due to limitations of the Arduino serial buffer size. Another important consideration is the memory limitations of your Arduino model. An Arduino Uno model only has 2 MB of memory available to read and store real-time data. Not ideal for a production package in many cases, but great for creating prototypes!
Here you have it, a data package communicated to the Arduino from the iPhone. Share with us what you create or intend to build in the comments!

If you modify your post, select the code, delete it, press the # icon, and paste the code back in, it will look a lot more like the code that is actually on your iPhone or Arduino.

@PaulS Thanks for the tip!

Thanks for the tip!

I have more, now that we can see the (horrid) structure of the code.

  1. Use the Tools + Auto Format tool to properly, consistently indent the code.
  2. Get rid of half the white space in the code. There really does not need to be 2 or 3 blank lines between each line of code.

Man, wish I had an iphone 4s and some siri, go ahead and eliminate my computer from the who voice control equation. Oh and the money to renew my apple developer's license, heh.

Anyone know of a voice recognition api/sdk for the 3gs?

The iPhone 4S cases add that little bit of zinc to the whole monotonous feel of the phone. They even make the phone look colorful and different from one another. These cases come cheap and so are easily affordable too. The iPhone 4S looks just the same in all hands but the new cases sure give them a new look. Get one for yourself today and define your own style statement among your peers.

My iPhone 4 case doesn't have any zinc in it at all.

My iPhone 4 case doesn't have any zinc in it at all.

Pretty sure that was supposed to be zing, and that there was some spam stripped out.

Yes, I got that. It was put there in case the person that posted it ever came back.