Hello all,
Didn't know whether to post this to this forum, or the LadyAda forums... See how we go, mmm?
I've got a project using a Waveshield, and it is to play a .WAV file on the happenstance of two events:
- Someone entering the room, triggering a Parallax PING))) sensor
and
- Said person staying in the room, triggering a Parallax Passive IR sensor.
The distance from the PING))) will probably be 2.5 metres, this will be calibrated in the final version. The PIR is set to "continuous HIGH" mode, ie, the sensor will remain HIGH while there is movement in the room.
For the purposes of tests (only because I have a small SD card) the delay time to the stopping of the .WAV file is 2 seconds, but in practice, will be about 30 seconds.
If the file does indeed play out to the end, it increments the file, and plays the next on the card, ie no random select, once the system is activated again.
I have written some code, but I can imagine I'll have someone rap me over the knuckles for the way its written. I'm no longer a coder, I'm more into the hardware, although I find Arduino relatively easy to comprehend.
I believe the hardware works, I have the whole shebang hooked up to my Linux AMD box, and there is no magic blue smoke coming out. Indeed, the debug messages come through onto the serial console for the Arduino program, but... no sound!
If anyone can help me, I would be very appreciative. Thanks for looking! Code follows:
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
AF_Wave card;
File f;
Wavefile wave;
int pingPin = 7;
int pirPin = 8;
int ledPin = 9;
int tracknum = 0;
int calibrationTime = 30;
long unsigned int lowIn;
boolean lockLow = true;
boolean takeLowTime;
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//Waveshield setup
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
//PIR calibration
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE\n");
delay(50);
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 ls() {
char name[13];
int ret;
card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);
if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);
}
}
void loop() {
long duration, cm;
int n, r;
char name[13];
card.reset_dir();
// scroll through the files in the directory
for (n=0; n<tracknum+1; n++) {
r = card.get_next_name_in_dir(name);
if (!r) {
// ran out of tracks! start over
tracknum = 0;
return;
}
}
// clean the input, trigger the sensor
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH); //reads our duration, ergo, distance. Will compare!
cm = msToCM(duration);
Serial.print(duration); //work out our distance. Lets compare!
Serial.print("\n");
Serial.print(cm);
Serial.print("cm\n");
delay(1000);
if (duration <= 1000 && digitalRead(pirPin) == HIGH) {
Serial.print("Stage 2 active\n");
digitalWrite(ledPin, HIGH);
playcomplete(name);
delay(2000);
do {
Serial.print("Confidence remains high\n");
delay (2000);
} while (pirPin == HIGH);
}
else if (duration >= 1000 && digitalRead(pirPin) == HIGH) {
digitalWrite(ledPin, HIGH);
if (wave.isplaying) {
//delay
delay(5000); //30 seconds here
digitalWrite(ledPin, LOW);
wave.stop();
card.close_file(f);
}
else if (duration <= 1000 && digitalRead(pirPin) == LOW) {
Serial.print("Stage 2 holdoff\n");
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, LOW);
if (wave.isplaying) {
wave.stop();
card.close_file(f);
delay(2000);
}
}
}
}
//duration to distance in an equation! Tada!
long msToCM(long microseconds) {
return microseconds / 29 / 2;
}
void playcomplete(char *name) {
playfile(name);
while (wave.isplaying);
card.close_file(f);
}
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}