Music Shield and Proximity Control: coding issues?

Hi,

I am using an Arduino UNO and a Seeedstudio Music Shield (http://seeedstudio.com/wiki/Music_Shield) to run a very simple project, which is giving me some problems anyway.. Basically, when someone is close enough to a proximity sensor (Analog5), a stored sound file should start playing. The file would play till the end (2.30 min), then the arduino would wait some time and start the process again so that the next person that comes in will enjoy the same treat.
You have to imagine that the sensor is in front of a mirror and the file plays only (but for all it's length) when someone approches it.. Of course this is quite hard cause if someone will remain in front of the mirror for longer time then the file will play again... but that is part of the game I guess.

Here is my code. At the moment I can get the file to play when the sensor goes under a threshold but then it won't play again a second time.. the process kind of freezes it self after a first reproduction. ..thanks a lot!!

#include <avr/io.h>
#include "config.h"
#include "filesys.h"
#include "player.h"
#include "vs10xx.h"
#include "record.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //pin2-Rx,pin3-Tx(note: pin3 is actually later used as volume down input)

const int analogPin = A5;
const int ledPin = 1;
const int threshold = 400;

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT);

InitSPI();
InitIOForVs10xx();
InitIOForKeys();
InitIOForLEDs();
InitFileSystem();
//VsSineTest();
Mp3Reset();
}

void loop() {

int proximityValue = analogRead(analogPin);
Serial.println(proximityValue);

if (proximityValue > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
Play();
}

}

If you comment out Play();, does the LED toggle as expected?

no it doesn't cause I think the pin is always in use somehow.. it's always on... but if I use pin13 and delete the Music Shield part then yes it does work.

I think the pin is always in use somehow

const int ledPin = 1;
Serial.begin(9600);

All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX)

It's best to not use pins 0 and 1 for anything else.

Yes that's make sense. That's also why the Music Shield is not using them I guess..
My problem is still there though. Anyone?

This seems to be the function I have to use, but I am not really sure how to read this reference..
http://code.google.com/p/musicshield/source/browse/branches/arduino+code/music/player.cpp

void Play()
{
playingState = PS_NEXT_SONG;

currentFile = 1;

//cyclely play
while(1)
{

//CheckPlay();
//CheckKey();
AvailableProcessorTime();

if(1 ==playStop)
{
if(OpenFile(currentFile))
{
//if open failed, then try it again
if(OpenFile(currentFile))
{
playStop = 0;
playingState = PS_NEXT_SONG;
currentFile = 1;
continue;
}
}

PlayCurrentFile();
if (playingState == PS_PREVIOUS_SONG) currentFile--;
if (playingState == PS_NEXT_SONG) currentFile++;
if (currentFile==0) currentFile = 1;
//if (playingState == PS_END_OF_SONG) playingState = PS_NORMAL;
Mp3SoftReset();
}
}
}

My problem is still there though. Anyone?

A suggestion was made to change your code. You say that you did that, but we can't see what changes you made.

All the digital Pins are in use by the the board, so I am not checking the state with an Led.. which anyhow was pointless. I haven't made any change.

All the digital Pins are in use by the the board

OK. It is possible to use pin 1 for the LED, then.

so I am not checking the state with an Led.

Yes, you are.

  if (proximityValue > threshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
    Play();
  }

I haven't made any change.

But, you need to.

Ok, big update. Using the Play() function as it was would have not allow me to just play a file, as that function never returns. I have written a new one (JPlay) and now it works much better. I can now get the patch to run as many times as I want... everytime the distance sensor goes over the threshold the file play for all it's length.

Now, would be good to write something to avoid the file to play twice if the person remains in front of the sensor. Is this clear enough? or should I try explain it differently?

first attempt which doesn't work (it plays the file when something is close to the sensor, but it will play it twice if the sensor is still covered)

#include <avr/io.h>
#include "config.h"
#include "filesys.h"
#include "player.h"
#include "vs10xx.h"
#include "record.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //pin2-Rx,pin3-Tx(note: pin3 is actually later used as volume down input)

const int analogPin = A5;
const int ledPin = 0;
const int threshold = 300;

int sensor = A5;

void setup()
{
Serial.begin(9600);
InitSPI();
InitIOForVs10xx();
InitIOForKeys();
InitIOForLEDs();
InitFileSystem();
Mp3Reset();
}

void loop() {

//Serial.println("loop");
Serial.println(analogRead(analogPin));
int pippo = 1;

while (analogRead(analogPin) > threshold) {
Serial.println("paperino");
if (pippo = 1) {
JPlay();
pippo = 0;
}

}
}

    if (pippo = 1) {

I'd like to have a nickel for every time someone posted code using = where == was supposed to be used...