Sunday, September 30, 2007

ipod + arduino = love

Last spring, I was able to control my ipod using key presses via Processing (see that post). This fall, I wanted to move away from the laptop and use external switches/sensors and an Arduino microcontroller to facilitate the building of a multi-switch music station. The code required some tweaks to work with Arduino, but it's very similar to the previously posted code. Note that you need to set the serial baud rate to 19200; if you use 9600, nothing will happen.

//code that controls basic (play/pause, next, previous, volume up, and volume down) functions of ipod
//rosie daniel

int hits = 0;

int buttonStates[]={0,0,LOW,LOW,LOW,LOW,LOW};
int buttonPrevious[]={0,0,LOW,LOW,LOW,LOW,LOW};
int buttonRelease[] = {0xFF, 0x55, 0x03, 0x02, 0x00, 0x00,0xFB};

int commands[]={0,0,0x01,0x08,0x10,0x02,0x04};

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

void setup() {
Serial.begin(19200);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
}

void loop() {

for (int c=2; c<7; c++)
{
buttonStates[c] = digitalRead(c);


buttonStates[c] = digitalRead(c);

if (buttonStates[c] != buttonPrevious[c] ) {
delay(5); //helps avoid a 'double' press - check a second time to see if the button is still pressed after a delay
buttonStates[c] = digitalRead(c);
if (buttonStates[c] == HIGH) {
sendCommand(commands[c]);
//Serial.print(hits);
hits++;
}
buttonPrevious[c] = buttonStates[c];
}

}

}

void sendCommand(int cmd) {
int cs = checkSum(0x03, 0x02, 0x00, cmd, 0);
Serial.println(cs,HEX);



int bytes[] = {0xFF, 0x55, 0x03, 0x02, 0x00, cmd, cs};
for (int i = 0; i < 8; i++) {
Serial.print(bytes[i], BYTE);

}

for (int i = 0; i < 8; i++) {
Serial.print(buttonRelease[i],BYTE);
}

}

2 comments:

Anonymous said...

Silver Byte again.... AWESOME!.. just got an arduino a couple days ago and right now im using it to control a servo with a nintendo wii nunchuck..

really cool / love the ARDUINO!!!!

ipod + arduino truly does = love.... hey what a coincidence... itz valentines :) !!

l8r,
SilverByte

Dell-Ray said...

Hey awesome work, i finally found somebody who published how they did their checksumming, i really appreciate people who share their findings I'm new to having to use checksums and this was a HUGE help