Here is the coding:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Bounce.h>
#include "mngs1.h"
#include "mngs2.h"
#include "mngs3.h"
AudioPlayMemory gunSound; // creates the Audio memory object
AudioOutputAnalog dac; // play to on-chip DAC
AudioConnection c1(gunSound, 0, dac, 0);
AudioControlSGTL5000 audioShield;
int inPin = 2;
const int ledPin = 13;
const int motorPin = 12;
Bounce Button = Bounce(inPin, 20);
uint8_t STATE; // variable that tells us where we are in the sequence
uint8_t playnow; // variable that tells us whether we are playing
// the sequence of states (another way of writing {0,1,2}):
enum Sequence
{
SOUND1,
SOUND2,
SOUND3
};
void setup()
{
Serial.begin(9600);
pinMode(inPin, INPUT); //Sets the digital pin as input:
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(motorPin, OUTPUT); // initialize the motor pin as an output:
AudioMemory(10); //Allocates memory space for audio file to be played
// turn on the output
audioShield.enable();
audioShield.volume(0.5);
// initial state:
STATE = SOUND1;
playnow = false;
}
void loop()
{
Button.update(); // check button
if (Button.risingEdge()) playnow = true; // did someone push it? if so start playing.
if (playnow) playgun(); // do we play?
};
void playgun()
{
// which stage are we at?
switch (STATE)
{
case SOUND1:
{
Serial.println("1st Sound");
// play file #1 once, start up motor and move on to stage 2:
digitalWrite(motorPin, HIGH); // turn on motor:
gunSound.play(mngs1);
delay(662);
STATE = SOUND2;
break;
}
case SOUND2:
{
Serial.println("2nd Sound");
// button still held down?
digitalWrite(ledPin, HIGH); // turn LED on:
if (digitalRead(inPin))
{
// if so, (re)play sound 2, but only if nothing is being played already:
if (!gunSound.isPlaying()) gunSound.play(mngs2);
delay(58);
}
// if button was released, move on:
else STATE = SOUND3;
digitalWrite(ledPin, LOW); // turn LED off:
break;
}
case SOUND3:
{
Serial.println("3rd Sound");
// play sound # 3
gunSound.play(mngs3);
delay(600);
// turn off motor
digitalWrite(motorPin, LOW);
// and reset state:
STATE = SOUND1;
playnow = false;
break;
}
}
};