sound to play when button activates servo.

Hello guys:

OK, I’m working on a model Enterprise from Star Trek TV show. I have worked out how I am going to get my servo to open and close the shuttlebay. Now, I want to add an MP3 to play while it is opening and another one to play while it is closing. I am a newbie so please keep everything simple…

#include <Servo.h>

// constant variables used to set servo angles, in degrees
const int straight = 100;
const int divergent = 140;

// constant variables holding the ids of the pins we are using
const int divergent_led = 6;
const int straight_led = 7;
const int buttonpin = 8;
const int servopin = 9;

// servo movement step delay, in milliseconds
const int step_delay = 100;

// create a servo object
Servo myservo;

// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous

void setup()
{
// set the mode for the digital pins in use
pinMode(buttonpin, INPUT);
pinMode(straight_led, OUTPUT);
pinMode(divergent_led, OUTPUT);

// setup the servo
myservo.attach(servopin); // attach to the servo on pin 9
myservo.write(pos); // set the initial servo position
int pos = 0;
// set initial led states
digitalWrite(straight_led, HIGH);
digitalWrite(divergent_led, LOW);
}

void loop()
{
// start each iteration of the loop by reading the button
// if the button is pressed (reads HIGH), move the servo
int button_state = digitalRead(buttonpin);
if(button_state == HIGH){
// turn off the lit led
if(pos == straight){
digitalWrite(straight_led, LOW);
} else {
digitalWrite(divergent_led, LOW);
}
old_pos = pos; // save the current position

// Toggle the position to the opposite value
pos = pos == straight ? divergent: straight;

// Move the servo to its new position
if(old_pos < pos){ // if the new angle is higher
// increment the servo position from oldpos to pos
for(int i = old_pos + 1; i <= pos; i++){
myservo.write(i); // write the next position to the servo
delay(step_delay); // wait
}
} else { // otherwise the new angle is equal or lower
// decrement the servo position from oldpos to pos
for(int i = old_pos - 1; i >= pos; i–){
myservo.write(i); // write the next position to the servo
delay(step_delay); // wait
}
}
// turn on the appropriate LED.
if(pos == straight){
digitalWrite(straight_led, HIGH);
} else {
digitalWrite(divergent_led, HIGH);
}
}
}// end of loop

I have the VS1053 MP3 player to go with the UNO. Please please help!

Thanks!

Please use code tags </> for presenting code.

Have you tried to find a library and/or sample code for the MP3 player module?

I have found a bunch of libraries that I have downloaded. So far I have these: Adafruit VS1053 Library SFEMP3 SHIELD SD SPI

Are there any others I might need? Also, here is my mp3 shield: http://www.geeetech.com/wiki/index.php/Arduino_MP3_shield_board_with_TF_card

I am using an UNO board.

Thank you so much!

Go on and use one of the libraries, and find out whether it will do what you want.

It doesn't. no sound

Have you tried the MP3-TF.zip, mentioned in the product page?

Yes. It does not work. I have basically given up on that board. I did a little more research and have purchased the https://www.sparkfun.com/products/12660 board. It looks like it will actually work.

It looks like if you ground one of the pins temporarily (via a momentary "on" push button?) I might be able to work it and the servo at the same time.

I hope. Otherwise I will have to give up on the project.