1

我是编程的初学者并试图制作一个清洁机器人 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 );
}

有人可以帮忙解决这个问题吗??????

thnx 无论如何。

4

1 回答 1

1

认为循环有点错误......

基本上,我认为你需要一个标志来指示机器人应该移动,这样当你拍手时,它就会翻转标志......

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 );

或类似的东西。否则只有在有其他声音时才会移动

我还想说,我很嫉妒;)

于 2014-02-23T02:05:29.240 回答