Reputation: 363
My friend has this problem of wanting to use an arduino with a range sensor to detect a moving object within a certain range, and play two different sound from an SD card depending on whether the object is in the back half of the area of movement, or on the front half. The problem we are facing is that we need to attach the SD card to the arduino, but since we both aren't that advanced with the arduino, we are unsure what we're doing wrong. The code:
#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;
}
The current error we receive is:
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
I have the feeling that we defined the SD_ChipSelectPin correctly, but the error keeps saying that it isn't declared etc. Does anyone know how to solve this and if there are any other errors in this code which we could face (and if so how could we solve them)?
Upvotes: 0
Views: 539
Reputation: 792
I'v just compiled your sketch without problems. I think you have multiple libraries for SD installed and the IDE gets confused. Try to delete the SD library from /Users/marinaveldhuizen/Documents/Arduino/libraries/SD-master
and relaunch Arduino IDE. You can also try to change
#if !defined(SD_ChipSelectPin)
#define SD_ChipSelectPin 4
#endif
to
#ifndef SD_ChipSelectPin
#define SD_ChipSelectPin 4
#endif
Else try to rename /Users/marinaveldhuizen/Documents/Arduino/
folder and the reinstall the Arduino IDE and the TMRpcm library.
Upvotes: 1