Pages: [1]   Go Down
Author Topic: Turn on LED when Waveshield plays a .wav  (Read 357 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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);
           }

  


    
« Last Edit: February 06, 2013, 05:53:14 am by ClaraL » Logged

NE PA
Offline Offline
Full Member
***
Karma: 5
Posts: 156
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(wave.isplaying())
{
digitalWrite(ledPin, HIGH):
}
else
{
digitalWrite(ledPin, LOW);
}
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you! I completely missed that smiley-razz

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post the whole program so the problem can be seen in context
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Entire code so far! Thanks everyone c:


Code:
// 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
    



Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you posted the right program ?
I thought that you were going to try out wave.isplaying()
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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!*/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-grin

Code:
        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);
           
        }       
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

and which is right, the comment or the code ?
Code:
  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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: