Here's the code. Each individual IF statement works but they won't work together.
#include <SD.h>
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
int Sensor1 = 0; //Variable for first sensor
int Sensor2 = 0; //Variable for second sensor
int Sensor3 = 0; //Variable for third sensor
int Sensor4 = 0; // for fourth sensor
int Sensor5 = 0; //Variable for fifth sensor
int MotorOn = 0; //Variable turning motor on
int MotorOff = 0; //Variable turning motor off
int Lights1 = 0; //Variable turning on LED cluster 1
int Lights2 = 0; //Variable turning on LED cluster 2
int blinky = 0;
void setup()
{
Serial.begin(9600); //Setup Serial
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);
pinMode(A0, INPUT); //Assigns analog pins 0-4 as sensor inputs
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(6, OUTPUT); //Assigns digital pin 6 as output
pinMode(7, OUTPUT); //Assigns analog pin 7 as output for motor
pinMode(8, OUTPUT); //Assigns digital pin 8 as output for LED cluster 1
pinMode(9, OUTPUT); //Assigns digital pin 9 as output for LED cluster 2
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
// while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
// card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one ![]()
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
// while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
// while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
void sdErrorCheck(void)
{
if (!card.errorCode())
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
// while(1);
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
}
// ok time to play! start playback
wave.play();
}
void ghost()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(8,HIGH);
delay(50); //set proper delay here for ghost to go to the end
digitalWrite(8,LOW);
delay(50);
blinky++;
}
}
void strobe1()
{
blinky = 0;
while(blinky<12)
{
digitalWrite(8,HIGH); //Blink LED cluster 1 and 2
digitalWrite(9,HIGH); //for first half of cat scream
delay(40);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(40);
blinky++;
}
}
void strobe2()
{
blinky = 0;
while(blinky < 16)
{
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(80);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
delay(80);
blinky++;
}
}
void strobe4()
{
blinky = 0;
while(blinky < 80)
{
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
delay(50);
blinky++;
}
}
void strobe5()
{
blinky = 0;
while(blinky < 40)
{
digitalWrite(9,HIGH);
delay(40);
digitalWrite(9,LOW);
delay(30);
blinky++;
}
}
// Plays a full file from beginning to end with no pause.
//void playcomplete(char *name) {
// call our helper to find and play this name
// playfile(name);
// while (wave.isplaying) {
// do nothing while its playing
// }
// now its done playing
//}
void loop()
{
int Sensor1 = analogRead(A0); //Assigns values to the sensor readings
int Sensor2 = analogRead(A1);
int Sensor3 = analogRead(A2);
int Sensor4 = analogRead(A3);
int Sensor5 = analogRead(A4);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(7,LOW);
/* if(Sensor1 > 300)
{
playfile("cat.WAV");
while (wave.isplaying)
{
strobe1();
}
}
if(Sensor2 > 300)
{
playfile("suspense.WAV");
while(wave.isplaying)
{
strobe2();
}
}
*/
if(Sensor3 > 300)
{
digitalWrite(7,HIGH);
playfile("ghost.WAV");
while(wave.isplaying)
{
ghost();
}
digitalWrite(7,LOW);
}
/* if(Sensor4 > 300)
{
playfile("wlaugh2.WAV");
while(wave.isplaying)
{
strobe4();
}
}
if(Sensor5 > 300)
{
playfile("laugh3.WAV");
while(wave.isplaying)
{
strobe5();
}
}
*/ delay(10);
}