import ukcrobots.simplecore.Motor;
import ukcrobots.simplecore.TouchSensor;
import ukcrobots.util.Sleeper;

/**
 * A main method for the Bumper class.
 */
public class BumperPollingMain
{
    public static void main(String[] args)
    {
        Bumper bump = new Bumper();
        bump.run();
    }
}

/**
 * An illustration of how to use a simple model with
 * motors attached to port A and port C. This model
 * has a TouchSensor attached. When touched, the model reverses
 * its direction. This code uses polling to identify when
 * the sensor has been touched.
 *
 * @author David J. Barnes (d.j.barnes@ukc.ac.uk)
 * @version 2003.03.26
 */
class Bumper
{
    private Motor left, right;
    private TouchSensor touch;

    /**
     * Set up the motors and touch sensor.
     */
    public Bumper()
    {
        left = new Motor('A');
        right = new Motor('C');
        touch = new TouchSensor(2);
    }

    /**
     * Run for up to five presses of the touch sensor.
     */
    public void run()
    {
        left.setPower(1);
        right.setPower(1);

        // Run forward.
        left.forward();
        right.forward();

        // Stop after 5 touches.
        for(int i = 0; i < 5; i++){
            while(!touch.isPressed()){
                // Nothing to do until the sensor is pressed.
            }
            
            // The sensor has been pressed.
            left.reverse();
            right.reverse();
            
            // Wait for the release.
            while(touch.isPressed()){
            }
        }

        left.stop();
        right.stop();
    }
}

