       // Button is no longer pressed, so reset the button state.
       noteButtonState = 0;
     }
+    if ((millis() - debounce[i]) > DEBOUNCE_THRESHOLD) {
 #ifdef SENSOR_FIX
-    int threshold = (i == 3) ? 950 : THRESHOLD;
-    if(analogRead(0) > threshold) {
+      int threshold = (i == 3) ? 950 : THRESHOLD;
+      if (analogRead(0) > threshold) {
 #else
-    if(analogRead(0) > THRESHOLD) {
+      if (analogRead(0) > THRESHOLD) {
 #endif
-      /* Apply the AND operator to the lastNote indicator and the
-       * corresponding value from the laser array to check if the
-       * laser bit has been set or not
-       *
-       * If it returns a non zero value, then that means it is set.
-       * If it is a zero, then the bit has not been set yet */
-      if (!(lastNote & (laserArray[i]))) {
-        // Beam has been cut
-        musicOn(i);
-        /* Set the bit using the OR operator between lastNote and
-         * the corresponding value from the laser array */
-        lastNote |= (laserArray[i]);
+        /* Apply the AND operator to the lastNote indicator and the
+           corresponding value from the laser array to check if the
+           laser bit has been set or not
+
+           If it returns a non zero value, then that means it is set.
+           If it is a zero, then the bit has not been set yet */
+        if (!(lastNote & (laserArray[i]))) {
+          debounce[i] = millis();
+
+          // Beam has been cut
+          musicOn(i);
+          /* Set the bit using the OR operator between lastNote and
+             the corresponding value from the laser array */
+          lastNote |= (laserArray[i]);
+        }
+      } else {
+        /* Check if the last note bit has been set or not using the
+           AND operator. This won't always be true because this section
+           of the code runs even if the beam hasn't been cut yet */
+        if ((lastNote & (laserArray[i]))) {
+          debounce[i] = millis();
+          /* Unset the flag using the AND and invert operators */
+          lastNote &= ~(laserArray[i]);
+        }
+        // Hand is gone. Stop the sound
+        musicOff(i);
       }
-    } else {
-      /* Check if the last note bit has been set or not using the
-       * AND operator. This won't always be true because this section
-       * of the code runs even if the beam hasn't been cut yet */
-      if ((lastNote & (laserArray[i]))) {
-        /* Unset the flag using the AND and invert operators */
-        lastNote &= ~(laserArray[i]);
-      }
-      // Hand is gone. Stop the sound
-      musicOff(i);
-    }
-    #ifndef NODELAY
-    // Delay 10 to prevent weird analog readings
-    delay(DELAY);
-    #endif
+#ifndef NODELAY
+      // Delay 10 to prevent weird analog readings
+      delay(DELAY);
+#endif
+    }
   }
 }

  // LASTPORT+1 ports, from 0 to LASTPORT
  for (int i = 0; i <= LASTPORT; i++) {
    /* The following three lines convert the port we want
     * into the correct bit selections for the 74HC4051. I'm
     * not really sure exactly how it works, but I'm pretty
     * sure it's some bit shifting
     */
    r0 = bitRead(i,0);
    r1 = bitRead(i,1);
    r2 = bitRead(i,2);
    // Select the correct input using the values we calculated above
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);
    // Detect if the instrument selection button is pressed
    if(digitalRead(INSTRUMENTBUTTON) == HIGH) {
      /* Make sure that this is the first time that the button
       * has been pressed
       */
      if (instrumentButtonState == 0) {
        if (currentInstrument >= (NUMINSTRUMENTS - 1)) {
          // Max number of instruments, so wrap around to 0

  // Initialize serial at 115200 baud
  Serial.begin(115200);
  // Set the 74HC4051 command ports
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  // Wait for a button press to start the loop, because
  // delaying can either be too short or too long, and
  // Hairless-MIDI (Windows) can't properly handle malformed
  // MIDI commands.
  while (!start) {
    if (digitalRead(INSTRUMENTBUTTON) == HIGH) {
      start = true;
