javaeclipsenxt

LeJOS NXT Programming


iam a beginner in Programming and trying to make a Cleaning Robot NXT i attached ( Ultrasonic Sensor) ,and ( Sound Sensor ) the job of the Robot is that when i Clap it have to start moving Forward and when the UltraSonic Sensor sees Something on the way it must turns around and keep going Forward . The Problem is that when it turns it doesn't keep moving Forward till i clap again !!!!!

and this is the code that i wrote :

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

Can any one help solving this Problem please?????

thnx Any way .


Solution

  • The think the loop is slightly wrong...

    Basically, I think you need a flag to indicate that the bot should be moving, so that when you clap, it flips the flag...

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

    Or something similar. Otherwise, it will only move when there is another sound

    I'd also just like to say, I'm very jealous ;)