On Ubuntu Jaunty (board = duemilenove atmega 328 - waveshield 1.0)
Using Arduino 16
** Hello coders and codesters **
I would like to make an Arduino work without a computer, playing sound which reacts to the output of a Sonar.
The Sonar (http://arduino.cc/en/Tutorial/Ping?from ... oundSensor) works fine
Waveshield works fine as well with the random file player code.
As I want to control which sound is playing by the sonar, I chose a different sketch to control the sounds
--> I chose to work with thisexample sketch:
http://www.ladyada.net/media/wavshield/ ... ontrol.pde )
--> the sound must play a particular in a particular distance range: when the sonar detects a presence between 25 and 75 centimeters it should play a sound
==> I have done a mashup of Waveshield code and working Sonar code.
Arduino gives me no errors and the sketch uploads without a problem. But somewhere in that sketch the link between the output of the sonar and the playing of the sound is missing...
My Sonar gives me lovely centimeters in the Serial Monitor - this is a snippet:
Wave test!
Files found:
Files found:
01.WAV
02.WAV
03.WAV
04.WAV
174cm
57cm
56cm
...
As this sketch is beyond my coding capabilities - it would be nice if someone with some C-experience might have a look..
Quite a bone to chew on! But by publishing the code here, someone can benefit from it too
- Code: Select all
//Code van de sonar
#define RLED1 14 //testled on the duino
#define RLED2 15 //testled on the duino
#define RLED3 16 //testled on the duino
#define RLED4 17 //testled on the duino
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
#define one "01.WAV"
#define two "02.WAV"
#define three "03.WAV"
#define four "04.WAV"
#define redled 9
AF_Wave card;
File f;
Wavefile wave; // only one!
int wasplaying = 0;
int rVal;
int pingPin = 7;
void setup()
{
Serial.begin(9600);
Serial.println("Wave test!");
pinMode(RLED1, OUTPUT);
pinMode(RLED2, OUTPUT);
pinMode(RLED3, OUTPUT);
pinMode(RLED4, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);
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;
}
putstring_nl("Files found:");
ls();
}
void loop()
{
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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(cm);
Serial.print("cm ");
Serial.println();
if (cm<25) {
digitalWrite(RLED1, LOW);
digitalWrite(RLED2, LOW);
digitalWrite(RLED3, LOW);
digitalWrite(RLED4, HIGH);
} else if (cm<75) {
digitalWrite(RLED1, LOW);
digitalWrite(RLED2, LOW);
digitalWrite(RLED3, HIGH);
digitalWrite(RLED4, LOW);
} else if (cm<150) {
digitalWrite(RLED1, LOW);
digitalWrite(RLED2, HIGH);
digitalWrite(RLED3, LOW);
digitalWrite(RLED4, LOW);
} else if (cm<300) {
digitalWrite(RLED1, HIGH);
digitalWrite(RLED2, LOW);
digitalWrite(RLED3, LOW);
digitalWrite(RLED4, LOW);
}
delay(100);
}
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/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
char c, *toplay;
if (Serial.available()) {
c = Serial.read();
Serial.println(c, BYTE);
if (c<25) {
toplay = one;
}
else if (c<75) {
toplay = two;
}
else if (c<150) {
toplay = three;
}
else if (c<300) {
toplay = four;
} else {
// return;
}
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
card.close_file(f);
}
playfile(toplay);
}
if (wave.isplaying && !wasplaying) {
digitalWrite(redled, HIGH);
} else if (!wave.isplaying && wasplaying) {
digitalWrite(redled, LOW);
}
wasplaying = wave.isplaying;
}
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;
}
void ls() {
char name[13];
card.reset_dir();
putstring_nl("Files found:");
while (1) {
if (!card.get_next_name_in_dir(name)) {
card.reset_dir();
return;
}
Serial.println(name);
}
}
void playfile(char *name) {
f = card.open_file(name);
if (!f) {
putstring_nl("Couldn't open file"); return;
}
if (!wave.create(f)) {
putstring_nl("Not a valid WAV"); return;
}
// ok time to play!
wave.play();
}

