#include <timeObj.h>
#include <blinker.h>
#include <squareWave.h>
#include <mechButton.h>

#define BEEP_PIN 2
#define BEEP_HZ 262


timeObj     aTimer(5000);         // How often, in Ms does the beeper go off.
mechButton  theBtn(3);            // Debounced button object.
squareWave  beepTime(750,250);
bool        sounding;

void setup() {

  sounding = false;               // We're not making noise now.
  theBtn.setCallback(buttonClk);  // Tell the button to call this when it's changed.
}


// Gets called when the button changes state.
void buttonClk(void) {

  if (!theBtn.trueFalse()) {    // If the button was pressed. (False for grounded)
    noTone(BEEP_PIN);           // Shut off the beeper.
    sounding = false;           // Note that its quiet now.
    beepTime.setOnOff(false);   // Shut off the wave form (Beep pattern)   
    aTimer.start();             // Restart the timer. (Timing)
  }
}


void loop() {

  idle();                                     // Runs the magic.
  if (aTimer.ding()) {                        // If the timer has expired *ding*!
    beepTime.setOnOff(true);                  // Fire up the beeper.
    aTimer.reset();                           // Reset the timer. (Off)
  }                                           //
  if (!sounding && beepTime.pulseHiLow()) {   // If not sounding and pulse is high..
    tone(BEEP_PIN,BEEP_HZ);                   // Start the beep sound
    sounding = true;                          // Note its making noise.
  }                                           // 
  if (sounding && !beepTime.pulseHiLow()) {   // IF it IS making noise and trhe pulse is low..
    noTone(BEEP_PIN);                         // Shut off the noise.
    sounding = false;                         // Note that the noise is off.
  }
}