使用和LDR从SD卡播放音频时出现问题

时间:2015-04-28 10:43:30

标签: android audio arduino

我的项目遇到了一些障碍。我已经尝试了几个小时,似乎无法找出导致我问题的原因。

我正在尝试创建一个盒子,当它关闭时我可以使用蓝牙命令,arduino会播放响应。 当您打开该框时,它将播放一个独特的音频剪辑。

问题是,LDR工作正常,因为每当我打开盒子时都会播放独特的剪辑,但是当盒子关闭时,蓝牙响应不能按预期播放,它们只是播放一小段独特的响应从何时开放? 因此,一旦打开盒子,就会播放独特的剪辑,并且蓝牙响应会按预期工作。

我尝试过使用switch case和for循环,但无济于事。

非常感谢您的帮助!

//Coded By: Angelo Casimiro (4/27/14)                                          
//Voice Activated Arduino (Bluetooth + Android)                                
//Feel free to modify it but remember to give credit                           
//PCM library used to turn voice file into a string of text for the speaker.  
//Credit to HighTechLowTech                                                    


#include <SD.h>                    // need to include the SD library           
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560   
#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins 
#include <TMRpcm.h>           //  also need to include this library...         
#include <SPI.h>                                                               

TMRpcm tmrpcm;   // create an object for use in this sketch                    
int ldrPin = A2; //declared LDR on analog pin 2                                
int ldrValue = 0; //reading different values from the LDR                      


//declare voice as a string of data                                            
String voice;                                                                  


void setup() {                                                                 
    //Speaker pin                                                              
    tmrpcm.speakerPin = 9;                                                     
    Serial.begin(9600);                                                        
    if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized: 
        Serial.println("SD fail");                                          
        return;   // don't do anything more if not                             
    }                                                                          
}                                                                              
//-----------------------------------------------------------------------//   
void loop() {                                                                  
    while (Serial.available()){  //Check if there is an available byte to read 
        delay(50); //Delay added to make thing stable                          
        char c = Serial.read(); //Conduct a serial read                        
        if (c == '#') {break;} //Exit the loop when the # is detected after the word 
        voice += c; //Shorthand for voice = voice + c                          
    }                                                                         
    if (voice.length() > 0) {                                                  
        Serial.println(voice);                                                 
    }                                                                          
    //LDR variable defined from analog pin 2                                   
    ldrValue = analogRead(ldrPin);                                             
    //define LDR value ranges and map them to cases                            


    //if voice string is "marco" then playback the sample array                
    if(voice == "*marco") {tmrpcm.play("Polo!.wav");}                          
    else if(voice == "*hello") {tmrpcm.play("Hello!.wav");}                    
    else if(voice == "*where") {tmrpcm.play("here.wav");}                      
    else if(voice == "*who") {tmrpcm.play("Scott.wav");}                       
    else if(voice == "*knock knock") {tmrpcm.play("Who.wav");}                 
    else if(ldrValue > 500)  {tmrpcm.play("Burp.wav");}                        
    voice="";                                                                  

    delay(1);                                                                  
}    

使用切换案例可能更容易在此草图中看到。

基本上,当盒子关闭时,我可以在串行监视器中看到案例2处于活动状态,这就是我想要的。 当盒子打开时,案例1处于活动状态,但是在我再次关闭它并且它切换回案例2之前它没有播放'Burp.wav'

//Coded By: Angelo Casimiro (4/27/14)


#include <SD.h>                    // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch

//declare voice as a string of data
String voice;
//LDR value Range
const int sensorMin = 100;
const int sensorMax = 480;

void setup()
{
  //Speaker pin
  tmrpcm.speakerPin = 9;
  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin))
  {
    // see if the card is present and can be initialized:
    Serial.println("SD fail");
    return;   // don't do anything more if not
  }
}
//-----------------------------------------------------------------------//
void loop()
{
  while (Serial.available())
  {
    //Check if there is an available byte to read
    delay(50); //Delay added to make thing stable
    char c = Serial.read(); //Conduct a serial read
    if (c == '#')
    {
      break; //Exit the loop when the # is detected after the word
    }
    voice += c; //Shorthand for voice = voice + c
  }
  if (voice.length() > 0)
  {
    Serial.println(voice);
  }

  //LDR variable defined from analog pin 2
  int sensorReading = analogRead(A2);
  Serial.println(sensorReading);
  //define LDR value ranges and map them to cases
  int range = map(sensorReading, sensorMin, sensorMax, 1, 2);
  Serial.println(range);
  switch (range)
  {
    case 1:
      {
        tmrpcm.play("Burp.wav");
      }
      break;

    case 2:
      //if voice string is "marco" then playback the sample array
      if (voice == "*marco") 
      {
        tmrpcm.play("Polo!.wav");
      }
      else if (voice == "*hello") 
      {
        tmrpcm.play("Hello!.wav");
      }
      else if (voice == "*where") 
      {
        tmrpcm.play("here.wav");
      }
      else if (voice == "*who") 
      {
        tmrpcm.play("Scott.wav");
      }
      else if (voice == "*knock knock") 
      {
        tmrpcm.play("Who.wav");
      }
      voice = "";
      break;


  }
  delay(50);
  Serial.println();
}

0 个答案:

没有答案
相关问题