' first determine what state we're in
select case GetVariable("state")

  case "0"

    ' if we are still sampling then get 
    ' the sample count
    sample = GetVariable("sample")

    ' set the motors to rotate
    SetVariable "LEFT_MOTOR", 3
    SetVariable "RIGHT_MOTOR", -3

		' get how many pixels exist in the 
		' current bounding box    
    size = GetVariable("COG_AREA")
    min_size = GetVariable("min_size")
    if min_size = 0 then
      SetVariable "min_size", size
    else    
      ' if it is the smallest we've seen
      ' then remember that
      if size<min_size then
        SetVariable "min_size", size
      end if  
    end if

    sample = sample + 1

    ' if we're done sampling
    ' change the state to the next 
    ' step
    if sample>80 then
      SetVariable "state", "2"
      SetVariable "sample", 0
    else
      SetVariable "sample", sample
    end if
    
  case "2"

    ' we're done sampling, now we
    ' need to keep rotating until
    ' we see the smallest side
    ' again
    SetVariable "LEFT_MOTOR", 3
    SetVariable "RIGHT_MOTOR", -3

    size = GetVariable("COG_AREA")
    min_size = GetVariable("min_size")

    ' if the current pixels within the
    ' bounding box is with 80% of the
    ' smallest recorded amount then
    ' assume that we have a match
    if (min_size / size) > .8 then

      ' we have a match so stop
      ' the motors and jump to the 
      ' next state
      SetVariable "LEFT_MOTOR", 0
      SetVariable "RIGHT_MOTOR", 0
      SetVariable "state", "3"

    end if
  
  case "3"

    ' we're done ... reset the min_size
    ' in case we run this again.
    SetVariable "min_size", 10000

end select
