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.

1 Answers1

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 ); } }

Peter Paul Kiefer
  • 1,893
  • 9
  • 11