Go Down

Topic: Turn on LED when Waveshield plays a .wav (Read 384 times) previous topic - next topic

ClaraL

Feb 06, 2013, 11:50 am Last Edit: Feb 06, 2013, 11:53 am by ClaraL Reason: 1
Hi guys,

I've been trying to get this working but I can't seem to figure the LED part out... Basically I want LED 11 to turn on when a .wav file plays. The rest of the code works but I think I'm just not placing the right code in the right places......

Code: [Select]
// LA_wav_play_skel

#include <FatReader.h>            // don't change any of these 'includes'   //
#include <SdReader.h>             // don't change any of these 'includes'   //
#include <avr/pgmspace.h>     // don't change any of these 'includes'   //
#include "WaveUtil.h"                 // don't change any of these 'includes'   //
#include "WaveHC.h"                  // don't change any of these 'includes'   //

// don't change any of the following block of code  //
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
FatReader f;         // This holds the information for the file we're play
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

// your variables go here

#define voltageFlipPin1 8
#define voltageFlipPin2 9
#define plantA 5
#define voltageFlipPin3 6
#define voltageFlipPin4 7
#define plantB 3

int ledPin = 11;// LED connected to digital pin 9
int ledState = 0;//state of the led



#include <SoftwareSerial.h>            // we need to include this library !

#define rxPin 30  // rxPin is immaterial - not used - just make this an unused Arduino pin number
#define txPin 2 // txPin is the one needed for the LCD - we'll use Digital Pin A0/14 to send serial data to the LCD
// mySerial is connected to the TX pin so mySerial.print commands are used
// one could just as well use the software mySerial library to communicate on another pin
// we're using SoftwareSerial so that we can save pins 0&1 for the BUB

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);    // let SoftwareSerial know what pins we'll use


int flipTimer = 500;



void setup()
{
   Serial.begin(9600);                 // set up serial port
   
   pinMode(voltageFlipPin1, OUTPUT);
   pinMode(voltageFlipPin2, OUTPUT);
   pinMode(plantA, INPUT);
   pinMode(voltageFlipPin3, OUTPUT);
   pinMode(voltageFlipPin4, OUTPUT);
   pinMode(plantB, INPUT);
   
   pinMode(ledPin, OUTPUT);
   
   Serial.println("  setup  ");    
   putstring_nl("Wave player program");
   set_up_wav_player();

   
}



void setSensorPolarityA(boolean flip){
 if(flip){
   digitalWrite(voltageFlipPin1, HIGH);
   digitalWrite(voltageFlipPin2, LOW);
 }else{
   digitalWrite(voltageFlipPin1, LOW);
   digitalWrite(voltageFlipPin2, HIGH);
 }
}

void setSensorPolarityB(boolean flip){
   if(flip){
   digitalWrite(voltageFlipPin3, HIGH);
   digitalWrite(voltageFlipPin4, LOW);
 }else{
   digitalWrite(voltageFlipPin3, LOW);
   digitalWrite(voltageFlipPin4, HIGH);
 }
}


void loop()
{
   
   
   
   //
   setSensorPolarityA(true);
   delay(flipTimer);
   int val1 = analogRead(plantA);
   delay(flipTimer);  
   setSensorPolarityA(false);
   delay(flipTimer);
   // invert the reading
   int val2 = 2023 - analogRead(plantA);
   //
   
   //
   setSensorPolarityB(true);
   delay(flipTimer);
   int val3 = analogRead(plantB);
   delay(flipTimer);  
   setSensorPolarityB(false);
   delay(flipTimer);
   // invert the reading
   int val4 = 2023 - analogRead(plantB);
   //
   reportLevels(val1,val2,val3,val4);
   
   
   
       

}

   void reportLevels(int val1,int val2, int val3, int val4){
   //reports levels AND does the wav testing
 
       int avgA = (val1 + val2);
 
       String msgA = "avgA: ";
       msgA += avgA;
       Serial.println(msgA);
     
       int avgB = (val3 + val4);
 
       String msgB = "avgB: ";
       msgB += avgB;
       Serial.println(msgB);
   

       int moistA = avgA;
       int moistB = avgB;
   
   
       while (moistA >= 1500){        //////When moistA is higher than 200=
           
          if (moistB >= 200){        //.....and moistB is higher than 200=
              int quote = random(1, 5);
              switch(quote){
                  case 1:
                  Serial.print("a.wav song name");
                  playcomplete("A.WAV");
                  case 2:
                  Serial.print("b.wav song name");
                  playcomplete("B.WAV");
                  case 3:
                  Serial.print("c.wav song name");
                  playcomplete("C.WAV");
                  break;  
              }

              break;
              delay(100);
          }

 


   

Quick5pnt0

Check out this page: http://www.ladyada.net/make/waveshield/libraryhcplay6.html

Toward the middle of the page they show how to use wave.isplaying to detect if the file is currently playing. I don't have a wave shield so I can't test it but I believe something like this should work.

Code: [Select]
if(wave.isplaying())
{
digitalWrite(ledPin, HIGH):
}
else
{
digitalWrite(ledPin, LOW);
}

ClaraL

Thank you! I completely missed that :P

I'm going to try that out and let you know how it goes

ClaraL

Ah! This still doesn't work... For some reason the LED remains lit and does not change at all

UKHeliBob

Please post the whole program so the problem can be seen in context
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ClaraL

Entire code so far! Thanks everyone c:


Code: [Select]
// LA_wav_play_skel
// a good skeleton program for using the lady ada's 2 important functions- playcomplete and playfile
// this program/sketch allows one to play specific files on an SD Card.
//
// stripped down LadyAda Wave Shield Code
//  adapted from:  http://www.ladyada.net/media/wavshield/daphc.pde
// Lucky and Dory
//
// we have moved the SD card functions and setup to the end of the program - leave them be please.
//  code you should modify is in the loop function

// don't change any of these 'includes'   //
#include <FatReader.h>            // don't change any of these 'includes'   //
#include <SdReader.h>             // don't change any of these 'includes'   //
#include <avr/pgmspace.h>     // don't change any of these 'includes'   //
#include "WaveUtil.h"                 // don't change any of these 'includes'   //
#include "WaveHC.h"                  // don't change any of these 'includes'   //

// don't change any of the following block of code  //
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
FatReader f;         // This holds the information for the file we're play
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

// your variables go here

#define voltageFlipPin1 8
#define voltageFlipPin2 9
#define plantA 4
#define voltageFlipPin3 6
#define voltageFlipPin4 7
#define plantB 3
#define voltageFlipPin5 11
#define voltageFlipPin6 12
#define plantC 2


int ledState = 0;//state of the led
int fadeDown = 10;//delay per fade
int fadeUp = 10;//delay per fade
int minLight = 1500;//min light threshold
int maxLight = 100;//max light threshold


#include <SoftwareSerial.h>            // we need to include this library !

#define rxPin 30  // rxPin is immaterial - not used - just make this an unused Arduino pin number
#define txPin 2 // txPin is the one needed for the LCD - we'll use Digital Pin A0/14 to send serial data to the LCD
// mySerial is connected to the TX pin so mySerial.print commands are used
// one could just as well use the software mySerial library to communicate on another pin
// we're using SoftwareSerial so that we can save pins 0&1 for the BUB

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);    // let SoftwareSerial know what pins we'll use


int flipTimer = 500;


void setup()
{
   Serial.begin(9600);                 // set up serial port
   
   pinMode(voltageFlipPin1, OUTPUT);
   pinMode(voltageFlipPin2, OUTPUT);
   pinMode(plantA, INPUT);
   pinMode(voltageFlipPin3, OUTPUT);
   pinMode(voltageFlipPin4, OUTPUT);
   pinMode(plantB, INPUT);
   pinMode(voltageFlipPin5, OUTPUT);
   pinMode(voltageFlipPin6, OUTPUT);
   pinMode(plantC, INPUT);
   

   
   Serial.println("  setup  ");    
   putstring_nl("Wave player program");
   set_up_wav_player();

   
}



void setSensorPolarityA(boolean flip){
 if(flip){
   digitalWrite(voltageFlipPin1, HIGH);
   digitalWrite(voltageFlipPin2, LOW);
 }else{
   digitalWrite(voltageFlipPin1, LOW);
   digitalWrite(voltageFlipPin2, HIGH);
 }
}

void setSensorPolarityB(boolean flip){
   if(flip){
   digitalWrite(voltageFlipPin3, HIGH);
   digitalWrite(voltageFlipPin4, LOW);
 }else{
   digitalWrite(voltageFlipPin3, LOW);
   digitalWrite(voltageFlipPin4, HIGH);
 }
}

void setSensorPolarityC(boolean flip){
   if(flip){
   digitalWrite(voltageFlipPin5, HIGH);
   digitalWrite(voltageFlipPin6, LOW);
 }else{
   digitalWrite(voltageFlipPin5, LOW);
   digitalWrite(voltageFlipPin6, HIGH);
 }
}

void loop()
{
   
   
   
   //
   setSensorPolarityA(true);
   delay(flipTimer);
   int val1 = analogRead(plantA);
   delay(flipTimer);  
   setSensorPolarityA(false);
   delay(flipTimer);
   // invert the reading
   int val2 = 2023 - analogRead(plantA);
   //
   
   //
   setSensorPolarityB(true);
   delay(flipTimer);
   int val3 = analogRead(plantB);
   delay(flipTimer);  
   setSensorPolarityB(false);
   delay(flipTimer);
   // invert the reading
   int val4 = 2023 - analogRead(plantB);
   //
   
   //
   setSensorPolarityC(true);
   delay(flipTimer);
   int val5 = analogRead(plantC);
   delay(flipTimer);  
   setSensorPolarityC(false);
   delay(flipTimer);
   // invert the reading
   int val6 = 2023 - analogRead(plantC);
   //
   
   reportLevels(val1,val2,val3,val4,val5,val6);

       

}

   void reportLevels(int val1,int val2, int val3, int val4, int val5, int val6){
   //reports levels AND does the wav testing
 
       int avgA = (val1 + val2);
 
       String msgA = "avgA: ";
       msgA += avgA;
       Serial.println(msgA);
     
       int avgB = (val3 + val4);
 
       String msgB = "avgB: ";
       msgB += avgB;
       Serial.println(msgB);
   
       int avgC = (val5 + val6);
 
       String msgC = "avgC: ";
       msgC += avgC;
       Serial.println(msgC);
       
       
       int moistA = avgA;
       int moistB = avgB;
       int moistC = avgC;

   
       while (moistA >= 1500){        //////When moistA is higher than 200=
           
          if (moistB >= 200){        //.....and moistB is higher than 200=

              int quote = random(1, 5);
              switch(quote){
                  case 1:
                  Serial.print("a.wav song name");
                  playcomplete("1.WAV");
                  case 2:
                  Serial.print("b.wav song name");
                  playcomplete("2.WAV");
                  case 3:
                  Serial.print("c.wav song name");
                  playcomplete("3.WAV");
                  break;  
              }

              break;

              delay(100);

          }
          if (moistB < 100 && moistB >= 200){        //.....and moistB is between 100 & 200=
              int quote = random(1, 5);
              switch(quote){
                  case 1:
                  Serial.print("a.wav song name");
                  playcomplete("A.WAV");
                  case 2:
                  Serial.print("b.wav song name");
                  playcomplete("B.WAV");
                  case 3:
                  Serial.print("c.wav song name");
                  playcomplete("C.WAV");
                  break;  
              }
              break;
              delay(500);
          }
          if (moistB < 100){        //.....and moistB is lower than 100
              int quote = random(1, 5);
              switch(quote){
                  case 1:
                  Serial.print("a.wav song name");
                  playcomplete("A.WAV");
                  case 2:
                  Serial.print("b.wav song name");
                  playcomplete("B.WAV");
                  case 3:
                  Serial.print("c.wav song name");
                  playcomplete("C.WAV");
                  break;  
              }
              break;
              delay(500);
          }
         
         
       }        //while A > B... then 1,2,3
       
   }
 
   //voidreport: mini mama
   




UKHeliBob

Have you posted the right program ?
I thought that you were going to try out wave.isplaying()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ClaraL

Ah! wrong file. Here's the real one.

Esstentially, I'll also have moistB, and moistC along with moistA, and they all have different .wav files. I'd like it so that when .wav files in association with moistA plays, ledA goes on.....

Code: [Select]

#include <FatReader.h>            // don't change any of these 'includes'   //
#include <SdReader.h>             // don't change any of these 'includes'   //
#include <avr/pgmspace.h>     // don't change any of these 'includes'   //
#include "WaveUtil.h"                 // don't change any of these 'includes'   //
#include "WaveHC.h"                  // don't change any of these 'includes'   //

// don't change any of the following block of code  //
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
FatReader f;         // This holds the information for the file we're play
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

// your variables go here

#define voltageFlipPin1 6
#define voltageFlipPin2 7
#define plantA A4
#define voltageFlipPin3 A5
#define voltageFlipPin4 A0
#define plantB A3
#define voltageFlipPin5 8
#define voltageFlipPin6 9
#define plantC A2





#include <SoftwareSerial.h>            // we need to include this library !

#define rxPin 30  // rxPin is immaterial - not used - just make this an unused Arduino pin number
#define txPin 2 // txPin is the one needed for the LCD - we'll use Digital Pin A0/14 to send serial data to the LCD
// mySerial is connected to the TX pin so mySerial.print commands are used
// one could just as well use the software mySerial library to communicate on another pin
// we're using SoftwareSerial so that we can save pins 0&1 for the BUB

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);    // let SoftwareSerial know what pins we'll use

int ledA = A1;
int flipTimer = 500;



void setup()
{
   Serial.begin(9600);                 // set up serial port
   
   pinMode(voltageFlipPin1, OUTPUT);
   pinMode(voltageFlipPin2, OUTPUT);
   pinMode(plantA, INPUT);
   pinMode(voltageFlipPin3, OUTPUT);
   pinMode(voltageFlipPin4, OUTPUT);
   pinMode(plantB, INPUT);
   pinMode(voltageFlipPin5, OUTPUT);
   pinMode(voltageFlipPin6, OUTPUT);
   pinMode(plantC, INPUT);
   
   pinMode(ledA, OUTPUT);

   
   Serial.println("  setup  ");    
   putstring_nl("Wave player program");
   set_up_wav_player();

   
}



void setSensorPolarityA(boolean flip){
 if(flip){
   digitalWrite(voltageFlipPin1, HIGH);
   digitalWrite(voltageFlipPin2, LOW);
 }
 else{
   digitalWrite(voltageFlipPin1, LOW);
   digitalWrite(voltageFlipPin2, HIGH);
 }

}

void setSensorPolarityB(boolean flip){
   if(flip){
   digitalWrite(voltageFlipPin3, HIGH);
   digitalWrite(voltageFlipPin4, LOW);
 }
 else{
   digitalWrite(voltageFlipPin3, LOW);
   digitalWrite(voltageFlipPin4, HIGH);
 }

}

void setSensorPolarityC(boolean flip){
   if(flip){
   digitalWrite(voltageFlipPin5, HIGH);
   digitalWrite(voltageFlipPin6, LOW);
 }
 else{
   digitalWrite(voltageFlipPin5, LOW);
   digitalWrite(voltageFlipPin6, HIGH);
 }

}


void loop()
{

   //
   setSensorPolarityA(true);
   delay(flipTimer);
   int val1 = analogRead(plantA);
   delay(flipTimer);  
   setSensorPolarityA(false);
   delay(flipTimer);
   // invert the reading
   int val2 = 1023 - analogRead(plantA);
   //
   
   //
   setSensorPolarityB(true);
   delay(flipTimer);
   int val3 = analogRead(plantB);
   delay(flipTimer);  
   setSensorPolarityB(false);
   delay(flipTimer);
   // invert the reading
   int val4 = 1023 - analogRead(plantB);
   //
   
   //
   setSensorPolarityC(true);
   delay(flipTimer);
   int val5 = analogRead(plantC);
   delay(flipTimer);  
   setSensorPolarityC(false);
   delay(flipTimer);
   // invert the reading
   int val6 = 1023 - analogRead(plantC);
   //
   
   reportLevels(val1,val2,val3,val4,val5,val6);

       
}

   void reportLevels(int val1,int val2, int val3, int val4, int val5, int val6){
   //reports levels AND does the wav testing
 
       int avgA = (val1 + val2) / 2;
 
       String msgA = "avgA: ";
       msgA += avgA;
       Serial.println(msgA);
     
       int avgB = (val3 + val4) / 2;
 
       String msgB = "avgB: ";
       msgB += avgB;
       Serial.println(msgB);
   
       int avgC = (val5 + val6) / 2;
 
       String msgC = "avgC: ";
       msgC += avgC;
       Serial.println(msgC);
       
       
       int moistA = avgA;
       int moistB = avgB;
       int moistC = avgC;

//dying hungry, good stop 0-1023
//0-100 100-300 300-700 700-1023
   
       while (moistA < 100){        //////When moistA is higher than 200=
           
 
         
          int quote = random(1, 5);
          switch(quote){

              case 1:
              Serial.print("DYING1");
              playcomplete("DYIN1.WAV");
              //break;
             
              case 2:                
              Serial.print("DYING2");
              playcomplete("DYIN2.WAV");

             
          }
         
       
          if (wave.isplaying) {
              digitalWrite(ledA, HIGH);
          }
         
          else{
              digitalWrite(ledA, LOW);
          }
           break;
          delay(100);
         
       }        //while A > B... then 1,2,3
       
       
       
       while (moistA > 100){      
       
 
              digitalWrite(ledA, LOW);
         

         
       }
       
       
       
       
   }
 

   





   
   

////////// You don't need to change things below this line unless you want to ////////////

/*I took out Ladyada's code here for the Waveshield so I don't go over 9600 characters!*/

ClaraL

I figured it out!! Attached is the code that works if anyone else needs it! It's ugly, but it works...

If you guy know of a more elegant way of writing it... please let me know! I would like to keep improving my code writing :D

Code: [Select]
        while (moistA < 100){       
           
   
           
           int quote = random(1, 5);
           switch(quote){
               
               
               case 1:
               digitalWrite(ledA, HIGH);
               Serial.print("DYING1");
               playcomplete("DYIN1.WAV");
               break;
               
               case 2:   
               digitalWrite(ledA, HIGH);
               Serial.print("DYING2");
               playcomplete("DYIN2.WAV");
               break;   
               
           }
           
       
               digitalWrite(ledA, LOW);

            break;
           delay(100);
           
        }       

UKHeliBob

I will look at doing it elegantly, but as it stands what happens if random(1,5) returns a value other than 1 or 2 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UKHeliBob

Comments :
How will this while loop ever end ?
Code: [Select]
  while (moistA > 100)
  {       
    digitalWrite(ledA, LOW);
  }


and which is right, the comment or the code ?
Code: [Select]
  while (moistA < 100){        //////When moistA is higher than 200=

More generally, your program has a number of possible states that it could be in depending on the sensors readings.  When dealing with such cases I find it helps to write down each state, the values that cause the program to move into that state, what needs to happen to change state and the new state when conditions are met.  Give each state a number then, during each pass through loop() check for a change of conditions that would cause the current state to change and act on it.  This makes the code more readable, easier to debug and it is easier to see when a combination of conditions has not been allowed for.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up