1

enter image description hereProblem 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)==LOW)&&(digitalRead(necklaceDoor)==HIGH)) 
  {                                                                                 //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. Yes, I asked the the original question and the answer I received not only did not work, somehow neither latch worked anymore.

6
  • 2
    The code you've presented should not even compile. Some of what looks like code is in comments, line endings are apparently missing, and lines extend far out across the page. Is this what you tried to run or did it get mangled when you posted it? If it's the latter, please repost the code you actually compiled. – JRobert Aug 14 '20 at 12:42
  • I don't know what happened to your code when you copy-pasted it, but please reformat it entirely and paste it in here again. It's entirely unreadable, and definitely should not compile as it is. Once you paste it, highlight it all and click the "Code Sample" button in the editor to get the editor to treat it as a code block (as it is now already). – Gabriel Staples Aug 14 '20 at 16:05
  • Updated. Thank you, I had not realized it messed up. – Vincent Ledbetter Aug 14 '20 at 16:12
  • Did you intend for the two middle if statements to have the exact same condition? Or should one have been for the other door? – Delta_G Aug 14 '20 at 16:27
  • all you need is two if statements to cover the open doors ... if either door is open then lights on and play music .... if ring door is open then run motor ... run them one after the other – jsotola Aug 14 '20 at 22:54
1

First of all, you have to find out how your magnetic switches work. Are they opened or closed if you attach a magnet to them? Do they behave the same or is one closed and the other opened if you attach a magnet? Your code above is written for active LOW switches i.e. if the respective door is closed the switch is opened and therefore the logic level of the pin is HIGH. The level is LOW if the door is opened (activated; hence active LOW).

In order your code had a chance to work, the switches must be connect the right way: on one side connect them to GND and on the other side to the respective Arduino pin. If this is not the case, the code must be changed or the switches must be conneted the right way. You did not show us your schematic, so I can not check it for you.

Here is some code to try: But first find out if the switches you use are LOW or HIGH active. I also have no access to an IDE at the moment, so I can not try out the code. Take it as a hint on how to do it.

And one tip from a guy that does the job for 30 years now: Don't comment variables if their names say it all:

//  this sort of comments makes the code unreadable --V
const int ringDoor=2;                          //Ring door 

I know it is a common behavior in the Arduino community, but don't write comments after a statement. In your code above I did realize that there are comment for the if statements only when I copied the code in my answer. Comments are only usefull if they say something new and can be read without problems.

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

const int stepsPerRevolution = 2048;
const int ringDoor = 2;             
const int necklaceDoor = 7;         
const int relayPower = 22;

Stepper myStepper = Stepper( stepsPerRevolution, 8, 10, 9, 11 );
    
void setup() 
{
  Serial.begin( 9600 );          
  if ( ! SD.begin( 53 ) )           
  {
    Serial.println( "failed!" ); 
    return;
  }
  
  tmrpcm.speakerPin = 46;      
  myStepper.setSpeed( 5 );       
  pinMode( ringDoor, INPUT );     
  pinMode( necklaceDoor, INPUT ); 
  pinMode( relayPower, OUTPUT );  

  digitalWrite( ringDoor, HIGH ); 
  digitalWrite( necklaceDoor, HIGH );

  digitalWrite( relayPower, LOW );    
}

void loop() {

  // the following code works if both switches are LOW active 
  // (i.e. LOW if the door is open)
  // if the "ringDoor-switch is HIGH active change 
  // digitalRead( ringDoor ) == LOW to 
  // digitalRead( ringDoor ) == HIGH
  // if the "necklaceDoor-switch is HIGH active change 
  // digitalRead( necklaceDoor ) == LOW to 
  // digitalRead( necklaceDoor ) == HIGH
  // and 
  // digitalRead( necklaceDoor ) == HIGH to 
  // digitalRead( necklaceDoor ) == LOW

  if ( ( digitalRead( ringDoor )     == LOW ) ||
       ( digitalRead( necklaceDoor ) == LOW ) ) 
  {                                                                                        
    // one of the doors is open
    digitalWrite( relayPower, HIGH );
    tmrpcm.play( "music.wav" );
    if ( digitalRead( necklaceDoor ) == HIGH )
    {
      // only the ring door is opend
      myStepper.step( stepsPerRevolution );
      delay( 500 );
      myStepper.step( -stepsPerRevolution );
      delay( 500 );
    }
  }
  else
  {
    digitalWrite( relayPower, LOW );
  }
}
4
  • So your code works almost perfect. Thank you very much! I do apologize for the notes. The arduino page forum asked for them as did my first post here. The only issue I am having now is that if I open necklace door first no music will play until I close that switch? Then music will play? I am trying to figure out how to add picture of my schematic. Be gentle with me, I don't have CAD so I drew it in MultiSim. ( A program I still have from college). I just edited original post with the schematic. – Vincent Ledbetter Aug 16 '20 at 15:09
  • @VincentLedbetter You're welcome. There is no reason to apollogize. The schematic is fine. The switches connect GND to the sensor pins so the first part of the program (e.g. activating the pull up resistors) seems to be correct. Theres one thing I can not check, and that's: Are the switches close if the doors are close or are they open? See the comment in the code above. If you can not find out, make the HIGH/LOW changes I suggest in the comment and find out if that works better. There could also be an issue with the needed power. The Mega might not be able to drive all the current you need. – Peter Paul Kiefer Aug 17 '20 at 7:55
  • So the 12v Power supply has a splitter. One side goes to the arduino and I am only running the sd card reader, the relay enable and the audio pin. Then the other side of the 12v power supply is ran through the relay. The LED's plug in before the voltage regulator and the stepper motor is powered after the voltage regulator. So my high amperage draw is coming straight from the power source and not through the board. – Vincent Ledbetter Aug 17 '20 at 13:38
  • @VincentLedbetter The 3.3V Pin has a maximum current of 50mA. How much current draws the SD Card Module? The IO Pins can deliver 40mA. How much draws the Buzzer? The 5V can deliver the minimum of the possible Power Supply current and 500mA (controlled by a thermofuse). From that you have to subtract the current that's drawn from the other pins: 3.3V, IO Pins, the pullups on the switches draw current ... It was just an idea if nothing else makes sense. The other idea, that the switches might not behave as expected, is more important to me. – Peter Paul Kiefer Aug 19 '20 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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