Here's my code to control an iPod through the arduino serial pins:
Sheldon
/* Control iPod/iPhones from Arduino
Sheldon Stokes
Jan 3, 2009
Standing on the shoulders of ipodLinux.org
http://ipodlinux.org/wiki/Apple_Accessory_Protocol
This send comands to the iPod as though it were a remote.
These are the simple 2 byte commands that should work on all
Apple iPods and iPhones starting with the 3rd Generation iPod
*********** Commands (array index, command value, command description) **************
0 0x00 Button Release
1 0x01 Play/Pause
2 0x02 Vol+
3 0x04 Vol-
4 0x08 Skip >
5 0x10 Skip <
6 0x20 Next Album
7 0x40 Prev Album
8 0x80 Stop
*/
int commandBytes[]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int checkSum;
int playPin = 2;
int stopPin = 3;
int fwdPin = 4;
int backPin = 5;
int playVal, stopVal, fwdVal, backVal;
void setup()
{
Serial.begin(19200);
pinMode(playPin, INPUT);
pinMode(stopPin, INPUT);
pinMode(fwdPin, INPUT);
pinMode(backPin, INPUT);
}
void loop()
{
playVal = digitalRead(playPin); // read play button
stopVal = digitalRead(stopPin); // read stop button
fwdVal = digitalRead(fwdPin); // read fwd button
backVal = digitalRead(backPin); // read back button
if (playVal == LOW)
{
sendRequest(commandBytes[1]); // send play command
sendRequest(commandBytes[0]); // send button release
}
else if (stopVal == LOW)
{
sendRequest(commandBytes[8]); // send stop command
sendRequest(commandBytes[0]); // send button release
}
else if (fwdVal == LOW)
{
sendRequest(commandBytes[4]); // send stop command
sendRequest(commandBytes[0]); // send button release
}
else if (backVal == LOW)
{
sendRequest(commandBytes[5]); // send stop command
sendRequest(commandBytes[0]); // send button release
}
delay(100);
}
void sendRequest(int val) {
checkSum = 0x100 - ((0x03 + 0x02 + val + 0) & 0xFF);
int request[] = {0xFF, 0x55, 0x03, 0x02, 0x00, val, checkSum};
for (int i = 0; i < 8; i++)
{
Serial.print(request[i], BYTE);
}
Serial.println();
}