Phys/CSci 135: Assignments
Home Syllabus Assignments Samples

Lab 2. Acting and Reacting

ButtonLCD (page 9)

import lejos.nxt.*;

public class ButtonLCD {
    public static void main(String[] args) {
        while (Button.ESCAPE.isPressed() == false) {
            if (Button.LEFT.isPressed() == true) {
                LCD.drawString("Left"00);
            }
            if (Button.RIGHT.isPressed() == true) {
                LCD.drawString("Right"00);
            }
        }
    }
}

ForwardBack (page 10)

import lejos.nxt.*;

public class ForwardBack {
    public static void main(String[] args) {
        while (Button.ESCAPE.isPressed() == false) {
            if (Button.LEFT.isPressed() == true) {
                Motor.B.forward();
                Motor.C.forward();
            }
            if (Button.RIGHT.isPressed() == true) {
                Motor.B.backward();
                Motor.C.backward();
            }
        }
    }
}

ForwardPauseBack (page 12)

import lejos.nxt.*;

public class ForwardPauseBack {
    public static void main(String[] args) {
        while (Button.ESCAPE.isPressed() == false) {
            if (Button.LEFT.isPressed() == true) {
                Motor.B.forward();
                Motor.C.forward();
            } else if (Button.RIGHT.isPressed() == true) {
                Motor.B.backward();
                Motor.C.backward();
            } else {
                Motor.B.stop();
                Motor.C.stop();
            }
        }
    }
}

GetAway (page 14)

import lejos.nxt.*;

public class GetAway {
    public static void main(String[] args) {
        TouchSensor leftBump = new TouchSensor(SensorPort.S1);
        TouchSensor rightBump = new TouchSensor(SensorPort.S2);

        while (Button.ESCAPE.isPressed() == false) {
            if (leftBump.isPressed() == true) {
                Motor.B.stop();
                Motor.C.forward();
            }
            if (rightBump.isPressed() == true) {
                Motor.B.forward();
                Motor.C.stop();
            }
        }
    }
}