I've built a model of an ancient temple (think Indiana Jones) that has a rolling door that opens or closes based on the direction of the joystick. I would like to add a sound effect of a stone door rolling but I'm having trouble incorporating this sound effect into my model when using the joystick (perhaps because I'm using a While statement. When I add the DoorRoll.wav to my joystick operation the door refuses to open and the SFX hang the program. Other SFX work when various buttons are pushed so I know it's a coding issue.
I'm using an Arduino Mega with the tmrpcm library. The sound effects are tested and work in other configurations but I cannot attach this sound effect to the action of the joystick with success. Any thoughts would be appreciated.
As I said, are other elements on the model but, to keep this post simple, I have whittled down the code to just include what's necessary for the door to operate but I'm also attaching the complete code for the other functions of the model (Buttons, LED's, LCD screen, etc.) if you want to see the full code.
#include <Stepper.h> //Stepper Library
#include <SD.h> //SD Card Library
#include <TMRpcm.h> //Speaker Library
String sketchName = "Temple Exhibit";
String sketchVersion = "3.00";
// 28BYJ48 with ULN2003 driver pins
#define STEPS 32 //default value 32
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
Stepper templeStepper(STEPS, IN1, IN3, IN2, IN4);
// Audio
TMRpcm tmrpcm; //creating an audio player object
//joystick pins
const int joyXPin = A0;
const int joyYPin = A1;
const int joyButtonPin = 48;
//stepper limit switches
const int limitSwitch0Pin = 20; // closed limit
const int limitSwitch1Pin = 21; // open limit
volatile int limitSwitch0State;
volatile int limitSwitch1State;
//other pins
const int tree1Pin = 37; //Model's Indicator LED
const int lavaPin = 38; //Trench fire LED
const int cavePin = 39; //Cave Torch Fire LED (x3)
int doorState; //Is door open or closed
int joyXVal;
int joyYVal;
int joyButtonVal;
int dt = 10; // delay time
int mdt = 100; // delay time-medium
int ldt = 1000; // delay time-long
void setup() {
Serial.begin(9600);
Serial.println(sketchName); //Report Sketch version
Serial.println(sketchVersion);
tmrpcm.speakerPin = 12; //The speaker is connected to 12 Pin. You can use any PWM Pin.
tmrpcm.setVolume(4); //to adjust the speaker volume; 4 = max for current speaker
pinMode(limitSwitch0Pin, INPUT_PULLUP); //home pin (door closed)
pinMode(limitSwitch1Pin, INPUT_PULLUP); //stop pin (door opened)
pinMode(joyXPin, INPUT);
pinMode(joyYPin, INPUT);
pinMode(joyButtonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(limitSwitch0Pin), stopMotor0, FALLING);
attachInterrupt(digitalPinToInterrupt(limitSwitch1Pin), stopMotor1, FALLING);;
// TEMPLE DOOR HOMING******************************************************
delay(dt);
lcd.clear(); //clear screen
lcd.setCursor(0, 0);
lcd.print("<<DOOR RESETTING");
lcd.setCursor(0, 1);
lcd.print(" Homing Protocol");
delay(dt);
limitSwitch0State = digitalRead(limitSwitch0Pin);
while (limitSwitch0State == HIGH)
{
templeStepper.setSpeed(250); // code to move one step towards the switch
templeStepper.step(1); //Set rotation direction to CW
limitSwitch0State = digitalRead(limitSwitch0Pin);
} //end while, motor is now at HOME position
doorState = 0; //door is now closed
delay(dt);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" DOOR RESET");
lcd.setCursor(0, 1);
lcd.print("Homing Complete ");
Serial.println("Homing Complete");
Serial.println("");
delay(ldt);
//Speaker Check************************************************************
if (!SD.begin(53)) //If the card is available.
{
lcd.clear(); //Set LCD to default screen
lcd.setCursor(0, 0);
lcd.print(" SD CARD READER ");
lcd.setCursor(0, 1);
lcd.print(" FAIL ");
Serial.println("SD fail"); //Write in the serial port "SD fail".
return;
}
tmrpcm.play("LampKey1.wav"); //Play startup music cue
delay(mdt);
} //End Setup************************************************************
void loop() {
joyXVal = analogRead(joyXPin); // read and store X value
joyYVal = analogRead(joyYPin); // read and store Y value
joyButtonVal = digitalRead(joyButtonPin); // read and store button value
if ((joyXVal > 500) && (joyXVal < 525)) //If joystick is neutral turn off stepper motor
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(dt);
}
else
{
while ((joyXVal >= 525) && (doorState == 0)) //While doorState is closed and joystick is pushed RIGHT
{
templeStepper.setSpeed(900);
templeStepper.step(-1); //Set rotation direction to CCW
joyXVal = analogRead(joyXPin);
}
}
while ((joyXVal <= 495) && (doorState == 1)) //While joystick is pushed LEFT
{
templeStepper.setSpeed(900);
templeStepper.step(1); //Set rotation direction to CW
joyXVal = analogRead(joyXPin);
}
delay(dt);
joyXVal = analogRead(joyXPin);
joyYVal = analogRead(joyYPin);
joyButtonVal = digitalRead(joyButtonPin);
Serial.print("Joystick x,y = (");
Serial.print(joyXVal);
Serial.print(" , ");
Serial.print(joyYVal);
Serial.println(")");
} //End Void Loop*********************************************************
void stopMotor0() {
doorState = 0; //door is closed
Serial.println("Door is closed");
}
void stopMotor1() {
doorState = 1; //door is open
Serial.println("Door is open");
}
Temple_Exhibit_3.00.ino (17.5 KB)

