我是一个编程初学者,并试图制造一个清洁机器人NXT I附加(超声波传感器)和(声音传感器)机器人的工作是,当我卡普,它必须开始前进,当Ultrasonic传感器看到的东西,它必须回头,并继续前进。问题是,当它转动时,它不会继续前进,直到我再次鼓掌!
这就是我写的代码:
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 );
}有谁能帮我解决这个问题吗?
无论如何。
发布于 2014-02-23 02:05:29
认为这个循环有点错误..。
基本上,我认为你需要一个旗子来表示机器人应该移动,所以当你鼓掌的时候,它会翻转旗帜.
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 );或者类似的东西。否则,只有当有另一种声音时,它才会移动。
我也想说,我很嫉妒;)
https://stackoverflow.com/questions/21963186
复制相似问题