Arduino + Processing + SRF02 Ultrasonic Sensor + MP3

Hello there.
I'm badly in need of help with my code to a schoolproject.

We have a SRF02 Ultrasonic sensor to help us and play three different mp3files whenever it receives a certain value.
We will make use of processing and arduino platform engines.

Does anyone have a sample code we can go after when I have very little experience with coding?

What code do you currently have, and what does it not do that you want it to?

Hello. We have this example-code but our first problem with it is that it starts to play a sound right away and does not stop until this error messages comes up: "Invalid memory access of location". the mp3 never stops playing

our code:
import ddf.minim.;
import ddf.minim.signals.
;
import ddf.minim.analysis.;
import ddf.minim.effects.
;

import cc.arduino.*;

Minim minim;
AudioPlayer song;
// Example by Tom Igoe

import processing.serial.*;

Serial myPort; // The serial port
PFont myFont; // The display font
String inString; // Input string from serial port
float i;
int lf = 10; // ASCII linefeed

void setup() {
size(400, 200);
minim = new Minim(this);
song = minim.loadFile("song.mp3");

// Make your own font. It's fun!
myFont = loadFont("AgencyFB-Reg-48.vlw");
textFont(myFont, 18);

// List all the available serial ports:
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.buffer(3);

}

void draw() {
// Twiddle your thumbs
background(255);

//text("Received: " + inString, 10, 50);
// println(inString);
if (inString!= null)
i = Float.valueOf(inString).floatValue();
if (i>20){
song.play();

}

delay(200);
}

void serialEvent(Serial myPort) {
inString = myPort.readString();
print(inString);
}
void stop()
{
song.close();
minim.stop();

super.stop();
}

Thanks in advance

if (inString!= null)
i = Float.valueOf(inString).floatValue();
if (i>20){
song.play();

}

So, once inString gets to be non-null, it is forever non-null. How is that a useful test?

What is sending data to the Processing application? Specifically, why is i a float?

I am totally new at this and dont really have every part of the code fully understood.
I understand that inString needs a new value to stop the song from playing but i have no idea what to write. I have been searching for rewind() in the minim-library but cant really get it working.

the data for inString comes from Arduino. the float receives its value from a range sensor.

let me know if im unclear about anything else.

the data for inString comes from Arduino. the float receives its value from a range sensor.

I don't think I've ever come across an ultrasonic ranger that returns a float value.

I understand that inString needs a new value to stop the song from playing but i have no idea what to write. I have been searching for rewind() in the minim-library but cant really get it working.

The variable inString starts out null. It is assigned a value when serial data arrives. Once assigned a value, it can never be null again.

So, your if test should be

if(inString != null && inString.length() > 0)
{
}

Then, inside the block, you need to set inString to an empty string.

inString = "";

AFTER getting the value from it.

An else block to go with the if(i > 20) statement would be useful.

In the else block, stop the song from playing.

let me know if im unclear about anything else.

You seem to be unclear on the need to post the Arduino code. Let's clear that up right away. You need to post the Arduino code, too.

OK great. Thanks for good reply! Think I understand how you mean and what code i need to put in.

Sorry about the loss of Arduino-code. I only a slightly modified standard example:

/*
Sharp GP2D12 IR ranger reader
Language: Wiring/Arduino

Reads the value from a Sharp GP2D12 IR ranger and sends
it out serially.

Based on an example from Tom Igoe's Making Things Talk.

int sensorPin = 0; // Analog input pin
int sensorValue = 0; // value read from the sensor

void setup() {

randomSeed(42);

Serial.begin(9600); // for debugging
}

void loop() {

sensorValue = analogRead(sensorPin); // read the sensor value
//Serial.println(sensorValue);
// the sensor actually gives results that aren't linear.
// this formula converts the results to a linear range.

int range = (6787 / (sensorValue - 3)) - 4;
Serial.println(range, DEC); // print the sensor value
delay(1000);

}

Many thanks again for your help and fast replies!

int range = (6787 / (sensorValue - 3)) - 4;
Serial.println(range, DEC); // print the sensor value

And, yet, you are reading it as a float in Processing. Why?

PaulS:
int range = (6787 / (sensorValue - 3)) - 4;
Serial.println(range, DEC); // print the sensor value

And, yet, you are reading it as a float in Processing. Why?

PaulS:
int range = (6787 / (sensorValue - 3)) - 4;
Serial.println(range, DEC); // print the sensor value

And, yet, you are reading it as a float in Processing. Why?

As I said im a newbie and I have only copied example-code and put them together. Sure I can change the float to an int but i doubt it will make any big difference.
Thanks!

Break your problem down.

You need to receive numbers into your Processing, s write a very simple looping app on the Arduino that outputs an incrementing count, at a rate of one complete number every two seconds.
Verify that this works using the Serial Monitor.
When you've done that, use it to debug your Processing.

When you've debugged your Processing, and are reliably accepting and processing numbers, go back to your Arduino code, and concentrate on getting the rangefinder working, again using the Serial Monitor.

Build on proven software and simple steps - chucking it all together and expecting it to work is a bit ambitious.

Okey, with help from you people and some friends in my class we've now managed to get one track playing.
Now we want to add three tracks to play randomly when the sensor activates.

I made a new topic here http://arduino.cc/forum/index.php/topic,96456.0.html
theres our new code.

Thanks alot for the help so far !