#眉標=Java #副標=智慧型樂高機器人與Java程式開發(5) #大標=開發NXT藍芽無線控制程式 #作者=文/圖 何致億 ============= 程式1 GetDeviceInfo.java 01. package basic; 02. import icommand.nxtcomm.*; 03. 04. public class GetDeviceInfo { 05. public static void main(String[] args) { 06. NXTCommand.setVerify(true); 07. DeviceInfo nxt = NXTCommand.getDeviceInfo(); 08. FirmwareInfo firmware = NXTCommand.getFirmwareVersion(); 09. 10. System.out.println("==============================="); 11. System.out.println("NXT名稱: "+ nxt.NXTname.trim()); 12. System.out.println("藍芽裝置位址: " 13. + nxt.bluetoothAddress); 14. System.out.println("剩餘記憶體: " + nxt.freeFlash); 15. System.out.println("NXT韌體版本: " 16. + firmware.firmwareVersion); 17. 18. NXTCommand.close(); 19. } 20. } ================ ============= 程式2 StartMotor.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.Motor; 04. 05. public class StartMotorA { 06. public static void main(String[] args) { 07. NXTCommand.setVerify(true); 08. Motor.A.forward(); 09. NXTCommand.close(); 10. } 11. } ================ ============= 程式3 StopMotor.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.Motor; 04. 05. public class StopMotorA { 06. public static void main(String[] args) { 07. NXTCommand.setVerify(true); 08. Motor.A.stop(); 09. NXTCommand.close(); 10. } 11. } ================ ============= 程式4 TestMotor1.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.Motor; 04. 05. public class TestMotor1 { 06. public static void main(String[] args) throws Exception{ 07. NXTCommand.setVerify(true); 08. Motor.A.forward(); 09. Thread.sleep(2000); 10. Motor.A.stop(); 11. NXTCommand.close(); 12. } 13. } ================ ============= 程式5 TestMotor2.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.Motor; 04. 05. public class TestMotor2 { 06. public static void main(String[] args) throws Exception{ 07. long start = 0; 08. long diff = 0; 09. NXTCommand.setVerify(true); 10. System.out.println("啟動 A 馬達..."); 11. start = System.currentTimeMillis(); 12. Motor.A.forward(); 13. Thread.sleep(2000); 14. Motor.A.stop(); 15. diff = System.currentTimeMillis()- start; 16. System.out.println("停止 A 馬達, 經過"+ diff + "毫秒"); 17. NXTCommand.close(); 18. } 19. } ================ ============= 程式6 TestTouchSensor1.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.*; 04. 05. public class TestTouchSensor1 { 06. public static void main(String[] args) throws Exception{ 07. long target = 10000; 08. long start = 0; 09. long now = 0; 10. Touch touch1 = new Touch(Sensor.S1); 11. 12. start = System.currentTimeMillis(); 13. now = start; 14. 15. while ((now - start) <= target) { 16. System.out.format("\n現在時間: %tT", now); 17. 18. if (touch1.isPressed() == true) { 19. System.out.print(", 觸碰感應器 1 被按下 "); 20. } 21. Thread.sleep(1000); 22. now=System.currentTimeMillis(); 23. } 24. NXTCommand.close(); 25. } 26. } ================ ============= 程式7 TestTouchSensor2.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.*; 04. 05. public class TestTouchSensor2 { 06. public static void main(String[] args) { 07. int counts = 0; 08. Touch touch1 = new Touch(Sensor.S1); 09. 10. while(counts < 5) { 11. if(touch1.isPressed() == true){ 12. counts++; 13. System.out.println("按下第 " + counts + " 次"); 14. } 15. } 16. NXTCommand.close(); 17. } 18. } ================ ============= 程式8 TestTouchSensor3.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.*; 04. 05. public class TestTouchSensor3 { 06. public static void main(String[] args) { 07. long target = 10000; 08. long start = 0; 09. long now = 0; 10. Touch touch1 = new Touch(Sensor.S1); 11. 12. start = System.currentTimeMillis(); 13. now = start; 14. 15. while ((now - start) <= target) { 16. now = System.currentTimeMillis(); 17. System.out.format("\n現在時間: %tT.%tL",now, now); 18. 19. if (touch1.isPressed() == true) { 20. System.out.print(", 觸碰感應器 1 被按下 "); 21. } 22. } 23. NXTCommand.close(); 24. } 25. } ================ ==<反灰>=========== 現在時間: 17:01:47.984 現在時間: 17:01:48.015 現在時間: 17:01:48.046, 觸碰感應器 1 被按下 現在時間: 17:01:48.078, 觸碰感應器 1 被按下 現在時間: 17:01:48.109, 觸碰感應器 1 被按下 現在時間: 17:01:48.140, 觸碰感應器 1 被按下 現在時間: 17:01:48.171 現在時間: 17:01:48.187 現在時間: 17:01:48.265 現在時間: 17:01:48.343 現在時間: 17:01:48.406 現在時間: 17:01:48.484 現在時間: 17:01:48.562 現在時間: 17:01:48.593 現在時間: 17:01:48.625 現在時間: 17:01:48.640 現在時間: 17:01:48.671 現在時間: 17:01:48.703 現在時間: 17:01:48.734 現在時間: 17:01:48.765 現在時間: 17:01:48.812 現在時間: 17:01:48.843 現在時間: 17:01:48.921 現在時間: 17:01:48.984 現在時間: 17:01:49.062 現在時間: 17:01:49.140 現在時間: 17:01:49.203, 觸碰感應器 1 被按下 現在時間: 17:01:49.234, 觸碰感應器 1 被按下 現在時間: 17:01:49.265, 觸碰感應器 1 被按下 現在時間: 17:01:49.296, 觸碰感應器 1 被按下 現在時間: 17:01:49.328, 觸碰感應器 1 被按下 現在時間: 17:01:49.359 現在時間: 17:01:49.375 現在時間: 17:01:49.406 現在時間: 17:01:49.437, 觸碰感應器 1 被按下 現在時間: 17:01:49.468, 觸碰感應器 1 被按下 現在時間: 17:01:49.546, 觸碰感應器 1 被按下 現在時間: 17:01:49.609 現在時間: 17:01:49.671 現在時間: 17:01:49.750 現在時間: 17:01:49.812 現在時間: 17:01:49.843 現在時間: 17:01:49.875 現在時間: 17:01:49.906 現在時間: 17:01:49.937 現在時間: 17:01:49.968 現在時間: 17:01:50.000 ================ ============= 程式9 TestLightSensor1.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.*; 04. 05. public class TestLightSensor1 { 06. public static void main(String[] args) { 07. long target = 5000; 08. long start = 0; 09. long diff = 0; 10. Light light1 = new Light(Sensor.S1); 11. NXTCommand.setVerify(true); 12. start = System.currentTimeMillis(); 13. 14. while (diff <= target) { 15. System.out.println("光感值:"+light1.getLightPercent()+ 16. "% , 經過 "+diff+" 毫秒"); 17. diff = System.currentTimeMillis() - start; 18. } 19. 20. NXTCommand.close(); 21. } 22. } ================ ============= 程式10 TestLightSensor2.java 01. package basic; 02. import icommand.nxtcomm.NXTCommand; 03. import icommand.platform.nxt.*; 04. 05. public class TestLightSensor2 { 06. public static void main(String[] args) { 07. int counts = 100; 08. long start = 0; 09. long end = 0; 10. Light light1 = new Light(Sensor.S1); 11. 12. start = System.currentTimeMillis(); 13. 14. for(int i=0; i < counts ; i++) { 15. System.out.println("光感值:"+light1.getLightPercent()+"%"); 16. } 17. 18. end = System.currentTimeMillis(); 19. 20. System.out.println("讀取 "+counts+"次"+ 21. "經過 "+(end-start)+" 毫秒"); 22. NXTCommand.close(); 23. } 24. } ================