Pushbutton control of iphone functions - stuck with next steps

The concept is pretty simple. I want to have 5 buttons that control six functions on the iphone. vol +/-, play/stop, skip +/-. Ultimately I want to add a few more buttons and a power supply for the iphone, but that is phase 2.

I’ve started with a leonardo and I worked together some code and approach from this http://www.tmientp.com/ project and just a simple button push (starting with just one button). The code below compiles but when I push the button only the “L” LED lights up, no TX is happening. I understand code logic, but I admit my C++ skills and arduino are very novice, so as I’ve hacked this all together I’m likely missing some big items. Beyond doing and modifying some of the examples, this is my first project. I’m connecting to the iphone through a podbreakout board.

Any suggestions for where I am going wrong?


//Debug switch
//boolean DEBUG = true;                             //Send serial output in HEX with other formatting
boolean DEBUG = false;                          //Send serial output in BYTE for iPod connection

//Variables for the buttons
int buttonPins[] = {2, 3, 4, 5, 6};          //Array containing the pins the buttons are connected to
int numberOfButtons = 5;                          //The number of buttons connected
boolean buttonStates[]={false,false,false,false,false};    //Arrays for storing button information in
int buttonPresent[]={HIGH,HIGH,HIGH,HIGH,HIGH};    //Arrays for storing buttons states in
int buttonPrevious[]={HIGH,HIGH,HIGH,HIGH,HIGH};       // "
long time;                                        //To keep track of the time for debouncing
int debounce = 200;                               //The debounce delay (from the last button push to the next)

//Define commands for iPod mode 2
// old byte switchMode2[] =             {0xFF, 0x55, 0x03, 0x00, 0x01, 0x02, 0xFA};
byte switchMode2[] =             {0xFF, 0x55, 0x03, 0x02, 0x00, 0x01, 0xFA};
byte buttonRelease =             0x00;
byte PlayPause2 =            0x01;
byte VolUp2 =                  0x02;
byte VolDown2 =                  0x04;
byte SkipForward2 =            0x08;
byte SkipBack2 =            0x10;
byte NextAlbum2 =            0x20;
byte PreviousAlbum2 =            0x40;
byte Stop2 =                    0x80;

void setup ()
  //Start the serial connection
  //Setup the buttons
  for (int i=0; i<numberOfButtons; i++)
    pinMode(buttonPins[i], INPUT);
  //Switch to mode 2 (simple ipod remote mode)
  if (DEBUG){Serial.print("Mode 2: ");}
  for (int i = 0; i < 7; i++) {
    if (DEBUG)
      Serial.print(" ");
  if (DEBUG){Serial.println();}

void loop ()
  //Loop through the amount of buttons
  for (int button=0; button<numberOfButtons; button++)
    //Update the present state of the button
    buttonPresent[button] = digitalRead(buttonPins[button]);
    //If the button state has changed and the debounce duration has passed
    //The debounce period will work for all buttons. This means it will stop different buttons being pressed
    // within the debounce period but this shouldn't be too much of an issue.
    if (buttonPresent[button] != buttonPrevious[button] && millis() - time > debounce)
      //If the button is pressed...
      if (buttonPresent[button] == LOW)
        //Update the button's state in the array
        buttonStates[button] = true;
        if (DEBUG){Serial.print("Button: ");Serial.print(button);Serial.print("  Pressed   ");}
        //Update the time the last button was pressed
        time = millis();
        //Depending on what buttons been pressed send a command
        switch (button)
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
      //If the button is not pressed...
        //Update the button's state in the array
        buttonStates[button] = false;
        if (DEBUG){Serial.print("Button: ");Serial.print(button);Serial.print("  Released  ");}
        //Send the button release command
      //Update the button's previous state in the array
      buttonPrevious[button] = buttonPresent[button];


void sendCommand(byte cmd)
  //Create a checksum for the command
  byte cs = checkSum(0x03, 0x02, 0x00, cmd, 0x00);
  //Create a bytes to be sent array
  byte bytes[] = {0xFF, 0x55, 0x03, 0x02, 0x00, cmd, cs};
  if (DEBUG){Serial.print("Command: ");Serial.print(cmd, HEX);Serial.print("  ");}
  //Send the bytes
  for (int i = 0; i < 7; i++)
    if (DEBUG)
      Serial.print(bytes[i], HEX);
      Serial.print(" ");
  if (DEBUG){Serial.println("");}

int checkSum(byte len, byte mode, byte command1, byte command2, byte parameter)
  byte checksum = 0x100 - ((len + mode + command1 + command2 + parameter) & 0xFF);
  return checksum;

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

How to use this forum

Read this before posting a programming question

I have the code singled out now.

That code is far too complex. You need to walk before you can run, and in development terms this means prove that your basic input and output mechanisms are working correctly before you try to do anything complicated with them. I suggest you write a test sketch that confirms you can detect button presses correctly, and then another one that proves you can light an LED, and then one that proves you can transmit data to whatever-it-is that you're trying to transmit to.

As far as possible, avoid using the hardware UART for inter-device communication so that you can use it for debugging.

You are right. I got carried away trying to get it all done in one step. I'll take it back to the basics where I started and work through one step at a time. I'll be back...