Wave Shield is not going fast enough

OK, so I’ve built a laser harp that has 12 beams that shine onto photo-resistors, and using the wave shield when the beam is broken it makes a noise. When the code is used on a breadboard with 12 buttons it worked fine. Our problem is when uploaded to the harp the delay between breaking the beam and a noise being played is slow, and when switching beams the transfer time is even slower. So in essence we can barely manage 1/8 notes. Do you have any ideas for why this is so slow. When I uploaded a basic code that turns on a LED when the beam is broken, the speed at which the Arduino could comprehend was definitely fast enough. My only idea is that the code reads “just released” when there is absolutely no light shown onto the sensor so it takes a little bit of time for the sensor to read completely dark. Any ideas? I’m stuck

I trimmed up the code for the preview, the rest is in the attachment
The photo resistor that I’m using is 5k

//12 string Laser Harp

//Analog 0 and A1 have 2 sensors set up
//while pins  6,7,8,9,A2,A3,A4,A5 are digital

int isButtonBeingPushed1 = 0;   
int oneShotMode1 = 1; // 1 = oneshot mode on, 0 = oneshot mode off
byte buttonStateRaw1;

int isButtonBeingPushed2 = 0;   
int oneShotMode2 = 1; // 1 = oneshot mode on, 0 = oneshot mode off
byte buttonStateRaw2;

#define DEBOUNCE 5  // button debouncer

byte buttons[] = {6, 7, 8, 9, 16, 17, 18, 19};

#define NUMBUTTONS sizeof(buttons)

volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  byte i;

  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
    }
  
 
void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(buttons[index]);   // read the button
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}

void loop() {
 // check the button state
  buttonStateRaw1 = Wire.read();                                     
  delayMicroseconds(3);
 
 if(buttonStateRaw1 == 1 && isButtonBeingPushed1 == 0)           
 {
     isButtonBeingPushed1 = 1;
   
   if(oneShotMode1 == 1)
   {
     oneShotMode1 = 0;
  }
   else if(oneShotMode1 == 0)
   {
     oneShotMode1 = 1;
   }
   delay(DEBOUNCE);
 }
 else if(buttonStateRaw1 == 0 )
  {
       isButtonBeingPushed1 = 0; 
  }
   unsigned long timeSinceButtonPush1;                          
  int analog0;  
  delay(2);
  timeSinceButtonPush1;
  if(analog0 > 100 && analog0 < 300) // button1
  {
    buttonState1 = 1;
    playfile("9.WAV");
    while (wave.isplaying && analog0 > 100 && analog0 < 300)
    analog0 = analogRead(0); 
    buttonState1 = 0;
  }
  wave.stop();
 
  if (analog0 > 300 && analog0 < 370) // button 2
  {

    buttonState1 = 1;
    playfile("10.WAV");
    while (wave.isplaying && analog0 > 300 && analog0 < 370)
    analog0 = analogRead(0);
    buttonState1 = 0;
    
  }
  wave.stop();
  
  buttonStateRaw2 = Wire.read();                                   
  delayMicroseconds(3);
 
 if(buttonStateRaw2 == 1 && isButtonBeingPushed2 == 0)           
 {
     isButtonBeingPushed2 = 1;
   
   if(oneShotMode2 == 1)
   {
     oneShotMode2 = 0;
  }
   else if(oneShotMode2 == 0)
   {
     oneShotMode2 = 1;
   }
   delay(DEBOUNCE);
 }
 else if(buttonStateRaw2 == 0 )
  {
       isButtonBeingPushed2 = 0; 
  }
   unsigned long timeSinceButtonPush2;                          
  int analog1; 
  delay(2);
  timeSinceButtonPush2;
  if(analog1 > 150 && analog1 < 340) // button1
  {
    buttonState2 = 1;
    playfile("11.WAV");
    while (wave.isplaying && analog1 > 150 && analog1 < 340)
    analog1 = analogRead(1); 
    buttonState2 = 0;
  }
  wave.stop();

  if (analog1 > 340 && analog1 < 395) // button 2
  {
    buttonState2 = 1;
    playfile("12.WAV");
    while (wave.isplaying && analog1 > 340 && analog1 < 395)
    analog1 = analogRead(1);
    buttonState2 = 0;
  }
  wave.stop();
  // Digital Pins
  byte i;

  if (justreleased[0]) {
    justpressed[0] = 0;
    playfile("1.WAV");
    while (wave.isplaying && pressed[0]) {
      //Serial.print(".");
    }
    wave.stop();    
  }
  if (justreleased[1]) {
    justpressed[1] = 0;
    playfile("2.WAV");
    while (wave.isplaying && pressed[1]) {
      //Serial.print(".");
    }
    wave.stop();    
  }
  if (justreleased[2]) {
    justpressed[2] = 0;
    playfile("5.WAV");
    while (wave.isplaying && pressed[2]) {
    }
    wave.stop();    
  }
  if (justreleased[3]) {
    justpressed[3] = 0;
    playfile("6.WAV");
    while (wave.isplaying && pressed[3]) {
      //Serial.print(".");
    }
    wave.stop();    
 }
  if (justreleased[4]) {
    justpressed[4] = 0;
    playfile("3.WAV");
    while (wave.isplaying && pressed[4]) {
      //Serial.print(".");
    }
    wave.stop();    
  }
  if (justreleased[5]) {
    justpressed[5] = 0;
    playfile("4.WAV");
    while (wave.isplaying && pressed[5]) {
      //Serial.print(".");
    }
    wave.stop();    
  }
  if (justreleased[6]) {
    justpressed[6] = 0;
    playfile("7.WAV");
    while (wave.isplaying && pressed[6]) {
      //Serial.print(".");
    }
    wave.stop();    
  }
  if (justreleased[7]) {
    justpressed[7] = 0;
    playfile("8.WAV");
    while (wave.isplaying && pressed[7]) {
      //Serial.print(".");
    }
    wave.stop();    
  }
}
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  }  
}

Di8_4An_harp_code.ino (10.1 KB)

the delay between breaking the beam and a noise being played is slow

Delays are neither fast or slow. They can be long or short.

 // check the button state
  buttonStateRaw1 = Wire.read();                                     
  delayMicroseconds(3);

The code does not go with the comment. What are you using Wire for?

  timeSinceButtonPush1;

What is this supposed to do? Perhaps you should just replace it with:

   47;
  int analog0;  
  delay(2);

  if(analog0 > 100 && analog0 < 300) // button1
  {

Crap code. You have not assigned analog0 a value, so it contains an undefined value. Expecting the value to be in some range is absolutely pointless.

That was as far as I could stand to read.

I know the code is long and a little confusing (really confusing). I combined my code from two sources. One from Adafruit’s wave shield examples, and from this code I found online that reads analog values to play notes. There is a pause from when the beam is broken to when the note is being played. When I tested the code on a breadboard with 12 buttons it works fine and there is a very small delay between the button sending a value and the wave file being played.
Thank’s for your tolerance of my code despite it being complicated and having a unnecessary parts.

Adafruit’s code -http://learn.adafruit.com/system/assets/assets/000/010/345/original/wavehc_play6singlekeyonce.pde.txt

arduinoSamplePlayerAnalogRead12ButtonsOneLadder.ino (16.1 KB)

Thank's for your tolerance of my code despite it being complicated and having a unnecessary parts.

But, you're not going to do anything to make it better? Well, OK. Good luck.

It looks as if your code waits for each note to complete before it goes back to handling the inputs. If you want to control the notes while they are playing, you need to play them asynchronously i.e. start them playing and then have your code go on to do other things.

But, you’re not going to do anything to make it better?

PaulS, I’m a High School student who’s learning while building a school project. I’m doing my best to fix the problem under a time crunch. If my friend and I had the time we would devote all our resources to understanding and making the code better, but we have determined that the code works on the small scale despite it’s many flaws that you have pointed out. So we can’t completely redo everything with the little time we have now. It turns out the problem was extremely basic. The line of code if (justreleased[0]) { when changed to if (justpressed[0]) { allowed the whole harp to perfectly. Strange, but we’ll take it.