 const int ping_distance = 80; // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
 const byte ping_delay = 200; // Number of milliseconds to wait before next ping
 NewPing sonar(ping_trigger, ping_echo, ping_distance); // NewPing setup of pins and maximum distance.
-
-
+int ping_timer = 0; // Stores the timer used for the ping sonar.
 
 // Battery configuration
 const byte battery_led = 5;

  return result; // Vcc in millivolts
}

/********************************************************************************
Sound functions
********************************************************************************/
/*
void sound_nextNote()
{
  ++sound_current_note;
  // @todo: remove hard coding to the 8 note melody
  if (sound_current_note < 8) {
    sound_playNote(sound_current_note);
  } else {
    // reset the counter
   sound_current_note = 0; 
  }
}

void sound_stopNote()
{
  // stop the tone playing:
  noNewTone(sound_pin);
  sound_nextNote();
}

void sound_playNote(int note)
{
  int noteDuration = 1000/noteDurations[note];
  NewTone(sound_pin, melody[note], noteDuration);

  // to distinguish the notes, set a minimum time between them.
  // the note's duration + 30% seems to work well:
  int pauseBetweenNotes = noteDuration * 1.30;
  Serial.print(melody[note]); 
  Serial.print(" : ");
  Serial.println(pauseBetweenNotes);
  timer.setTimeout(pauseBetweenNotes, sound_stopNote);
}

void sound_playMelody() 
{
  sound_playNote(sound_current_note);
}
*/


int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };
unsigned long melody_delay = 10000; // Number of milliseconds to wait before next melody

// The motion states, so we alwasys no where we're going.
enum motion_states {
  M_STOP, 
  M_FWD, 
  M_REV,
  M_ROTATE_LEFT,
  M_ROTATE_RIGHT
};
// The curent motion state.
motion_states motion_state = M_STOP;

// The action states, so we alwasys no what action is currently happening.
enum action_states {
  A_STOPPED,
  A_TRUNDLE, 
  A_PINGSEARCH,
  A_DANCE1,
  A_DANCE2,
  A_BORED,
  A_HAPPY,
  A_CALIBRATION
};
// The curent action state.
action_states action_state = A_STOPPED;
int timer_action = 0;
byte action_done = 0; // mnmm needs explaining

// There must be one global SimpleTimer object.
// More SimpleTimer objects can be created and run,
// although there is little point in doing so.
SimpleTimer timer;

// Creat the sound playing object.
Sound snd(sound_pin);

void setup() {
