Pages: [1]   Go Down
Author Topic: Arduino + Processing + SRF02 Ultrasonic Sensor + MP3  (Read 1794 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Code:
if(inString != null && inString.length() > 0)
{
}

Then, inside the block, you need to set inString to an empty string.
Code:
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.

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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?
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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26626
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

Pages: [1]   Go Up
Jump to: