/* Follows an object if it moves (such as a sheet of paper held in front of it)
 * using the Scribbler 2 IR sensors.
 */

#include <stdio.h>
#include <unistd.h>
#include <MyroC.h>
#include <stdbool.h>

int
main (void)
{
  const int NUM_SAMPLES = 10;

  rSetForwardnessTxt ("fluke-forward");

  int irValues[2]; // array to store sensor readings
  // irValues[0] is left, and irValues[1] is right

  while (true)
  {
    rGetIRAll (irValues, NUM_SAMPLES);
      
    if ( irValues[1] & irValues[1] )
      // both sensors don't sense obstacles
      rForward (1,1);              
      
    else if ( (!irValues[0]) & irValues[1] )
      // right senses obstacle, left doesn't
      rTurnRight (1, .2);          
     
    else if (irValues[0] & !irValues[1] )
      // left senses obstacle, right doesn't
      rTurnLeft (1, .2);

    else if ( !irValues[0] & !irValues[1] ) // both sense obstacle
      sleep (1);
  } // while
  
  rDisconnect();
  return 0;
} // main

