Hello,
Im able to get the ping))) ultrasonic sensor to work with an Adruino UNO which also has the adafruit music maker shield on it. And in a separate sketch Im able to get the music maker shield to play a song with the same setup. But when I put the code together slowly and piece by piece, the sensor wont seem to take readings with the serial monitor reporting the max distance, and the status light on the sensor remains off instead of blinking when its working.
Ive looked at schematics and checked my code I cant identify any conflicts in the pins that I set for each. But if I comment out the code in void setup() that initializes the music player, the ultrasonic Ping))) sensor works fine.
here is my code where Im not even calling a music maker function, it's only the initialization from the example file that seems to make it not work:
//adapt music maker interrupts example to Halloween show, pair down
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. [this is an arbitrary number]
unsigned int duration, inches, cm;
#include "NewPing.h"
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
unsigned int critical_distance_in = 50; // Cutoff distance at which the light will switch [this is an arbitrary number]
bool state = 0;
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// 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
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);
////
#define buttonPin 2
#define ledPin 13
#define RELAY1 8
#define RELAY2 9
boolean sensedPerson = LOW;
boolean showStarted = LOW; //error that showStarted was not declared in void loop scope
boolean startShow = LOW;
boolean showRun = LOW;
unsigned long microsNow;
int i;
int restartAttempt =0;
//==========================================================
void setup() {
Serial.begin(9600);
Serial.println("Setup Halloween Show02. ");
microsNow = micros(); //Initailize
pinMode(RELAY1, OUTPUT);
digitalWrite(RELAY1,HIGH); // Turns Relay Off
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2,HIGH); // Turns Relay Off
pinMode(buttonPin, INPUT); // Set the button pin as input
pinMode(ledPin, OUTPUT); // Set the status led pin as output
digitalWrite(ledPin,LOW); // would this Turns Status LED Off?
// initialize the music player
Serial.println("Adafruit VS1053 Library 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"));
musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
Serial.println("SD OK!");
// list files
printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20,20);
/***** Two interrupt options! *******/
// This option uses timer0, this means timer1 & t2 are not required
// (so you can use 'em for Servos, etc) BUT millis() can lose time
// since we're hitchhiking on top of the millis() tracker
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
// This option uses a pin interrupt. No timers required! But DREQ
// must be on an interrupt pin. For Uno/Duemilanove/Diecimilla
// that's Digital #2 or #3
// See http://arduino.cc/en/Reference/attachInterrupt for other pins
// *** This method is preferred
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
Serial.println("Setup complete");
}
void loop() {
UltrasonicSensePerson(); //check ultrasonic sensor
}
//==========================================================
// FUNCTIONS
//==========================================================
void UltrasonicSensePerson() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int distance = readDistance(); // Current distance of any object facing the ultrasonic sensor
Serial.print("Ultrasonic: ");
Serial.print(distance); // Send ping, get distance in inches and print result (0 = outside set distance range)
Serial.println("in");
if (sensedPerson == LOW && distance < critical_distance_in)
{
Serial.println("Sensed someone!");
// digitalWrite(RELAY1, LOW); // Turn the light on
sensedPerson = HIGH;
}
else
{
Serial.println("not sensed");
// digitalWrite(RELAY1, HIGH); // Turn the light off
delay(50);
sensedPerson = LOW;
}
}//end UltrasonicSensePerson()
unsigned int readDistance()
{
// Read 7 values from the ultrasonic and get the median value ( median filter )
// Gets rid of noisy reading
unsigned int distance = sonar.convert_in(sonar.ping_median(7));
// The value 0 indicates that the ultrasonic sensor is reading nothing in front of it
// Set this distance to max distance so the light doesn't switch unnecessarily
if (distance == 0)
{
distance = MAX_DISTANCE;
}
return distance;
}//end readDistance
void playmp3(){
// Start playing a file, then we can do stuff while waiting for it to finish
if (! musicPlayer.startPlayingFile("/01.mp3")) {
Serial.println("Could not open file 01.mp3");
while (1);
}
Serial.println(F("Started playing"));
while (musicPlayer.playingMusic) {
// file is now playing in the 'background' so now's a good time
// to do something else like handling LEDs or buttons :)
Serial.print(".");
delay(1000);
}
Serial.println("Done playing music");
}
/// File listing helper
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();
}
}
Really appreciate it, this is to trigger some motorized skeletons with a song for a little haunted outdoor Halloween thing we are trying to do this year for this kids since trick or treating isnt the best idea this year.