利用compass sensor, 在螢幕顯示亙指北方的箭頭
import lejos.nxt.*;
import lejos.nxt.addon.CompassSensor;
import lejos.util.Delay;
import javax.microedition.lcdui.Graphics;
class Compass {
public static CompassSensor compass = new CompassSensor(SensorPort.S1);
public static int lineLength = 50; //線長度
public static int arrowLength = 15; //箭頭線長度
public static int arrowAng = 30; //箭頭線與線的角度
public static int dots = 12; //圓周點的數目
public static int delay = 150; //更新螢幕間隔
public static int offsetX;
public static int offsetY;
public static Graphics g = new Graphics();
public static double direction;
public static void main(String args[]) {
Button.ESCAPE.addButtonListener(new ButtonListener()
{
public void buttonPressed(Button b){System.exit(0);}
public void buttonReleased(Button b){}
});
while(true) {
direction = compass.getDegreesCartesian();
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), 100-getLocationX(direction, lineLength), 64-getLocationY(direction, lineLength));
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), getLocationX(direction, lineLength) + (int)(Math.cos(Math.toRadians(direction+90-arrowAng))*arrowLength), getLocationY(direction, lineLength) + (int)(Math.sin(Math.toRadians(direction+90-arrowAng))*arrowLength));
g.drawLine(getLocationX(direction, lineLength), getLocationY(direction, lineLength), getLocationX(direction, lineLength) – (int)(Math.cos(Math.toRadians(90-direction-arrowAng))*arrowLength), getLocationY(direction, lineLength) + (int)(Math.sin(Math.toRadians(90-direction-arrowAng))*arrowLength));
g.drawString("N", getLocationX(direction, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("W", getLocationX(direction+270, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+270, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("E", getLocationX(direction+90, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+90, lineLength+10)-LCD.FONT_HEIGHT/2);
g.drawString("S", getLocationX(direction+180, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+180, lineLength+10)-LCD.FONT_HEIGHT/2);
for(int i=0; i<360; i+=360/dots) g.drawLine(getLocationX(direction+i, lineLength), getLocationY(direction+i, lineLength), getLocationX(direction+i, lineLength-5), getLocationY(direction+i, lineLength-5));
Delay.msDelay(delay);
LCD.clear();
}
}
public static int getLocationX(double ang, int length) {
return 50 + (int)(Math.sin(Math.toRadians(ang))*length/2);
}
public static int getLocationY(double ang, int length) {
return 32 – (int)(Math.cos(Math.toRadians(ang))*length/2);
}
}
請問一個問題,http://lejos.sourceforge.net/forum/viewtopic.php?f=6&t=1457&p=13839&hilit=Wii+Nunchuk+Sensor&sid=a7dd4db8fe07a99e6ee83cbec6473ff4#p13839 在這個網頁中使用了Wii的雙截棍製作了一個感應器,我也製作了一個,但我使用原作者的NXC程式碼可以成功顯示數值在螢幕上NXC: http://www.mindstormsforum.de/viewtopic.php?t=3828(圖片已遺失,程式碼在中間) 但我不知道該如何把(下)網頁中的兩個代碼做結合。 http://lejos.sourceforge.net/forum/viewtopic.php?f=6&t=1457&p=13839&hilit=Wii+Nunchuk+Sensor&sid=a7dd4db8fe07a99e6ee83cbec6473ff4#p13839 請求您的協助,謝謝!