   if ((millis() - lastHangDebounceTime) > debounceDelay) { //then the bounce settled
     if (hangReading != hangState) {//then reciever state has changed
       hangState = hangReading;
-      ledState = hangState;   
       
       if(hangState == LOW) {//if first time off hook, start dial tone
-        dialState = nodial;
+        phoneState = nodial;
+        putstring_nl("hang low");
       }   
-    }
-  }
-  
-  //debounce the reciever switch
-  if (hangReading != lastHangState) {
-    lastHangDebounceTime = millis();
-  }
-
-  if ((millis() - lastHangDebounceTime) > debounceDelay) { //then the bounce settled
-    if (hangReading != hangState) {//then reciever state has changed
-      hangState = hangReading;
-      ledState = hangState;   
-      
-      if(hangState == LOW) {//if first time off hook, start dial tone
-        dialState = nodial;
-      }   
-    }
-  }
+      else {
+        putstring_nl("hang high");
+      }
+    }
+  }
+  
+  
 
   digitalWrite(ledPin, ledState);
 

dir_t dirBuf;     // buffer for directory reads

const int hangPin = 6; //pin for reciever switch. closed (HIGH) when hung up, open (LOW) when off the hook

const int ledPin = 13; //LED pin

int ledState = HIGH;
int hangState; //current reciever reading
int lastHangState = LOW; //previous reciever reading

long lastHangDebounceTime = 0;  // the last time the reciever pin was toggled
const long debounceDelay = 50;    // the debounce time; increase if the output flickers

State dialState = nodial;

void playfile(char *name) {
  if (wave.isplaying) {

    //stop any playback
    wave.stop();
    //reset any state
    dialCount = 0;
    dialIndex = 0;
    
    int i = 0;
    for(i = 0; i < DIAL_ARRAY_LEN; i++) {
      dialArray[i] = -1;
    }
  }
  else {//off the hook
    //debounce the dial switch
    if (dialReading != lastDialState) {
      lastDialDebounceTime = millis();
    }
  
    if ((millis() - lastDialDebounceTime) > debounceDelay) { //then the bounce settled
      if (dialReading != dialState) {//then reciever state has changed
        dialState = dialReading; 
        ledState = dialState;
        
        if(dialState == LOW) {
          putstring("dial low");
          if(dialReading == HIGH) {
            putstring("\tdial reading high");
          }
          else {
            putstring("\tdial reading high\n");
          }
        }
        
        //if just started dialing, reset count
        if(dialState == HIGH) {
          dialCount = 0;
          putstring_nl("dial high");
          

        }
        //if just ended dialing, save count, set to dialing state if recied a count > 0 (10 is the real zero)
        //if dialindex bigger than dial array size, then act like wrong number
        //if dialed number matches anything then play
        else if(dialCount > 0) {
          int i;
          phoneState = dialing;
          dialArray[dialIndex] = dialCount;
          if(dialArray[dialIndex] >= 10) {
            dialArray[dialIndex] = 0;
          }
          dialIndex++;
          
          i = 0;
          int match = 1;
          for(i = 0; i < p1_len; i++) {
            if(p1[i] != dialArray[i]) {
              match = 0;
              break;
            }
          }
          
          if(match) {
            playfile("P1.WAV");
          }
          
          i = 0;
          match = 1;
          for(i = 0; i < p2_len; i++) {
            if(p2[i] != dialArray[i]) {
              match = 0;
              break;
            }
          }
          
          if(match) {
            playfile("P2.WAV");
          }
          
        }  
      }
    }
    
    if(dialState == HIGH) {
      //debounce the count switch
      if (countReading != lastCountState) {
        lastCountDebounceTime = millis();
      }
    
      if ((millis() - lastCountDebounceTime) > countDebounceDelay) { //then the bounce settled
        if (countReading != countState) {//then reciever state has changed
          countState = countReading; 
          
          
          //if just started dialing, reset count
          if(countState == LOW) {
            dialCount++;
            putstring_nl("count low");
  
          }
          else {
            putstring_nl("count high");
          }

        }
      }
    }
    
    switch(phoneState) {
      case nodial:
        if(!wave.isplaying) {//if stopped playing, start again
          playfile("DIALTONE.WAV");
