如何让arduino播放SD卡中的WAV文件?

时间:2018-06-25 20:13:03

标签: arduino

我的朋友有一个问题,想要使用带有范围传感器的arduino来检测特定范围内的移动物体,并根据物体是否位于区域的后半部来播放SD卡中的两种不同声音运动或前半部分。 我们面临的问题是我们需要将SD卡连接到arduino,但是由于我们俩在arduino方面都没有那么先进,因此我们不确定自己在做什么错。 代码:

#include "Arduino.h"
#include "TMRpcm.h"
#include "SD.h"
// Max dist of movement
int d = 450; //Max distance in cm
#if !defined(SD_ChipSelectPin)
  #define SD_ChipSelectPin 4
#endif

// Pins for the sensor.
int trig = 11; // Attach Trig of ultrasonic sensor to pin 11
int echo = 10; // Attach Echo of ultrasonic sensor to pin 10
TMRpcm track1;
TMRpcm track2;

void setup() {
    if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD Card failed!");
    // stop if no SD detected
    return;
  }

    track1.play("001.wav");
    track1.loop(1);
    track2.play("002.wav");
    track2.loop(1);

    track1.speakerPin = 9;
    track2.speakerPin = 9;
}

void loop() 
{

    long duration, cm;

    pinMode(trig, OUTPUT);
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
    digitalWrite(trig, HIGH);
    delayMicroseconds(5);
    digitalWrite(trig, LOW);


    pinMode(echo, INPUT);
    duration = pulseIn(echo, HIGH);
    cm = microsecondsToCentimeters(duration);

    delay(1);

    if (cm>(d/2)) { // Play soundtrack 1

        track1.setVolume((d-cm)/2.25);
        track2.setVolume(0);
    }

    else { //Play soundtrack 2

        track2.setVolume((225-cm)/2.25);
        track1.setVolume(0);
    }
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

我们当前收到的错误是:

Arduino: 1.8.5 (Mac OS X), Board:"Arduino/Genuino Uno"

/Users/marinaveldhuizen/Downloads/Marina/Marina.ino:93:8: warning: extra tokens at end of #endif directive
 #endif }
        ^
/Users/marinaveldhuizen/Downloads/Marina/Marina.ino: In function 'void setup()':
/Users/marinaveldhuizen/Downloads/Marina/Marina.ino:20:23: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
  track1.play("001.wav");
                       ^
/Users/marinaveldhuizen/Downloads/Marina/Marina.ino:22:23: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
  track2.play("002.wav");
                       ^
/Users/marinaveldhuizen/Downloads/Marina/Marina.ino: In function 'long int microsecondsToCentimeters(long int)':
Meerdere bibliotheken gevonden voor "SD.h"
Marina:87: error: 'SD_ChipSelectPin' was not declared in this scope
 if (!SD.begin(SD_ChipSelectPin)) {
               ^
/Users/marinaveldhuizen/Downloads/Marina/Marina.ino:90:5: warning: return-statement with no value, in function returning 'long int' [-fpermissive]
     return;
     ^
Marina:111: error: a function-definition is not allowed here before '{' token
 void setup() {
              ^
Marina:186: error: expected '}' at end of input
 }
 ^
Marina:186: error: expected '}' at end of input
Gebruikt: /Users/marinaveldhuizen/Desktop/Arduino.app/Contents/Java/libraries/SD
Niet gebruikt: /Users/marinaveldhuizen/Documents/Arduino/libraries/SD-master
exit status 1
'SD_ChipSelectPin' was not declared in this scope

我有种感觉,我们正确定义了SD_ChipSelectPin,但是错误不断表明它没有声明,等等。 有谁知道如何解决这个问题,以及我们在代码中是否可能遇到其他错误(如果可以,我们该如何解决)?

1 个答案:

答案 0 :(得分:1)

我刚刚编译好您的草图,没有任何问题。我认为您为SD安装了多个库,并且IDE感到困惑。尝试从/Users/marinaveldhuizen/Documents/Arduino/libraries/SD-master删除SD库,然后重新启动Arduino IDE。您也可以尝试更改

#if !defined(SD_ChipSelectPin)
  #define SD_ChipSelectPin 4
#endif

#ifndef SD_ChipSelectPin
  #define SD_ChipSelectPin 4
#endif

否则,尝试重命名/Users/marinaveldhuizen/Documents/Arduino/文件夹,然后重新安装Arduino IDE和TMRpcm库。