我是编程初学者,正在尝试制作清洁机器人 NXT 我附有(超声波传感器)和(声音传感器) 机器人的工作是,当我拍手时,它必须开始向前移动,而当超声波传感器看到路上有东西时,它必须转身并继续前进。 问题是,当它转动时,它不会继续前进,直到我再次鼓掌!!!!!
这是我编写的代码:
public static void main(String[] args) {
// TODO Auto-generated method stub
TouchSensor touch = new TouchSensor(SensorPort.S2);
SoundSensor sound = new SoundSensor( SensorPort.S4 );
UltrasonicSensor sonic = new UltrasonicSensor( SensorPort.S3);
Motor.A.setSpeed( 400 );
Motor.C.setSpeed( 400 );
Button.waitForAnyPress();
int SoundValue;
SoundValue = sound.readValue();
System.out.print(SoundValue);
do {
if ( sound.readValue() > 50 ) {
// PROBLEM:
while ( sonic.getDistance() > 30 ){
Motor.B.backward();
Motor.A.backward();
Motor.C.backward();
}
{
Motor.A.rotate( -185, true );
Motor.C.rotate( 185, true );
}
};
}
while( Button.readButtons() != Button.ID_ESCAPE );
}
谁能帮忙解决这个问题吗????
无论如何,谢谢。
最佳答案
我认为循环有点错误......
基本上,我认为你需要一个标志来指示机器人应该移动,这样当你鼓掌时,它就会翻转标志......
boolean move = false;
do {
if ( sound.readValue() > 50 ) {
move = !move;
}
while ( sonic.getDistance() > 30 ){
Motor.B.backward();
Motor.A.backward();
Motor.C.backward();
}
if (move) {
Motor.A.rotate( -185, true );
Motor.C.rotate( 185, true );
}
} while( Button.readButtons() != Button.ID_ESCAPE );
或者类似的东西。否则,只有当有另一个声音时它才会移动
我也想说,我很嫉妒;)
关于java - LeJOS NXT 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963186/