What's wrong with these if statements?

Hi,

I am fairly new to Arduino and so I apologize in advance for the newbie questions :cold_sweat:

EDIT: I figured out a little bit more of the code but am stuck somewhere else…
Right now I have the Lady Ada wav shield (Audio Shield for Arduino) and I have three arrays of wav files. Each array tells a different story, and each wav file is a sentence or two of that story.

I also have an accelerometer. What I want is the arduino to be playing the three sets of wav files, one file after another, one set after another. However, the arduino will be monitoring the accelerometer’s acceleration, and when it surpasses some significant threshold (i.e. when the accelerometer is shaken or moved), it will stop whatever wav it is currently playing and begin playing the first wav of the next set.

Here’s what I have right now, it’s not playing anything. I know there is something wrong with the if statements in the loop but I don’t know what it is:

#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

//Now values
int x1=0;
int y1=0;
int z1=0;

unsigned long now = 0;
unsigned long lastTime = 0;
double timeChange = 0;
long changeNow = 0;
long changeLast = 0;
long change = 0;
int accel = 0;
int threshold = 100;
int storyCounter;

int pinx = A1;
int piny = A2;
int pinz = A3;

char* story1[]={
  "00.wav", "01.wav", "02.wav", "03.wav", "04.wav", "05.wav"};
char* story2[]={
  "10.wav", "11.wav", "12.wav", "13.wav", "14.wav", "15.wav", "16.wav", "17.wav"};
char* story3[]={
  "20.wav", "21.wav", "22.wav", "23.wav", "24.wav"};

void setup() {
  Serial.begin(9600);
}

void loop() {
  now = millis();
  x1 = analogRead(pinx);
  y1 = analogRead(piny);
  z1 = analogRead(pinz);
  changeNow = sqrt((x1)^2+(y1)^2+(z1)^2);
  change = (changeNow - changeLast);
  timeChange = (double)(now - lastTime);
  accel = change/timeChange;
  Serial.print(accel);
  Serial.print("   ");
  changeLast = changeNow; 
  lastTime = now;

  if (accel > 1 || accel < -1)
  {
    storyCounter++;
    playfile(story1[storyCounter]);
  }

  if (accel <=1 && accel >= -1)
  {
    playfile(story1[storyCounter]);
  }

}

void playcomplete(char *name)     // Plays a full file from beginning to end with no pause.
{
  Serial.println("  playcomplete  ");
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) 
  {
    // do nothing while its playing
  }
  // now it's done playing
}

void playfile(char *name) 
{
  Serial.println("  playfile  ");
  if (wave.isplaying)     // see if the wave object is currently doing something if already playing something, so stop it!
  {
    wave.stop(); // stop it
  }

  if (!f.open(root, name))      // look in the root directory and open the file
  {
    putstring("Couldn't open file ");
    Serial.print(name);
    return;
  }

  if (!wave.create(f))          // OK read the file and turn it into a wave object
  {
    putstring_nl("Not a valid WAV");
    return;
  }

  wave.play();         // ok time to play! start playback
}


int freeRam(void)    // this handy function will return the number of bytes currently free in RAM, great for debugging!
{
  Serial.println("  freeRAM  ");
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) 
  {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }
  else 
  {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}

void sdErrorCheck(void)
{
  Serial.println("  dErrorCheck ");
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

void cardcheck()
{
  Serial.println("cardcheck");
  //  if (!card.init(true)) [ //play with 4 MHz spi if 8MHz isn't working for you
  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!
  }
  putstring("Using partition ");        // Let's tell the user about what we found
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
  if (!root.openRoot(vol))             // Try to open the root directory
  {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
  putstring_nl("Ready!");          // Whew! We got past the tough parts.
}

All and any of your help is greatly appreciated…

You don’t need two “if” statements.
Your code…

 if (accel > 1 || accel < -1)
  { storyCounter++;
    playfile(story1[storyCounter]);
  }
  if (accel <=1 && accel >= -1)
  { playfile(story1[storyCounter]);
  }

Says to me…

  1. Add one to storyCounter ONLY if accel is outside the range of -1 and +1.
  2. Since you call playfile() no matter what, it doesn’t need to be in the selection statements, so If I understand your task, I’d do this…
if (accel > 1 || accel < -1)
  storyCounter++;

playfile(story1[storyCounter]);

Still no dice... It doesn't seem to be playing anything at all. Any other thoughts? A friend suggested a state machine, but I don't know how to implement that.

What are your debug prints telling you?

Hmm, it's reading "Couldn't open file 00.wav0 playfile"

Maybe it's adding storyCounter to the end of the wav? Is that possible? I think what I want is for storyCounter to replace the first digit of any of the .wav files...

Edit: Actually, I think that 0 might be the accel reading. Wah, I'm so sloppy..