1

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:

  1. ringDoor open and necklaceDoor closed: LED on, stepper spin, audio plays.
  2. ringDoor closed and necklaceDoor open: LED on and audio plays, no stepper motor.
  3. Both doors closed: Everything off or off after delay.
  4. 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

1
  • 1
    couple of things ... use descriptive variable names ... use ringDoor instead of doorLatch, similar for the other door .... draw a chart of behavior ... it is either door -> led & sound ... ring door -> motor
    – jsotola
    Commented Aug 11, 2020 at 15:59

1 Answer 1

1

Your code doesn't match your desired output. You say you want to:

doorLatch open and doorLatch2 closed: LED on, stepper spin, audio plays.
doorLatch closed and doorLatch2: LED on and audio plays, no stepper motor.
Both latches closed: Everything off or off after delay.
Both latches open: LED on and music plays, no stepper motor.

Note that both latches open and and doorLatch closed DoorLatch2 open result in the same state: LED on and audio plays, no stepper motor.

Your code says

if doorLatch & doorLatch2 are both closed (high)

    relayPower = low (off)

else 

    relayPower = HIGH (on). This in ALL cases other than both latches HIGH

Next, a second if statement:

if doorLatch2 == HIGH && doorLatch == LOW
     relayPower = HIGH (on)
     step motor back and forth
else 
     play sound

So you play the sound if doorlatch2 is low or doorLatch is high

You turn off the relay if doorLatch & doorLatch2 are both closed (high)

You turn on the relay if doorLatch is low or doorlatch2 is low.

void loop() {
   int latchState digitalRead(doorLatch); 
   int latch2state = digitalRead(doorLatch2);
   if (latchState = LOW && latch2Sate == HIGH) {        //0 - 1
      //turn on led
      //spin stepper
      //play audio
   } else if (latchState = HIGH && latch2Sate == LOW) { //1 - 0
      //Turn LED on
      //play audio
      //turn off stepper
   } else if (latchState = HIGH && latch2Sate == HIGH) {//1 - 1
     //turn everything off (after delay?)
   } else if (latchState = LOW && latch2Sate == LOW {   //0 - 0
     //LED on
     //play music
     //turn off stepper
   } 
}
2
  • I tried that and my relay pin stayed energized the entire time. I have redone a little bit and will edit original post Commented Aug 12, 2020 at 16:14
  • I solved the same problem for a friend a long time ago. Maybe that's the same case: there were a interrupt conflict between the stepper and the player. The way to solve is to reinit the player each time you want to play, so it wil re-install it's interrupt..
    – Peter
    Commented Jun 9, 2021 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.