/* Program to find a object and follow it if it moves (e.g. paper used as
 * "wall").  This uses the Scribbler 2 IR sensors.
 */

#include <stdio.h>
#include <unistd.h> /* for sleep(3) */
#include <MyroC.h>


int
main (void)
{
  rConnect ("/dev/rfcomm0");

  int left;
  int right;

  while (1)
    {
      left = rGetIRTxt ("left", 3);
      right = rGetIRTxt ("right", 3);

      printf ("left = %d", left);
      printf ("\tright = %d\n", right);

      if (!left && !right) // no obstacle
        {
          printf ("moving forward\n");
          rForward (1,1);
        }
      else if (left && !right) // obstacle at left
        {
          printf ("turning right\n");
          rTurnRight (1, .2);
        }
      else if (!left && right) // obstacle at right
        {
          printf ("turning left\n");
          rTurnLeft (1, .2);
        }
      else if (left && right) // obstacle at both
        {
          printf ("no motion\n");
          sleep (1);
        }
    } // while

  rDisconnect ();
  return 0;
} // main
