/* HC-SR04 Sensor Portion https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696 This sketch reads a HC-SR04 ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor. The circuit: * VCC connection of the sensor attached to +5V * GND connection of the sensor attached to ground * TRIG connection of the sensor attached to digital pin 2 * ECHO connection of the sensor attached to digital pin 4 Original code for Ping))) example was created by David A. Mellis Adapted for HC-SR04 by Tautvidas Sipavicius SD Card reader & Player Portion DO NOT USE CLASS-10 CARDS on this project - they're too fast to operate using SPI Created by NorseEngineering October 2013 */ #include //This is the modified SD Library. #include TMRpcm tmrpcm; File root; File entry; // set chipSelect to '10' if using the $2 SD card module or '4' if using the // Ethernet shield's microSD card instead. const int chipSelect = 10; const int oldCard = SPI_HALF_SPEED; const int newCard = SPI_QUARTER_SPEED; const int trigPin = 2; const int echoPin = 4; const int led = 8; //The distance below which the sound triggers (in feet). Must be an integer const int DISTANCE = 6; //Duration of the LED on phase. const int CLIP_DURATION = 10000; //Refresh rate for the sensor. const int RATE = 10; // set cardType to 'oldCard' if using an old SD card (more than a few years old) or // to 'newCard' if using a newly-purchase Class-4 card. int cardType = oldCard; int wasPlaying = 0; int inSwitch = 7; int finished = 0; int start = 0; int pauseOn = 0; unsigned long timeDiff = 0; unsigned long timePress = 0; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); Serial.print("\nInitializing SD card..."); pinMode(chipSelect, OUTPUT); if (!SD.begin(chipSelect,cardType)) { Serial.println("failed!"); return; } Serial.println("done."); tmrpcm.speakerPin = 9; pinMode(inSwitch,INPUT_PULLUP); digitalWrite(inSwitch,HIGH); root = SD.open("/"); } void loop(void) { long duration, inches, cm, feet; pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); feet = microsecondsToFeet(duration); Serial.print(feet); Serial.print("ft, "); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); //Meat of the code here in this if statement. if(feet < DISTANCE ) { playNext(); delay(CLIP_DURATION); } delay(RATE); digitalWrite(led, LOW); } void playNext() { digitalWrite(led, HIGH); entry = root.openNextFile(); if (entry) { entry.close(); tmrpcm.play(entry.name()); wasPlaying = 1; Serial.print("wasPlaying = "); Serial.println(wasPlaying); } else { if (wasPlaying == 1) { Serial.println("Completed playback."); wasPlaying = 0; finished = 1; start = 0; root.rewindDirectory(); } } } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } long microsecondsToFeet(long microseconds) { return microsecondsToInches(microseconds) / 12 ; }