Guest User

Untitled

a guest
Jun 4th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import lejos.nxt.Button;
  2. import lejos.nxt.LCD;
  3. import lejos.nxt.MotorPort;
  4. import lejos.nxt.SensorPort;
  5. import lejos.nxt.TouchSensor;
  6. import lejos.nxt.UltrasonicSensor;
  7.  
  8.  
  9. public class CraneProject {
  10.  
  11.  
  12.     public static void main(String[] args) throws InterruptedException {
  13.         screen();
  14.         down();
  15.         clawclose();
  16.         up();
  17.         forward(10);
  18.         down();
  19.         clawopen();
  20.         up();
  21.         back();
  22.     }
  23.    
  24.     public static void screen() throws InterruptedException{
  25.         LCD.drawString("Press Any Button to", 0, 0);
  26.         LCD.drawString("      Start", 0, 1);
  27.         LCD.drawString("   The Program", 0, 2);
  28.         Button.waitForAnyPress();
  29.         LCD.clear();
  30.         Thread.sleep(1000);
  31.     }
  32.    
  33.     public static void forward(int distance) throws InterruptedException{
  34.         MotorPort motor1 = MotorPort.A;
  35.         UltrasonicSensor sonic= new UltrasonicSensor(SensorPort.S3);
  36.        
  37.         motor1.resetTachoCount();
  38.        
  39.         while(true){
  40.             motor1.controlMotor(80, 1);                 //Setting motor speed and direction
  41.             if(sonic.getDistance()< distance)           //When the crane goes close to the wall it stops
  42.                 break;
  43.         }
  44.         stop1();
  45.        
  46.     }
  47.    
  48.     public static void back() throws InterruptedException{
  49.         MotorPort motor1 = MotorPort.A;
  50.         motor1.controlMotor(80, 2);
  51.         while(motor1.getTachoCount()>0);                //The Crane goes back to it's original position
  52.         stop1();
  53.     }
  54.    
  55.     public static void down() throws InterruptedException{
  56.         TouchSensor touch= new TouchSensor(SensorPort.S4);
  57.         MotorPort motor2=MotorPort.B;
  58.                
  59.         motor2.resetTachoCount();
  60.         motor2.controlMotor(65, 1);
  61.         while(!touch.isPressed())                       //When the touch sensor is pressed the downwards movement stops
  62.         {
  63.             try
  64.             {
  65.                 Thread.sleep(1);
  66.             } catch (InterruptedException e) {
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.         stop2();
  71.     }
  72.    
  73.     public static void up() throws InterruptedException{
  74.         MotorPort motor2 = MotorPort.B;
  75.         motor2.controlMotor(65, 2);
  76.         while(motor2.getTachoCount()>0);                //After the crane grabs the box it lifts it back up
  77.         stop2();
  78.     }
  79.    
  80.    
  81.     public static void clawclose() throws InterruptedException{
  82.         MotorPort motor3=MotorPort.C;
  83.         motor3.controlMotor(65, 2);
  84.         {
  85.             Thread.sleep(505);                          //Thread sleep is used for the cranes precise stopping
  86.         }
  87.         motor3.controlMotor(100, 3);
  88.         {
  89.             Thread.sleep(1000);
  90.         }
  91.         stop3();
  92.        
  93.     }
  94.    
  95.    
  96.    
  97.     public static void clawopen() throws InterruptedException{
  98.         MotorPort motor3=MotorPort.C;
  99.         motor3.controlMotor(65, 1);
  100.         {
  101.             Thread.sleep(520);                         
  102.         }
  103.         motor3.controlMotor(100, 3);
  104.         {
  105.             Thread.sleep(1000);
  106.         }
  107.         stop3();
  108.        
  109.     }
  110.    
  111.    
  112.     //Codes for stopping of Motor 1, 2, and 3.
  113.     public static void stop1 () throws InterruptedException{
  114.         MotorPort motor1 = MotorPort.A;
  115.         motor1.controlMotor(100, 3);
  116.         Thread.sleep(1000);
  117.     }
  118.     public static void stop2 () throws InterruptedException{
  119.         MotorPort motor1 = MotorPort.B;
  120.         motor1.controlMotor(100, 3);
  121.         Thread.sleep(1000);
  122.     }
  123.     public static void stop3 () throws InterruptedException{
  124.         MotorPort motor1 = MotorPort.C;
  125.         motor1.controlMotor(100, 3);
  126.         Thread.sleep(1000);
  127.     }
  128.    
  129. }
Advertisement
Add Comment
Please, Sign In to add comment