Android 使用和LDR播放SD卡音频时出现问题
我的项目遇到了一些障碍。我已经试了好几个小时,但似乎找不出是什么导致了我的问题 我试图创建一个盒子,当它关闭时,我可以使用蓝牙命令,arduino将播放响应。 当你打开盒子时,它将播放一段独特的音频片段 问题是,LDR工作正常,就像我打开盒子时播放的独特片段一样,但是当盒子关闭时,蓝牙响应不会按预期播放,它们只是播放打开时独特响应的一小部分? 因此,一旦我打开盒子,独特的剪辑播放和蓝牙响应的工作预期 我试过使用开关盒和for循环,但没有用 非常感谢您的帮助Android 使用和LDR播放SD卡音频时出现问题,android,audio,arduino,Android,Audio,Arduino,我的项目遇到了一些障碍。我已经试了好几个小时,但似乎找不出是什么导致了我的问题 我试图创建一个盒子,当它关闭时,我可以使用蓝牙命令,arduino将播放响应。 当你打开盒子时,它将播放一段独特的音频片段 问题是,LDR工作正常,就像我打开盒子时播放的独特片段一样,但是当盒子关闭时,蓝牙响应不会按预期播放,它们只是播放打开时独特响应的一小部分? 因此,一旦我打开盒子,独特的剪辑播放和蓝牙响应的工作预期 我试过使用开关盒和for循环,但没有用 非常感谢您的帮助 //Coded By: Angelo
//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);
}
//编码人:Angelo Casimiro(4/27/14)
//语音激活的Arduino(蓝牙+Android)
//请随意修改它,但请记住给予信用
//PCM库用于将语音文件转换为扬声器的文本字符串。
//归功于高科技
#包含//需要包含SD库
//#定义SD_芯片SelectPin 53//示例使用Mega2560上的硬件SS引脚53
#定义SD_芯片选择引脚4//在arduino nano 328上使用数字引脚4,可以使用其他引脚
#包含//还需要包含此库。。。
#包括
TMRpcm TMRpcm;//创建用于此草图的对象
int-ldrPin=A2//模拟引脚2上的已声明LDR
int-ldrValue=0//从LDR读取不同的值
//将语音声明为数据字符串
弦乐;
无效设置(){
//扬声器插脚
tmrpcm.speakerPin=9;
Serial.begin(9600);
如果(!SD.begin(SD_ChipSelectPin)){//查看卡是否存在并且可以初始化:
Serial.println(“SD失败”);
return;//否则不要再做任何事情
}
}
//-----------------------------------------------------------------------//
void loop(){
while(Serial.available()){//检查是否有可用字节可读取
延迟(50);//增加延迟使事情稳定
char c=Serial.read();//进行串行读取
如果(c=='#'){break;}//在单词后面检测到#时退出循环
voice+=c;//voice=voice+c的简写
}
如果(voice.length()>0){
序列号println(语音);
}
//从模拟引脚2定义的LDR变量
ldrValue=模拟读取(ldrPin);
//定义LDR值范围并将其映射到案例
//如果语音字符串为“marco”,则播放示例数组
如果(声音=“*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”);}
如果(ldrValue>500){tmrpcm.play(“Burp.wav”);}
声音=”;
延迟(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();
}
//编码人:Angelo Casimiro(4/27/14)
#包含//需要包含SD库
//#定义SD_芯片SelectPin 53//示例使用Mega2560上的硬件SS引脚53
#定义SD_芯片选择引脚4//在arduino nano 328上使用数字引脚4,可以使用其他引脚
#包含//还需要包含此库。。。
#包括
TMRpcm TMRpcm;//创建用于此草图的对象
//将语音声明为数据字符串
弦乐;
//LDR值范围
const int sensorMin=100;
const int sensorMax=480;
无效设置()
{
//扬声器插脚
tmrpcm.speakerPin=9;
Serial.begin(9600);
如果(!SD.begin(SD_芯片选择PIN))
{
//查看该卡是否存在并可以初始化:
Serial.println(“SD失败”);
return;//否则不要再做任何事情
}
}
//-----------------------------------------------------------------------//
void循环()
{
while(Serial.available())
{
//检查是否有可读取的字节
延迟(50);//增加延迟使事情稳定
char c=Serial.read();//进行串行读取
如果(c='#')
{
布雷