I have a waveshield that I want to activate based on proximity from my parallax 28015 ping sensor. When I run the AF_wave sketch (easier than WaveHC for me to manipulate), the waveshield works fine playing all the files in a loop. When I run the PING))) sketch (http://arduino.cc/en/Tutorial/Ping?from ... oundSensor), I get a nice stream of numbers. However, I am having a hard time combining the two sketches to make them work in unison. The ping sensor only runs once and never activates a wav file. It gives me one numeric value on the serial monitor and then just stops.
I approached it by trying to use the serial numbers as a variable for a "while{_}" statement, thinking that an "if/else" statement would be activating a new wave every few milliseconds. If you're more experienced at coding, I could use a suggestion or two I'm sure. I couldn't even manipulate the wave_hc to make this work, so if that would be better, let me know what I can do.
Here is the code, I commented out unnecessary (or so I think) lines:
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
AF_Wave card;
File f;
Wavefile wave;
const int pingPin = 7;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
// Serial.println("Wave test!");
// set up waveshield pins
for (byte i = 2; i <= 5; ++i) {
pinMode(i, OUTPUT);
}
// open memory card
// if (!card.init_card()) {
// putstring_nl("Card init failed!");
// return;
// }
// if (!card.open_partition()) {
// putstring_nl("No partition!");
// return;
// }
// if (!card.open_filesys()) {
// putstring_nl("Couldn't open filesys");
// return;
// }
// if (!card.open_rootdir()) {
// putstring_nl("Couldn't open dir");
// return;
// }
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
// cm = microsecondsToCentimeters(duration);
Serial.print(inches);
// Serial.print("in, ");
//Serial.print(cm);
//Serial.print("cm");
Serial.println();
delay(100);
///////////wave stuff for loop
//sound files need to be named acordingly and distance ranges adjusted
while (inches >= 0 && inches <= 75) {
playfile("play1.WAV");
}
while (inches >= 100 && inches <= 175) {
playfile("play2.WAV");
}
while (inches >= 200 && inches <= 275) {
playfile("play3.WAV");
}
while (inches >= 300 && inches <= 399) {
playfile("play4.WAV");
}
return;
}
////////ping functions below
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/ac ... G-v1.3.pdf
return microseconds / 74 / 2;
}
//long microsecondsToCentimeters(long microseconds)
//{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
// return microseconds / 29 / 2;
//}
////wave functions below
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
}
// close file if open
if (f) {
card.close_file(f);
}
// play specified file
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}

