Yes, I'm sorry I am a bit confused, trying my best to explain. I have the Adafruit "Music Maker" Mp3 Shield (the VS1053) attached to the Arduino Uno R3. I'm trying to attach the Ultrasonic Sensor from the Super Starter Kit Uno R3. Right now the Arduino is just powered through usb connection to my computer but I have Elegoo AC-DC adaptors (Input: AC 100-240V 50/60Hz 0.25A Output: DC 9V) I can get a different power supply if needed, I just want to make sure I'm connecting the Ultrasonic Sensor to the Arduino appropriately. Here's my previous code for reference:
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// define the pins used
//#define CLK 13 // SPI Clock, shared with SD card
//#define MISO 12 // Input data, from VS1053/SD card
//#define MOSI 11 // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See http://arduino.cc/en/Reference/SPI "Connections"
// These are the pins used for the breakout example
#define BREAKOUT_RESET 9 // VS1053 reset pin (output)
#define BREAKOUT_CS 10 // VS1053 chip select pin (output)
#define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
const int pingPin = 2; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 5; // Echo Pin of Ultrasonic Sensor
// SOUND PLAYBACK VARIABLES
int currentFile = 0;
int pauseTime = 0; // How long has it been paused?
int waitTime = 10; // How many seconds to stay paused before stopping?
// Currently, ten sound files to consider playing.
// This is the list of their names.
char *soundFiles[] = {"/track001.ogg", "/track002.ogg", "/track003.ogg", "/track004.ogg", "/track005.ogg",
"/track006.ogg", "/track007.ogg", "/track008.ogg", "/track009.ogg" } ;
Adafruit_VS1053_FilePlayer musicPlayer =
// create breakout-example object!
// Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
void setup() {
Serial.begin(9600);
Serial.println("Adafruit VS1053 Simple Test");
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
Serial.println(F("VS1053 found"));
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
// Geers really doesn't think this is needed...
// printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20, 20);
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
// CONVERT waitTime from seconds to milliseconds:
waitTime = waitTime * 1000;
}
// MAIN CODE LOOP
void loop() {
// Define some variables:
long duration, inches;
// GET SENSOR DISTANCE reading
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.println();
// delay(100);
//then check the distance and play file 1,2,3,4,5,6,7,8 if it's in the range
if (inches >= 200 || inches <= 0) {
Serial.println("Out of range");
}
else if (inches < 48) {
if (musicPlayer.stopped() == 1) {
// If player was stopped, look to see what is next file to play, and start it.
// NOTICE: Now instead of putting in actual sound file name, we use a variable.
musicPlayer.playFullFile(soundFiles[currentFile]);
// Tell human what file we're playing
Serial.print("Starting to play ");
Serial.println(soundFiles[currentFile]);
}
else if (musicPlayer.paused() == 1) {
// If player was paused, then continue playing current sound file.
musicPlayer.playFullFile(soundFiles[currentFile]);
}
// Reset wait time, since we're not waiting to play sound any longer.
waitTime = 0; // reset wait time to zero. we're not waiting now...
// Print some info to human user:
Serial.print(inches);
Serial.println(" inches");
}
else {
// If no one is close, pause or stop playback.
// first, check if it is already paused. If not, pause it.
if (musicPlayer.paused() == 0) {
// Serial.print(inches);
Serial.println("Playback is PAUSED.");
musicPlayer.pausePlaying(true);
}
else if (pauseTime > waitTime) {
// If it is already paused, check how long it has been paused. If too long, stop the file.
Serial.println("Playback has STOPPED.");
musicPlayer.stopPlaying();
// increment "currentFile" variable to point to next one in the series.
currentFile = currentFile + 1;
// If we're counting beyond actual # of files, reset to file #0, the first one.
if (currentFile > 9) {
currentFile = 0;
}
}
}
delay(2000); // Wait some bit of time before we check sensor again.
// If we are paused, increment waitTime variable the duration in delay command above.
if (musicPlayer.paused() == 1) {
waitTime = waitTime + 2000;
}
}
// END OF MAIN LOOP ** END OF MAIN LOOP
// END OF MAIN LOOP ** END OF MAIN LOOP
/// File listing helper code
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}