Okay, so I am making a jewelry box for my niece that will spin a necklace holder with a stepper motor, play an audio file and turn on LED lights. I am using an Arduino MEGA 2560.
The flow is this:
- ringDoor open and necklaceDoor closed: LED on, stepper spin, audio plays.
- ringDoor closed and necklaceDoor 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 doorLatch open, the audio plays as it should. No issues, but if I only open doorLatch2, no audio plays. If I open doorLatch and then doorLatch2, 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 in the setup section because that just continuously plays with latches open or closed. I currently have it to where music plays with doorLatch 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 doorLatch2.
So what I would like is for audio to play and loop 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...");
// set this to your card type and cs pin
if (!SD.begin(53)) {
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 {
//ring door is open, lights on, stepper motor spinning, music playing
digitalWrite(relayPower,HIGH);
}
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");
}
}
NEW CODE, NEW PROBLEM, but closerUpdated names and notes for easier following*** New problem is if I open the necklaceDoor first, nothing happens. I have to open the ringDoor first. That turns everything on. Then with the necklaceDoor open, the stepper motor will turn off and the lights and music will continue to play. But I want to be able to open the necklaceDoor first and play the music with the lights. I do not want it to matter which one is opened first. I also realized that if I can get an or statement to work within an and statement I could do away with the fourth if statement completely. Both ringDoor open and necklaceDoor open, as well as ringDoor closed and necklaceDoor open have the same outcome. Now if I switch the door states in the third if statement, I get necklaceDoor only plays audio after closed(original problem) but if both doors are open and then ringDoor is closed, the music will shut off after the stepper timer is done and then start again after necklaceDoor is closed?!?!?!?
#include <SD.h>
#include <TMRpcm.h>
#include <Stepper.h>
TMRpcm tmrpcm;
const int stepsPerRevolution = 2048; //Number of pulses per revolution
const int ringDoor=2; //Ring door
const int necklaceDoor=7; //Necklace door
const int relayPower=22; //Turn relay on
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
Serial.begin(9600); //Start connection with computer
if (!SD.begin(53)) //Chip pin on card
{
Serial.println("failed!"); //SD card did not read
return;
}
tmrpcm.speakerPin = 46; //Speaker output pin
myStepper.setSpeed(5); //Stepper speed
pinMode(ringDoor,INPUT); //Check state of ring door magnetic switch, NO
pinMode(necklaceDoor,INPUT); //Check state of necklace door magnetic switch, NO
pinMode(relayPower,OUTPUT); //Relay enable for 12V LED
digitalWrite(ringDoor,HIGH); //Make door closed read high on start up
digitalWrite(necklaceDoor,HIGH); //Make door closed read high on start up
digitalWrite(relayPower,LOW); //Make relay off on start up
}
void loop() {
if ((digitalRead(ringDoor)==HIGH)&&(digitalRead(necklaceDoor)==HIGH))
{ //ring door and necklace door is closed, everything is off
digitalWrite(relayPower,LOW);
}
if ((digitalRead(ringDoor)==LOW)&&(digitalRead(necklaceDoor)==HIGH))
{ //ring door is open and necklace door is closed, lights on, stepper motor spinning, music playing
digitalWrite(relayPower,HIGH);
tmrpcm.play("music.wav");
myStepper.step(stepsPerRevolution);
delay(500);
myStepper.step(-stepsPerRevolution);
delay(500);
}
if ((digitalRead(ringDoor)==HIGH)&&(digitalRead(necklaceDoor)==LOW))
{ //ring door closed and necklace door open, lights on, stepper motor off, music playing
digitalWrite(relayPower,HIGH);
tmrpcm.play("music.wav");
}
if ((digitalRead(ringDoor)==LOW)&&(digitalRead(necklaceDoor)==LOW))
{ //ring door and necklace door open, lights on, stepper motor off, music playing
digitalWrite(relayPower,HIGH);
tmrpcm.play("music.wav");
}
}
Please and thank you to any and all who help. It is greatly appreciated
ringDoor
instead ofdoorLatch
, similar for the other door .... draw a chart of behavior ... it iseither door -> led & sound
...ring door -> motor