Exemplo n.º 1
0
 public static void main(String[] args) {
   try {
     Thread.setDefaultUncaughtExceptionHandler(new ThreadHandler());
     Sound.playTone(2000, 300);
     BtReceiver btReceiver = new BtReceiver();
     btReceiver.connect();
   } catch (Exception e) {
     Sound.buzz();
     System.out.println("Main: exception");
     System.out.println(e.toString());
     System.out.print(e.getMessage());
     Utils.sleep(1000);
     Button.waitForAnyPress();
     System.exit(0);
   }
 }
  // The recognize() method returns true if the object is a blue block
  public boolean recognize() {

    // Boolean that determines whether or not to start detecting what kind of object is in front
    boolean isRecognizing = true;

    while (isRecognizing) {

      // The robot will drive slowly if the light reads under 300, which means an object is closeby
      while (color.getNormalizedLightValue() < 300) {
        robot.setForwardSpeed(2);
      }

      // When the robot is close enough to the object, it will stop then turn on its
      // blue light which gives two noticeable different light values for a wooden and blue
      // block

      robot.setForwardSpeed(0);
      color.setFloodlight(Color.BLUE);

      // If the light value of blue reads over 250, it is a blue block
      if (color.getNormalizedLightValue() >= 250) {

        Sound.beep();
        LCD.drawString("Blue styrofoam block", 3, 5);

        // Turn the default light back on after analyzing an object
        color.setFloodlight(true);

        return true;

      } else { // If it is not a blue block, it is a wooden block

        Sound.buzz();
        LCD.drawString("Wooden block", 3, 5);

        // Turn the default light back on after analyzing an object
        color.setFloodlight(true);
      }

      // Set the boolean to false to break out of the while loop and stop analyzing the object
      isRecognizing = false;
    }

    // Default return value. It should never get here but JAVA requires a return type
    return false;
  }
Exemplo n.º 3
0
  public void run() {
    System.out.println("Connecting to " + name);
    conn = Bluetooth.connect(Bluetooth.getKnownDevice(name));
    conn.setIOMode(NXTConnection.PACKET);

    dis = conn.openDataInputStream();
    dos = conn.openDataOutputStream();
    System.out.println("NXT connected");

    // Request dimensions
    try {
      dos.writeByte(3);
      dos.flush();
    } catch (IOException e1) {
    }

    try {
      while (true) {
        byte type = dis.readByte();
        switch (type) {
          case 1:
            byte slot = dis.readByte();
            byte state = dis.readByte();
            states[slot] = state;
            break;

          case 3:
            slotCount = dis.readByte();
            stateCount = dis.readByte();
            break;

          case 127:
            EStop.thread.activate = true;
            break;
        }
      }
    } catch (IOException e) {
      Sound.buzz();
      System.out.println("Comm dropped to " + name);
    }
  }