[currently viewing: /LegoNXT/FollowLine.nxc]
 
#define SpeedSlow 50
#define SpeedFast 100

int SV;
int LoopCount;

sub FollowLine(int loopTime)
{
  int Threshold1=600;
  int Threshold2=630;
  int theSpeed;
  long t;

  // set sensor type and mode
  SetSensorType(IN_3, IN_TYPE_LIGHT_ACTIVE);
  SetSensorMode(IN_3, IN_MODE_RAW);

  // start looping
  t = CurrentTick() + loopTime;
  while (t > CurrentTick())
  {
    // read the light sensor value
    SV = SensorRaw(IN_3);
    
    // set speed for motor 1
    if (SV < Threshold2)
      OnFwd(OUT_A, SpeedFast);
    else
      OnFwd(OUT_A, SpeedSlow);
      
    // set speed for motor 2
    if (SV > Threshold1)
      OnFwd(OUT_B, SpeedFast);
    else
      OnFwd(OUT_B, SpeedSlow);
      
    // display sensor value
//    NumOut(0, LCD_LINE1, false, SV);
    
    LoopCount++;
  }
  // 10 second loop is done

}

task main()
{
  string lcStr;
  string svStr;
  string msg;
  
  // call subroutine
  FollowLine(10000);
  
  // output results
  lcStr = NumToStr(LoopCount);
  svStr = NumToStr(SV);
  msg = svStr + " - " + lcStr;
  TextOut(0, LCD_LINE1, msg);
  
  // stop both motors
  Off(OUT_AB);
  
  // let user see the last message
  Wait(10000);
}