Hi there, I'm new, so forgive me if I'm not quite ~socialized~ to the forum.
I'm working on a project right now getting an iPod to play or restart music based on a distance variable calculated by a Ping))) ultrasonic distance sensor ( http://www.parallax.com/tabid/768/ProductID/92/Default.aspx ) using an iPod serial port breakout kit and an Arduino Uno. I had a bit of trouble getting the serial to work, and I'm new to C++, so I could very well be doing something silly. I borrowed a bit of code too, so that might not help.
The idea is that if you get close to the device, it plays music, and if you back up, it stops and restarts the track.
This is what my original code (with interaction with the ping sensor) looked like:
// initialize
int PingPin = 7; // Serial for Ping)))
int PodPin = 0; // IPod TX, board RX
int PodPout = 1; // IPod RX, board TX
int PingBaud = 9600; // Ping))) Baud rate
int PodBaud = 19200; // IPod Baud rate
int threshOn = 20; // supposedly 20cm
int threshOff = 60; // supposedly 60cm
// Setup
void setup(){
}
int commands[]={0,0,0x01,0x08,0x10,0x02,0x04};
//nothing? nothing? play/pause forward back volup voldown
int buttonRelease[] = {0xFF, 0x55, 0x03, 0x02, 0x00, 0x00,0xFB};
void loop() {
int val = Ping();
if ( val < threshOn ) {
sendCommand(commands[2]);//play/pause
while (val < threshOff){
val = Ping();
delay (500);
}
sendCommand(commands[2]);//Play/pause
sendCommand(commands[4]);//back
}
val = Ping();
}
int checkSum(int len, int mode, int command1, int command2, int parameter) {
int checksum = 0x100 - ((len + mode + command1 + command2+ parameter) & 0xFF);
return checksum;
}
void sendCommand(int cmd) {
Serial.begin(PodBaud);
int cs = checkSum(0x03, 0x02, 0x00, cmd, 0);
Serial.println(cs,HEX);
// no clue
int bytes[] = {0xFF, 0x55, 0x03, 0x02, 0x00, cmd, cs};
for (int i = 0; i < 8; i++) {
Serial.write((byte)bytes[i]);
}
for (int i = 0; i < 8; i++) {
Serial.write((byte)buttonRelease[i]);
}
}
int Con_T_S(int pingLen){
return pingLen / 29 / 2;
}
int Ping(){
Serial.begin(PingBaud) ; // BaudRate set for Ping)))
int x = 0;
pinMode(PingPin, OUTPUT) ;
digitalWrite(PingPin, LOW) ; // init sensor to ensure clean HIGH pulse
delayMicroseconds(2) ;
digitalWrite(PingPin, HIGH) ; // make the sensor send a pulse
delayMicroseconds(5) ;
digitalWrite(PingPin, LOW) ; // Set LOW again
pinMode(PingPin, INPUT) ; // Get ready to capture the duration of the resulting pulse
// Capture how long the pin stays in HIGH state.
int Duration = pulseIn(PingPin, HIGH) ;
x = Con_T_S(Duration);
return x;
}
but that wasn't giving me the results I wanted (Ping sensor works great, for all I'm aware)
So I wrote this to test how I was interacting with the iPod:
#include <SoftwareSerial.h>
#define rxPin 0
#define txPin 1
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
// initialize
int PodPin = 0; // IPod TX, board RX
int PodPout = 1; // IPod RX, board TX
int PodBaud = 19200; // IPod Baud rate
// Setup
void setup(){
mySerial.begin(19200);
}
int commands[]={0,0,0x01,0x08,0x10,0x02,0x04};
//0,0, play/pause, forward, back, volup, voldown
int buttonRelease[] = {0xFF, 0x55, 0x03, 0x02, 0x00, 0x00,0xFB};
void loop() {
sendCommand(commands[2]);
delay(5000);//5 seconds
}
int checkSum(int len, int mode, int command1, int command2, int parameter) {
int checksum = 0x100 - ((len + mode + command1 + command2+ parameter) & 0xFF);
return checksum;
}
void sendCommand(int cmd) {
int cs = checkSum(0x03, 0x02, 0x00, cmd, 0);
mySerial.println(cs,HEX);
int bytes[] = {0xFF, 0x55, 0x03, 0x02, 0x00, cmd, cs};
for (int i = 0; i < 8; i++) {
mySerial.write((byte)bytes[i]);
}
for (int i = 0; i < 8; i++) {
mySerial.write((byte)buttonRelease[i]);
}
}
and despite my best efforts, I came up with nothing. No response from the iPod except when I plugged it into the board to let me know it's charging.
Any of you brilliant minds have some sort of magical remedy for my plight?
Thanks a ton.