2 PIR sensors to turn WaveShield ON and OFF

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
3LFM4N
 
Posts: 13
Joined: Sat Mar 09, 2013 12:31 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by 3LFM4N »

Almost there.. I set up my two LDRs like shown: http://learning.codasign.com/images/a/a ... no_ldr.png

I used this code which uses an LDR to play a file:

Code: Select all

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

int LDR = 0; // LDR Pin
int val = 0;       // variable to store the value coming from the sensor



// Function definitions (we define them here, but the code is below)
void lsR(FatReader &d);
void play(FatReader &dir);

void setup() {
  Serial.println(analogRead(0));   // print out value of light sensor on analog pin 0
  val = analogRead(LDR);       // read the value from the sensor
  if ( val > 500 ) play(root);
  
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  pinMode(LDR, INPUT);       // declare the LDR as an INPUT
  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());  
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
  
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
  

  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Files found:");
  dirLevel = 0;
  // Print out all of the files in all the directories.
  lsR(root);
}

//////////////////////////////////// LOOP
void loop() { 
     root.rewind();
//  play(root);
  val = analogRead(LDR);       // read the value from the sensor
if ( val > 500 ) play(root);
}

/////////////////////////////////// HELPERS

// this handy function will return the number of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end; 
  extern int  *__brkval; 
  int free_memory; 
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end); 
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval); 
  }
  return free_memory; 
} 

/*
 * print error message and halt if SD I/O error, great for debugging!
 */
void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
/*
 * print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
 */
void printName(dir_t &dir)
{
  for (uint8_t i = 0; i < 11; i++) {     // 8.3 format has 8+3 = 11 letters in it
    if (dir.name[i] == ' ')
        continue;         // dont print any spaces in the name
    if (i == 8) 
        Serial.print('.');           // after the 8th letter, place a dot
    Serial.print(dir.name[i]);      // print the n'th digit
  }
  if (DIR_IS_SUBDIR(dir)) 
    Serial.print('/');       // directories get a / at the end
}
/*
 * list recursively - possible stack overflow if subdirectories too nested
 */
void lsR(FatReader &d)
{
  int8_t r;                     // indicates the level of recursion
  
  while ((r = d.readDir(dirBuf)) > 0) {     // read the next file in the directory 
    // skip subdirs . and ..
    if (dirBuf.name[0] == '.') 
      continue;
    
    for (uint8_t i = 0; i < dirLevel; i++) 
      Serial.print(' ');        // this is for prettyprinting, put spaces in front
    printName(dirBuf);          // print the name of the file we just found
    Serial.println();           // and a new line
    
    if (DIR_IS_SUBDIR(dirBuf)) {   // we will recurse on any direcory
      FatReader s;                 // make a new directory object to hold information
      dirLevel += 2;               // indent 2 spaces for future prints
      if (s.open(vol, dirBuf)) 
        lsR(s);                    // list all the files in this directory now!
      dirLevel -=2;                // remove the extra indentation
    }
  }
  sdErrorCheck();                  // are we doign OK?
}
/*
 * play recursively - possible stack overflow if subdirectories too nested
 */
void play(FatReader &dir)
{
  FatReader file;
  while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
    // skip . and .. directories
    if (dirBuf.name[0] == '.') 
      continue;
    
    Serial.println();            // clear out a new line
    
    for (uint8_t i = 0; i < dirLevel; i++) 
       Serial.print(' ');       // this is for prettyprinting, put spaces in front

    if (!file.open(vol, dirBuf)) {       // open the file in the directory
      Serial.println("file.open failed");  // something went wrong :(
      while(1);                            // halt
    }
    
    if (file.isDir()) {                    // check if we opened a new directory
      putstring("Subdir: ");
      printName(dirBuf);
      dirLevel += 2;                       // add more spaces
      // play files in subdirectory
      play(file);                         // recursive!
      dirLevel -= 2;    
    }
    else {
      // Aha! we found a file that isnt a directory
      putstring("Playing "); printName(dirBuf);       // print it out
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
        putstring(" Not a valid WAV");     // ok skip it
      } else {
        Serial.println();                  // Hooray it IS a WAV proper!
        wave.play();                       // make some noise!
       
        while (wave.isplaying) {           // playing occurs in interrupts, so we print dots in realtime
          putstring(".");
          delay(100);
        }
        sdErrorCheck();                    // everything OK?
//        if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
  }
}
However I can't seem to adapt the code successfully to get the 2nd LDR to stop the playback of the file.

I added these bottom two lines at the top:

int LDR = 0; // LDR Pin
int val = 0; // variable to store the value coming from the sensor
int LDR2 = 1; // LDR Pin
int val2 = 1; // variable to store the value coming from the sensor

As well as these few lines:
Serial.println(analogRead(1)); // print out value of light sensor on analog pin 0
val = analogRead(LDR2); // read the value from the sensor
if ( val > 100 ) wave.stop();

To this section:

Code: Select all

void setup() {
  Serial.println(analogRead(0));   // print out value of light sensor on analog pin 0
  val = analogRead(LDR);       // read the value from the sensor
  if ( val > 100 ) play(root);
  Serial.println(analogRead(1));   // print out value of light sensor on analog pin 0
  val = analogRead(LDR2);       // read the value from the sensor
  if ( val > 100 ) wave.stop();
and changed the code from this:

Code: Select all

void loop() { 
     root.rewind();
//  play(root);
  val = analogRead(LDR);       // read the value from the sensor
if ( val > 500 ) play(root);
}
To this:

Code: Select all

void loop() { 
     root.rewind();
//  play(root);
  val = analogRead(LDR);       // read the value from the sensor
if ( val < 100 ) play(root);
  val2 = analogRead(LDR2);
if ( val2 < 100) wave.stop(); // stop it;
}
The arduino programme verified it so whatever I've fiddled around isn't causing errors, however when I put my hand over the 2nd LDR I just can't get it to stop the file playing.

Is there something obvious I am missing out?

I feel like I'm soo close lol

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by adafruit_support_rick »

Hmmm... Not sure why it doesn't stop on LDR2.

First - are you sure that LDR2 is working? Just a simple sketch with Serial.print and no wav files should tell you that:

Code: Select all

void loop()
{
  Serial.println(analogRead(LDR));   // print out value of light sensor 1
  delay(500);
  Serial.println(analogRead(LDR2));   // print out value of light sensor 2
  delay(500);
}
Second - I think you want to test for LDR2 in the play function. In this section:

Code: Select all

    else {
      // Aha! we found a file that isnt a directory
      putstring("Playing "); printName(dirBuf);       // print it out
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
        putstring(" Not a valid WAV");     // ok skip it
      } else {
        Serial.println();                  // Hooray it IS a WAV proper!
        wave.play();                       // make some noise!
       
        while (wave.isplaying) {           // playing occurs in interrupts, so we print dots in realtime
          putstring(".");
          delay(100);
        }
        sdErrorCheck();                    // everything OK?
//        if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
I think you want to replace the dot-printing inside of the while (wave.isplaying) loop with the LDR2 check :

Code: Select all

        while (wave.isplaying) {  //periodically check LDR2 while wave is playing
          val = analogRead(LDR2);       // read the value from the sensor
          if ( val > 100 ) wave.stop();  //stop the wave! I want to get off!
          delay(100);
        }

3LFM4N
 
Posts: 13
Joined: Sat Mar 09, 2013 12:31 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by 3LFM4N »

Thanks driverblock, I'll do that.

However my waveshield stopped working completely when I uploaded the same code with an added the delay line here:

Code: Select all

void loop() { 
     root.rewind();
//  play(root);
  val = analogRead(LDR);       // read the value from the sensor
if ( val < 500 ) play(root);
delay (10);  
  val2 = analogRead(LDR2);
if ( val2 < 500) wave.stop();
}
I think it is the SD card as my mac has the error message 'The disk you inserted was not readable by this computer' [initialize..] [ignore] [eject], and it won't let me do anything to it.

I'm going to purchase a new SD card and try again, although I don't see how it would randomly stop working after all this time. fustrating

3LFM4N
 
Posts: 13
Joined: Sat Mar 09, 2013 12:31 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by 3LFM4N »

Okay so I tested both the LDRs with just my arduino and both give me readings so the 2nd LDR is working alright.

As I said I need to purchase a new SD card, fingers crossed that is the only problem with my waveshield.

However when you say 'test for LDR2 in the play function' I don't understand what I need to do here, nor do I understand what you mean by the dot-printing.

Sorry I'm a noob

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by adafruit_support_rick »

In the sketch you posted, there's a little loop inside the play function that prints out dots while the wav is playing. It looks like this:

Code: Select all

        while (wave.isplaying) {           // playing occurs in interrupts, so we print dots in realtime
          putstring(".");   //print a dot
          delay(100);
        }
I'm just saying to replace it with a loop that checks LDR2 instead:

Code: Select all

        while (wave.isplaying) {  //periodically check LDR2 while wave is playing
          val = analogRead(LDR2);       // read the value from the sensor
          if ( val > 100 ) wave.stop();  //stop the wave! I want to get off!
          delay(100);
        }

3LFM4N
 
Posts: 13
Joined: Sat Mar 09, 2013 12:31 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by 3LFM4N »

Okay I think it's working now! The only thing is it doesn't play the file in a loop for some reason. So as it is a text to speech file, I'm simply going to have it repeated with pauses for a duration longer than I need so I can stop it playing.

Here is the code incase anyone else requires a similar function:

Code: Select all

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

int LDR = 0; // LDR Pin
int val = 0;       // variable to store the value coming from the sensor
int LDR2 = 1; // LDR Pin
int val2 = 1;       // variable to store the value coming from the sensor



// Function definitions (we define them here, but the code is below)
void lsR(FatReader &d);
void play(FatReader &dir);

void setup() {
  Serial.println(analogRead(0));   // print out value of light sensor on analog pin 0
  val = analogRead(LDR);       // read the value from the sensor
  if ( val > 300 ) play(root);
  Serial.println(analogRead(1));   // print out value of light sensor on analog pin 0
  val = analogRead(LDR2);       // read the value from the sensor
  if ( val > 300 ) play(root);
  
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  pinMode(LDR, INPUT);       // declare the LDR as an INPUT
  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());  
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
  
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
  

  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Files found:");
  dirLevel = 0;
  // Print out all of the files in all the directories.
  lsR(root);
}

//////////////////////////////////// LOOP
void loop() { 
     root.rewind();
//  play(root);
  val = analogRead(LDR);       // read the value from the sensor
if ( val < 300 ) play(root);
  val2 = analogRead(LDR2);
if ( val2 < 300) wave.stop();
}

/////////////////////////////////// HELPERS

// this handy function will return the number of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end; 
  extern int  *__brkval; 
  int free_memory; 
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end); 
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval); 
  }
  return free_memory; 
} 

/*
 * print error message and halt if SD I/O error, great for debugging!
 */
void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
/*
 * print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
 */
void printName(dir_t &dir)
{
  for (uint8_t i = 0; i < 11; i++) {     // 8.3 format has 8+3 = 11 letters in it
    if (dir.name[i] == ' ')
        continue;         // dont print any spaces in the name
    if (i == 8) 
        Serial.print('.');           // after the 8th letter, place a dot
    Serial.print(dir.name[i]);      // print the n'th digit
  }
  if (DIR_IS_SUBDIR(dir)) 
    Serial.print('/');       // directories get a / at the end
}
/*
 * list recursively - possible stack overflow if subdirectories too nested
 */
void lsR(FatReader &d)
{
  int8_t r;                     // indicates the level of recursion
  
  while ((r = d.readDir(dirBuf)) > 0) {     // read the next file in the directory 
    // skip subdirs . and ..
    if (dirBuf.name[0] == '.') 
      continue;
    
    for (uint8_t i = 0; i < dirLevel; i++) 
      Serial.print(' ');        // this is for prettyprinting, put spaces in front
    printName(dirBuf);          // print the name of the file we just found
    Serial.println();           // and a new line
    
    if (DIR_IS_SUBDIR(dirBuf)) {   // we will recurse on any direcory
      FatReader s;                 // make a new directory object to hold information
      dirLevel += 2;               // indent 2 spaces for future prints
      if (s.open(vol, dirBuf)) 
        lsR(s);                    // list all the files in this directory now!
      dirLevel -=2;                // remove the extra indentation
    }
  }
  sdErrorCheck();                  // are we doign OK?
}
/*
 * play recursively - possible stack overflow if subdirectories too nested
 */
void play(FatReader &dir)
{
  FatReader file;
  while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
    // skip . and .. directories
    if (dirBuf.name[0] == '.') 
      continue;
    
    Serial.println();            // clear out a new line
    
    for (uint8_t i = 0; i < dirLevel; i++) 
       Serial.print(' ');       // this is for prettyprinting, put spaces in front

    if (!file.open(vol, dirBuf)) {       // open the file in the directory
      Serial.println("file.open failed");  // something went wrong :(
      while(1);                            // halt
    }
    
    if (file.isDir()) {                    // check if we opened a new directory
      putstring("Subdir: ");
      printName(dirBuf);
      dirLevel += 2;                       // add more spaces
      // play files in subdirectory
      play(file);                         // recursive!
      dirLevel -= 2;    
    }
    else {
      // Aha! we found a file that isnt a directory
      putstring("Playing "); printName(dirBuf);       // print it out
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
        putstring(" Not a valid WAV");     // ok skip it
      } else {
        Serial.println();                  // Hooray it IS a WAV proper!
        wave.play();                       // make some noise!
       
         while (wave.isplaying) {  //periodically check LDR2 while wave is playing
          val = analogRead(LDR2);       // read the value from the sensor
          if ( val < 300 ) wave.stop();  //stop the wave! I want to get off!
          delay(100);
        }
        }
        sdErrorCheck();                    // everything OK?
//        if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
  }
Can I just say thank you Driverblock for your help and perseverance, much appreciated!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by adafruit_support_rick »

You're welcome!
Can you describe the project a little? Looks interesting - we like trains here!

3LFM4N
 
Posts: 13
Joined: Sat Mar 09, 2013 12:31 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by 3LFM4N »

I've designed this warning system for pedestrians at level crossings, where a wireless sensor installed 1.5kms from the crossing will detect the vibrations produced by a passing train. This sensor communicates with a speaker unit installed at the crossing which delivers the audio warning message 'train approaching' every few seconds, until the train arrives at the crossing and activates a 2nd sensor which turns it off.

The speaker unit also has another aspect that allows it to communicate the same warning to those nearby listening to music on their smartphone, via their headphones. This is an attempt to confront the growing trend of pedestrian incidents caused by distractions and inability to hear warning messages due to listening to music.

I've sourced all the components needed and how they work together, as well as designed the housing and the fixtures of the devices within the warning system. As the technology involved is beyond my skill-set, I needed to use the arduino and waveshield to construct a working simulation for my end of year exhibition!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by adafruit_support_rick »

Interesting. This is a school project?

fubbi
 
Posts: 3
Joined: Mon Mar 15, 2010 8:27 am

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by fubbi »

Hi

I am trying to do something similar. I have a photoresistor and when light hits (val > 100) I want to play a single file and when the sensor is in the dark (val < 100) the sound should stop.
The code is a little too advanced for me to dissect and I would like to recycle yours, minus the scanning for files. I just want to play (and stop) "audio.wav", thats it.

EDIT:

It works now, severe copy paste mess but works:

Code: Select all

/*
 * WaveShieldPlaySelection sketch
 *
 * play a selected WAV file
 *
 * Position of variable resistor slider when button pressed selects file to play
 *
 */

#include <FatReader.h>
#include <SdReader.h>

#include "WaveHC.h"
#include "WaveUtil.h"

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the volumes root directory
FatReader file;   // This object represents the WAV file
WaveHC wave;      // Only wave (audio) object - only one file played at a time


const int buttonPin = 15;
const int potPin = 0; // analog input pin 0

char * wavFiles[] = {
"bobby.wav","2.WAV","3.WAV","4.WAV","5.WAV","6.WAV","7.WAV","8.WAV","9.WAV"};

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // turn on pull-up resistor

  if (!card.init())
  {
    // Something went wrong, sdErrorCheck prints an error number
    putstring_nl("Card init. failed!"); 
    sdErrorCheck();
    while(1);                           // then 'halt' - do nothing!
  }


  // enable optimized read - some cards may time out
  card.partialBlockRead(true);

  // find a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++)      // we have up to 5 slots to look in
  {
    if (vol.init(card, part))
      break;                            // found one so break out of this for loop
  }
  if (part == 5)                        // valid parts are 0 to 4, more not valid
  {
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();                     // Something went wrong, print the error
    while(1);                           // then 'halt' - do nothing!
  }

  // tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?

  // Try to open the root directory
  if (!root.openRoot(vol))
  {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }

  // if here then all the file prep succeeded.
  putstring_nl("Ready!");
}

void loop()
{
  if(analogRead(A1) >= 100)
  {
    Serial.println("PLAY");
    playcomplete(wavFiles[0]);
    
  }
}

void playcomplete(char *name) 
{
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying)
  {
      if(analogRead(A1) <= 100)  // check for high signal on pin 14 (analog 0)
      {
        Serial.println("STOP");
          wave.stop();  // stop and return if no person detected
          delay(500);
          return;
      }
  }
}

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) { 
  // already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!file.open(root, name)) {
    putstring("Couldn't open file ");
    Serial.print(name);
    return;
  }
  // read the file and turn it into a wave object
  if (!wave.create(file)) {
    putstring_nl("Not a valid WAV");
    return;
  }
  // start playback
  wave.play();
}

void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1)
    ; // stay here if there is an error
}

User avatar
Kevinator108
 
Posts: 11
Joined: Tue Jul 19, 2016 3:08 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by Kevinator108 »

I want to activate only one single sound file with my wave shield (from my SD card) in response to a laser pointer beam contacting an LDR as well as an LED. I don't know how to do it. A sketch for something similar using a buzzer is out there but I cant figure how to modify or create one from scratch - any help appreciated.. This is the sketch with a buzzer that I want to use the wave sound file instead of:

Code: Select all

int ldrPin = A4;     // the cell and 10K pulldown are connected to a0
int sirenPin = 11;  //pin 3 selected!!
int ledPin = A0;
long ldrValue1, ldrValue2;

  
void setup(void) {
   pinMode (sirenPin,OUTPUT);  // set the siren pin as output
   pinMode (ledPin,OUTPUT);  // set the siren pin as output
   pinMode (ldrPin,INPUT);  // set the siren pin as output
   //Serial.begin(9600);  
}
 
void loop(void) {
  ldrValue1 = analogRead(ldrPin);  
  delay(10);
  ldrValue2 = analogRead(ldrPin);  

  if (ldrValue1-ldrValue2 > 20||ldrValue2-ldrValue1 > 20){
  digitalWrite(sirenPin,HIGH);
  digitalWrite(ledPin,HIGH);
  delay(1000);
  }
  else{
    digitalWrite(sirenPin,LOW);
    digitalWrite(ledPin,LOW);
  }
  //Serial.print("Analog reading = ");
  //Serial.println(ldrValue1);     // the raw analog reading
}
Last edited by adafruit_support_bill on Wed Jul 20, 2016 6:02 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by adafruit_support_bill »

Start with the example here: https://learn.adafruit.com/adafruit-wav ... o/play6-hc

Once you have that working, replace the code for one of the buttons with the code for your LDR. You can delete the code for the other 5 buttons.

User avatar
Kevinator108
 
Posts: 11
Joined: Tue Jul 19, 2016 3:08 pm

Re: 2 PIR sensors to turn WaveShield ON and OFF

Post by Kevinator108 »

OK. Thanks. I will try that.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”