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)