this was the challenge
- Code: Select all
Mission 1: A rover robot that moves in a straight line. The robot is required to do a
right 90° turn when it detects a 10% of brightness increment and continues its way.
Mission 2: A rover robot that moves in a straight line and detects the change of light
intensity. If the light intensity goes down the robot plays a happy tune and keeps
going. When the song ends the robot stops.
Mission 3: Design a robot program that avoids a beam of light
Mission 4: A rover robot starts its investigation tasks (going in a straight line) when it
finds a 30% or higher light intensity. When this happens the robot sounds a beep.
When the robot finds an obstacle it beeps, goes back an equivalent distance to its size,
makes a 90° left turn, goes forward the same distance and makes a 90° right turn
avoiding the obstacle. After this the robot continues its way until the light intensity goes below 30% which supposes the day is over. The rover robot beeps and stops
there.
now this is my program
- Code: Select all
import lejos.nxt.*;
import lejos.robotics.navigation.*;
import lejos.util.TextMenu;
public class LightSensorM1 {
static TachoPilot robot = new TachoPilot(4.32f, 12.9f,Motor.C, Motor.A, false); //Creates the robot object (wheel diameter,distance between wheels,motor left, motor right, reverse)
static ColorLightSensor cmps = new ColorLightSensor(SensorPort.S1,ColorLightSensor.TYPE_COLORFULL); //creates the Colorlightsensor object
public static void main(String[] args) { //main method
String missions[] = {"Mission1", "Mission2", "Mission3", "Mission4"}; //create the array for the mission menu
TextMenu modeMenu = new TextMenu(missions, 0, "Select the mission"); //displays the menu
for(;;){ //this loop makes it always return to the mission menu
robot.stop(); //this is so when you exist a mission it stops after that, meaning before choosing another
int mission = modeMenu.select(); //this is the method for
if(mission < 0){return;}
LCD.clear(); //clears screen
robot.reset(); //resets tachosensor in robot
robot.setSpeed(300); //set robot speed to cms/second
int firstLight =cmps.getLightValue(); //gets light from the start
cmps.setType(ColorLightSensor.TYPE_COLORFULL + 0); //makes the sensor to send the full colors (RGB)
switch (mission){ //switch statment is for the program to know which mission to run
case 0: //this is mission one
do { //do-while loop, makes it mandatory to run and after it is run, checks for the true statment
robot.forward(); //robots moves forward
int secondLight = cmps.getLightValue(); // gets current light and stores it in secondLight
if(firstLight + 10 <= secondLight){ //if first light plus ten is smaller or equal to current light, it will enter
robot.rotate(90); //rotate 90 degrees
}
firstLight = secondLight; //makes current light now the first light
}while(!Button.ESCAPE.isPressed()); //exits mission if escape is pressed
break;
case 1: //second mission
robot.forward();
compare: //just a flag
do {
int secondLight = cmps.getLightValue();
if(firstLight > secondLight + 10 ){
Sound.beepSequenceUp();
robot.stop();
break compare; //return to flag
}
firstLight = secondLight;
}while(!Button.ESCAPE.isPressed());
break;
case 2: //third mission
do {
int secondLight =cmps.getLightValue();
robot.forward();
if(firstLight <= secondLight - 7){
robot.rotate(180);
}
}while(!Button.ESCAPE.isPressed());
break;
case 3: //fourth mission
do{
robot.stop();
LCD.drawInt(cmps.getLightValue(), 0, 2); //draw light intensity (when not running)
while(cmps.getLightValue() >= 30){
robot.forward();
Sound.beep();
do{
String colorchecker = null;
colorchecker = cmps.readColor().name(); //gets color name for light thatbounces
LCD.drawString(colorchecker,0,0);
LCD.drawInt(cmps.getLightValue(), 0, 1);
if(colorchecker.equals("BLUE")){
Sound.beep();
robot.travel(-24); //travels -24 cms
robot.rotate(90);
robot.travel(24);
robot.rotate(-90);
colorchecker = "NOT BLUE"; //changes value of colorchecker so it wont stay in the loop and olny does this when it gets another obstacle
}
}while(cmps.getLightValue() >= 30);
}
}while(!Button.ESCAPE.isPressed());
break;
}
}
}
}
it has lots of comments becuase my teamates dont know java my teacher neither so i want them to understand a bit.
cheers, i expect feedback, wana get better
