Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. COLOUR SENSOR CODE:
  2.  
  3. import lejos.nxt.*;
  4. import lejos.robotics.*;
  5.  
  6. public class colourSensor {
  7.  
  8. public Color getColour() {
  9.  
  10. ColorSensor RGBsensor = new ColorSensor(SensorPort.S1); //PORT DEFAULTED TO S1, NEED TO FIND A WAY TO PASS AS A PARAMETER IF POSSIBLE
  11. Color DetectedColour = RGBsensor.getColor(); //MAY NEED TO CHANGE TO GET COLORID
  12. return DetectedColour; //Returns detected colour as data type Colour
  13.  
  14.  
  15. }
  16.  
  17. }
  18. _________________________________________________________________________________________________________________________________
  19.  
  20.  
  21. import lejos.nxt.*;
  22. import lejos.robotics.*;
  23.  
  24.  
  25. public class test {
  26.  
  27. public static void main(String[] args) {
  28.  
  29. //mainRobot robot = new mainRobot();
  30.  
  31. //Clear previous movement and move forward
  32. LCD.drawString("program 1", 0, 0);
  33. Button.waitForAnyPress();
  34. LCD.clear();
  35. Motor.A.forward();
  36. LCD.drawString("forward", 0, 0);
  37.  
  38. //Move backwards
  39. Button.waitForAnyPress();
  40. LCD.drawString("backward", 0, 0);
  41. Motor.A.backward();
  42.  
  43. //Stop the motor.
  44. Button.waitForAnyPress();
  45. Motor.A.stop();
  46. Color x = colourSensor.getColour();
  47. System.out.println();
  48.  
  49.  
  50. int counter = 0;
  51.  
  52.  
  53. level1Rob robot = new level1Rob();
  54. for(;;) {
  55. //If colour sensor does not find the coloured line display the message.
  56. if(robot.colourSensor(1) == false ) {
  57. System.out.println("no light found");
  58.  
  59. Motor.A.forward(true);
  60. Motor.D.forward(true);
  61.  
  62.  
  63. }
  64. //If colour sensor does find the coloured line display the message.
  65. if(robot.ColourSensor(1) == true) {
  66. System.out.Println("Light source found");
  67. }
  68. //While the colour sensor has not found the coloured line, keep moving forward.
  69. while(robot.ColourSensor(1) == true ) {
  70. Motor.A.forward();
  71. }
  72. //Once the colour sensor has found the light, stop moving
  73. while(robot.ColourSensor(1) == false ) {
  74. Motor.A.stop();
  75. }
  76. }
  77.  
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement