/*
  Polite ActivityBot.c

  Version 1.0, 10/31/2013

  http://learn.parallax.com/activitybot

  Requires abdrive version 0.5.5 and wavplayer 0.9 

  Drive forward until object detected with PING))) sensor on P8, within 20 cm.
  Stop and play 1st audio response from SD, then recheck for object. If it moved,
  play 2nd audio response and continue. If it did not move, play 3rd audio response,
  rotate left or right at random until clear path is detected, then drive forward. 
*/

#include "simpletools.h"                       // Include header files
#include "ping.h"
#include "wavplayer.h"                         // Must be 0.9 or later, 10-31-2013
#include "abdrive.h"                           // Must be 0.5.5 or later, 10-31-2013

int main()                                     // Main function
{
  freqout(4, 2000, 2000);                      // Reset indicator
  drive_setRampStep(10);                       // 10 ticks/sec / 20 ms
  
  int DO = 22, CLK = 23, DI = 24, CS = 25;     // SD I/O pins
  sd_mount(DO, CLK, DI, CS);                   // Mount SD card
  wav_volume(10);                              // Set volume here, 1-10

  while(1)
  {
    drive_ramp(128, 128);                      // Forward 128 ticks/second
    while(ping_cm(8) >= 20) pause(5);          // Drive until object in range
  
    drive_ramp(0, 0);                          // Then stop
    wav_play("excuseme.wav");                  // Play wav file named in string
    pause(4000);                               // Time to speak & let obstacle move
 
    if(ping_cm(8) < 21)                        // If obstacle is still there
    {  
      wav_play("byebye.wav");                  // Play wav file named in string
      pause(1000);                             // Time to speak before moving 

      int turn = rand() % 2;                   // Turn in a random direction
      if(turn == 1)                            // If turn is odd (1)
        drive_speed(48, -48);                  // rotate right
      else                                     // else if turn is even (0)
        drive_speed(-48, 48);                  // rotate left
    
      while(ping_cm(8) < 20);                  // Turn till object not in view
    }
    else
    {
      wav_play("thankyou.wav");                // Play wav file named in string
      pause(1000);                             // Time to speak before moving
    }
  } 
}

