I cannot get my audio file to play when I want it to.

Okay, so I am making a jewelry box for my niece that will spin a necklace holder with a stepper motor, play audio file and turn on LED lights. I am using a MEGA 2560. Flow is this:

Top door open and bottom door closed: LED on, stepper spin, audio plays.

Top door closed and bottom door open: LED on and audio plays, no stepper motor.

Both doors closed: Everything off or off after delay.

Both doors open: LED on and music plays, no stepper motor.

My issue is that with the top door open, the audio plays as it should. No issues, but if I only open the bottom door no audio plays. If I open the top door and then the bottom door, audio will play. If I open in reverse, no audio will play. I have tried adding an if statement by itself with an "or" in the loop section to check for either door open and I cannot put it inthe setup section because that just continuously plays. I currently have it to where music plays with the top door open. The extremely strange portion to me is that if I put the tmrpcm.play in the first else statement, the audio will play after opening and closing the bottom door.

So what I would like is for audio to play as long as either door is open. PLEASE HELP! WHAT AM I DOING WRONG?!?!?!?!?!

//CODE//

#include <SD.h>
#include <TMRpcm.h>
TMRpcm tmrpcm;

#include <Stepper.h>
const int stepsPerRevolution = 2048;
const int doorLatch=2; //Ring door
const int doorLatch2=6; //Necklace door
const int relayPower=13; //Turn relay on

Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
Serial.begin(9600);
Serial.print("\nInitializing SD card...");

if (!SD.begin(53)) { // set this to your card type and cs pin
Serial.println("failed!");
return;
}
Serial.println("done.");

tmrpcm.speakerPin = 46;

myStepper.setSpeed(5);
pinMode(doorLatch,INPUT);
pinMode(doorLatch2,INPUT);
pinMode(relayPower,OUTPUT);
digitalWrite(doorLatch,HIGH);
digitalWrite(doorLatch2,HIGH);

}

void loop() {

if ((digitalRead(doorLatch)==HIGH)&&(digitalRead(doorLatch2)==HIGH)){ //ring door and necklace door is closed
digitalWrite(relayPower,LOW);

}
else{

digitalWrite(relayPower,HIGH); //ring door is open, lights on, stepper motor spinning, music playing

}

if ((digitalRead(doorLatch2)==HIGH)&&(digitalRead(doorLatch)==LOW)){ //ring door open and necklace door closed

digitalWrite(relayPower,HIGH);

myStepper.step(stepsPerRevolution);
delay(500);
myStepper.step(-stepsPerRevolution);
delay(500);

}
else{
tmrpcm.play("music.wav");
}
}

Jewelrybox.ino (1.46 KB)

The code talks of ring and necklace doors, your description talks of top and bottom doors,
no idea which is which, or whether open means that pin is HIGH or LOW?

The last two digital writes in the setup set both doors high because i am using normally closed magnetic switches. The // ring door and the // necklace door tells which door latch is which and the the //ring door and necklace door says both doors closed if digital read is high and high. I used notes throughout.

What Mark said: which is 'top' and which is 'bottom?

Is it doorlatch or doorlatch2? Is it ring or necklace?

I am not sure what I am doing wrong here. The second const int is ""const int doorLatch = 2 // Ring door"". The third const int is ""const int doorLatch2 = 6 // Necklace door"". It does not matter which one is which. If either door is open I want music to play and loop. The top door is the ring door. The bottom door is the necklace door.

ok

this line, are the comments true?

digitalWrite(relayPower,HIGH);           //ring door is open, lights on, stepper motor spinning, music playing

I dont see how digitalWrite(relayPower,HIGH); plays the music. Does pin 13 call the music somehow? Is there an amp or something that turns on with your relay?

So, given there are 4 states:

State 0 - all closed
State 1 - Top Open only
State 2 - Bottom Open only
State 3 - Both open

The way I read the code is thus:

IF State 0, power down the relay
ELSE (IF State 1,2,3) power on the relay

IF state 2, spin the stepper
ELSE (State 0, 1, or 3) Play the music

Which means:
State 0 - Relay power is down, Music file is being played
State 1 - Relay Powered up, Music file is being played
State 2 - Relay Powered up, Stepper is going
State 3 - Relay Powered up, Music file is being played

This is the observed behavior IF the relay being OFF (State 0) stops the music from being heard by you somehow- maybe cuz its powering the speaker?

Anyway, It sounds convouted. Maybe rewrite with 4 IF statements, one for each State, and just define the actions for each case. Not as elegant, but there are only 4 states.