Pages: [1] 2   Go Down
Author Topic: Too many if statements?? Please help  (Read 1565 times)
0 Members and 1 Guest are viewing this topic.
Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am writing a program that reads multiple sensors on the Analog input pins and because I don't really understand how to initiate the switch/case parameters I am using if statements.  The sketch works fine when only using 2 IF statements but when I get to the third nothing works.  I didn't think there was a limit to the number of IF statements but am I wrong?  Each IF statement is exactly the same so it's not syntax errors or anything like that.  I am at my wits end trying to get this to work.  Please help with any input possible.  In the mean time I will keep working on it.  Thank you in advance for any input.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There must be something wrong, post your code we can't tell you anything without it (and use the # button to format it properly).

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

GCC can handle 1000 if statements in 1 block... (i guess)  smiley-wink

can u show us ur sketch?
do u use "if else if"?
i mean: maybe the 3rd if reverts the 2nd? then the 2nd reverts the 3rd, so that u cant c what u expect...
Logged

-Arne

Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-sad
    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);
}
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

CODE tags please.

Highlight the code text and click the # button at the top of the editing area.

At present most of the loop() function is commented out, are they the if statements you are talking about?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about that.  I have deleted unneeded segments and uncommented the IF statements.  Each statement works by itself and in any variation of pairs but all 5 will not work together.


Code:
#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++;
  }
}

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

Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't use ELSE IF because each IF statement is for a different sensor.  The sensors are Photo Diodes with a laser pointed at them.  The laser beam is walked through and then sends around 5VDC to the analog input pin.  This is accomplished by putting the photo diode in a voltage divider feeding a BJT.
Logged

Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 510
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am writing a program that reads multiple sensors on the Analog input pins and because I don't really understand how to initiate the switch/case parameters I am using if statements.  The sketch works fine when only using 2 IF statements but when I get to the third nothing works.  I didn't think there was a limit to the number of IF statements but am I wrong?  Each IF statement is exactly the same so it's not syntax errors or anything like that.  I am at my wits end trying to get this to work.  Please help with any input possible.  In the mean time I will keep working on it.  Thank you in advance for any input.

Auto Format is your friend!  Or it should be.

Are we to assume that you're talking about the IF statements in loop() or are you talking about playfile(char *name)?  And could you be anymore general than "nothing works"?

Your problem isn't IF statements or not using CASE statements.  Your problem is one of your code having some kind of bug.  Very hard for anyone to help when you're being so general and not giving specifics.

A good way to diagnose your problem is to put statements like the following inside your IF statements (as the first command).

Code:
Serial.println("1");

With each command being a different number, letter, or something to describe each IF statement.  Then, when you run your sketch you open the Serial Monitor so you can track where the code is and when it's failing.

In any case, it's not the fault of Arduino and using too many IF statements or needing to use CASE.  The problem is your code is wrong.  And for us to help specifically you should give DETAILS.

Tim
Logged

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

teckel I don't know how much more detail I can give.  As I said.  Each individual IF statement within the Loop() works perfectly fine and in any pair of two they work great but if I try to use three or more, nothing happens with ANY of the sensors.  Nothing will trigger an output.  I will try your advice and include a print statement at the beginning of each IF within the loop() and see what happens.
Logged

Earth (I think)
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried taking out as much code as possible to make the problem still happen? For example, what happens if you replace the playfile() code with a single Serial.print statement? If the problem still exists, take some code out from somewhere else... this way, you have a lot less code, but you still have the problem which is then easier to track down (you just removed half the haystack).
Logged


USA
Offline Offline
God Member
*****
Karma: 14
Posts: 644
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

teckel I don't know how much more detail I can give.  As I said.  Each individual IF statement within the Loop() works perfectly fine and in any pair of two they work great but if I try to use three or more, nothing happens with ANY of the sensors.  Nothing will trigger an output.  I will try your advice and include a print statement at the beginning of each IF within the loop() and see what happens.

Here's some more detail you could provide...  What exactly are the sensors you are using and is it physically possible for more than one to be triggered at the same time? 
Logged

Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 510
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

teckel I don't know how much more detail I can give.  As I said.  Each individual IF statement within the Loop() works perfectly fine and in any pair of two they work great but if I try to use three or more, nothing happens with ANY of the sensors.  Nothing will trigger an output.  I will try your advice and include a print statement at the beginning of each IF within the loop() and see what happens.

First, you never said you were speaking of the IF statements in loop().  Up until now, you only mentioned the IF statements, which also exist outside loop(), which is why I asked.  Please review what you wrote, you may have *thought* you said inside loop(), but you did not.

Secondly, all you've said is that "nothing works".  Do you mean that you start the sketch and nothing AT ALL happens no matter what?  Or, do you mean that the first thing works and the second doesn't?  Or, maybe some things work, but it doesn't work prefect?  Maybe try to give at least a sentence description of what happens from the time you start your sketch.  Or, we are all to assume what you're experiencing, and maybe assuming different things or more importantly different from what is actually happening.

Keep in mind that we're trying to help.  So, you should try to be as descriptive as possible.  It's one thing to take your car in for service and say it doesn't work as they can see your car to diagnose it.  But, we don't have that luxury here as we don't have your hardware in front of us to diagnose.  So, it's very important to give details and be descriptive.

Tim
Logged

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, my apologies for seeming a bit rude in earlier posts.  This has been turning out to be a frustrating project.
Second, I am posting some updated code below.
I am using five sensors that consist of a voltage divider with a photo resistor feeding a BJT and a red laser pointing on the photo resistor so that when someone walks through the beam the BJT turns on and sends 5VDC to the Arduino Uno digital input pin.  I have the Wave Shield attached to the Arduino Uno and have various short wave files on the SD card.  I also have various arrays of LED's on two output pins and a small DC motor attached to another output pin.  I am using IF and ELSE IF statements now to read the input pins searching for one to go high from the 5VDC from the sensor.  Each IF/ELSE IF statement has a different LED blink and a different audio file.  When commenting out sensor 2-5, sensors 1 and 2 trigger outputs as they should but when I include all the sensors in the LOOP(), none of the sensors will trigger an output. 

I have now discovered that if I only use three different audio files between the 5 conditional statements all 5 sensors trigger outputs.  If I try only 4 audio files, none of the sensors trigger outputs. 

I have tried inserting PRINT statements to print to the serial monitor for some debugging to see where it is getting stuck but I can't get that to work.

I haven't seen anything that limits the amount of audio files to be used.  Even the WAVEHC example code for the six buttons there are six different audio files to be played.

Any input will be greatly appreciated.

In the mean time I will rewrite it to only use three audio files as this is my final project to graduate and is due this friday.


Code:
#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


void setup()
{
  Serial.begin(9600);  //Setup Serial

  pinMode(14, INPUT);      //Assigns analog pins 0-4 as sensor inputs
  pinMode(15, INPUT);
  pinMode(16, INPUT);
  pinMode(17, INPUT);
  pinMode(18, INPUT);
  pinMode(19, 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()
{
  int 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++;
  }
  delay(1);


/*void strobe1()
{
  int 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()
{
 int blinky = 0;
  while(blinky < 16)
  {
    digitalWrite(8,HIGH);
    digitalWrite(9,HIGH);
    delay(80);
    digitalWrite(8,LOW);
    digitalWrite(9,LOW);
    delay(80);
    blinky++;
  }
}

void strobe4()
{
 int blinky = 0;
  while(blinky < 80)
  {
    digitalWrite(9,HIGH);
    delay(50);
    digitalWrite(9,LOW);
    delay(50);
    blinky++;
  }
  delay(1);
}

void strobe5()
{
 int blinky = 0;
  while(blinky < 40)
  {
    digitalWrite(9,HIGH);
    delay(40);
    digitalWrite(9,LOW);
    delay(30);
    blinky++;
  }
  delay(1);
}

// 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()
{

 if(digitalRead(14) == HIGH)
  {
   // blinky=0;
     
    playfile("cat.WAV");
 
    while (wave.isplaying)
    {
      //strobe1();    //try blinking light without function, you are already in a while loop
      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++;
    }
     //  putstring_nl("finish sensor 1");
  }   


else if(digitalRead(15)==HIGH)
  {
    playfile("help.WAV");
    while(wave.isplaying)
    {     
      //strobe2();
    }
   
  }


else if(digitalRead(16)==HIGH)
  {
   
  //  digitalWrite(7,HIGH);
    playfile("ghost.WAV");
    while(wave.isplaying)
    {
      //ghost();
    }
 //   digitalWrite(7,LOW); 
  }   


else if(digitalRead(17)==HIGH)
  {
   
    playfile("help.WAV");
    while(wave.isplaying)
    {
      //strobe4();
    }
   
  }

 else if(digitalRead(19)==HIGH)
  {
    playfile("ghost.WAV");
    while(wave.isplaying)
    {
      //strobe5(); 
    }   
  }
}
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have tried inserting PRINT statements to print to the serial monitor for some debugging to see where it is getting stuck but I can't get that to work.
You need to debug one thing at a time, forget the WAV files and blinking LEDs. Replace that code with a simple Serial.print() like

Code:
if(digitalRead(15)==HIGH) {
   Serial.println ("help");
}

Then when that works start gradually putting stuff back in. When it breaks the last thing you added is (usually) the problem.
______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 510
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have tried inserting PRINT statements to print to the serial monitor for some debugging to see where it is getting stuck but I can't get that to work.
You need to debug one thing at a time, forget the WAV files and blinking LEDs. Replace that code with a simple Serial.print() like

Code:
if(digitalRead(15)==HIGH) {
   Serial.println ("help");
}

Then when that works start gradually putting stuff back in. When it breaks the last thing you added is (usually) the problem.
______
Rob

+1
Logged

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Pages: [1] 2   Go Up
Jump to: