Commit 64088dcb authored by Mikkel's avatar Mikkel
Browse files

ColorLineFollower

parent 5395b9db
import lejos.nxt.*;
/**
* A locomotion module with methods to drive
* a car with two independent motors. The left motor
* should be connected to port C and the right motor
* to port B.
*
* @author Ole Caprani
* @version 17.4.08
*/
public class Car
{
// Commands for the motors
private final static int forward = 1,
backward = 2,
stop = 3;
private static MotorPort leftMotor = MotorPort.C;
private static MotorPort rightMotor= MotorPort.B;
private Car()
{
}
public static void stop()
{
leftMotor.controlMotor(0,stop);
rightMotor.controlMotor(0,stop);
}
public static void forward(int leftPower, int rightPower)
{
leftMotor.controlMotor(leftPower,forward);
rightMotor.controlMotor(rightPower,forward);
}
public static void backward(int leftPower, int rightPower)
{
leftMotor.controlMotor(leftPower,backward);
rightMotor.controlMotor(rightPower,backward);
}
}
import lejos.nxt.*;
import lejos.util.Delay;
/**
* A simple line follower for the LEGO 9797 car with a light sensor. Before the
* car is started on a line a BlackWhiteSensor is calibrated to adapt to
* different light conditions and colors.
*
* The light sensor should be connected to port 3. The left motor should be
* connected to port C and the right motor to port B.
*
* @author Ole Caprani
* @version 20.02.13
*/
public class ColorLineFollower {
public static int greenCount = 0;
public static void main(String[] aArg) throws Exception {
final int power = 80;
ColorSensor sensor = new ColorSensor(SensorPort.S1);
LCD.clear();
LCD.drawString("Press ENTER", 0, 0);
LCD.drawString("to start!", 0, 1);
while (Button.ENTER.isDown()); // Wait till the button is released.
while (!Button.ENTER.isDown()); // Wait till the button is pressed again.
String colorNames[] = { "None", "Red", "Green", "Blue", "Yellow",
"Megenta", "Orange", "White", "Black", "Pink", "Grey",
"Light Grey", "Dark Grey", "Cyan" };
while (!Button.ESCAPE.isDown()) {
ColorSensor.Color vals = sensor.getColor();
LCD.drawString(colorNames[vals.getColor() + 1], 10, 2);
LCD.refresh();
if (vals.getColor() == ColorSensor.Color.GREEN) {
greenCount++;
if (greenCount > 30) {
break;
}
} else {
greenCount = 0;
}
if (vals.getColor() == ColorSensor.Color.BLACK || vals.getColor() == ColorSensor.Color.GREEN)
Car.forward(power, 0);
else if(vals.getColor() == ColorSensor.Color.WHITE)
Car.forward(0, power);
else {
Car.forward(power / 2, power / 2);
}
Thread.sleep(10);
}
Car.stop();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
LCD.refresh();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment