import lejos.nxt.*;
import lejos.util.*;

/**
 allow premature ending of rotation

 @author Roger (from a www.lejos.org tutorial) 
 @author modified by Sharon Tuttle
 @version 2015-02-06
**/

public class EndRotatePlay
{
    /** 
     allow user to click a button to end specified
     rotation prematurely

     @param args not used
    **/

    public static void main(String[] args)
    {
        System.out.println("end rot early");
        Button.waitForAnyPress();
        LCD.clear();

        Motor.B.rotateTo(1440, true);

        // read and print B's tachometer every
        //    .2 seconds, BUT IF someone presses
	//    a button BEFORE we reach 1440,
	//    we want to STOP the rotation

        while (Motor.B.isMoving())
        {
            Delay.msDelay(200);
            LCD.drawInt(Motor.B.getTachoCount(), 0, 0);

            // static Button method readButtons
	        //    expects nothing, and returns an int
		    //    with bits set if one or more of the
		        //    NXT's buttons is pressed -- it only
			    //    then returns 0 if ALL buttons are 
			        //    released
            // KLUGING here, so that if NOT 0 at this
            //    point, STOP the rotation (if a button
	        //    is pressed at this point)

            if (Button.readButtons() > 0)
            {
                Motor.B.stop();
            }
        }

        LCD.drawString("after loop: " + Motor.B.getTachoCount(),
                      0, 1);

        // sometimes the stop-early-press causes following
        //    wait to END program before I can read the
	//    post-loop tachometer reading --
	// SO, I'm delaying a second

        Delay.msDelay(1000);

        Button.waitForAnyPress();
    }
}