     //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
-    switch(dialState) {
+    //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");

  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;
      }   
    }
  }
  
  //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;
      }   
    }
  }

  digitalWrite(ledPin, ledState);


  }
  
  lastHangState = hangReading;
  lastDialState = dialReading;
  lastCountState = countReading;
}

