Machine Gun Program Problems

Post your NXJ projects, project ideas, etc here!

Moderators: roger, imaqine, 99jonathan

pegu
Novice
Posts: 42
Joined: Tue May 26, 2009 1:00 am

Machine Gun Program Problems

Postby pegu » Tue Jun 30, 2009 2:56 am

I've written this program for a machine gun robot:

Code: Select all

import lejos.nxt.*;
import java.io.*;

public class MachineGun {

   public static void main(String [] args) {
   boolean safety = true; //Safety is on
   while(true) {
   TouchSensor TS = new TouchSensor(SensorPort.S1);
   Motor MA = Motor.A;   
      
      if (safety==true) {
         LCD.clear();
         LCD.drawString("Safety On", 4, 4);
         LCD.refresh();
      }
      
      if (Button.ENTER.isPressed() && safety==true) {

         safety=false;

      }
      
         if (Button.ENTER.isPressed() && safety==false) {
         
         safety=true;

      }   
      
      if (safety==false) {
         LCD.clear();
         LCD.drawString("Safety Off", 3,4);
         LCD.refresh();   
      }
      
      while (safety==false && TS.isPressed()) {
         MA.forward();
         LCD.clear();
         LCD.drawString("Firing", 5,4);
         LCD.refresh();   
         }
            
         MA.stop();
           if(Button.ESCAPE.isPressed()) {
      System.exit(0);
      }      
   }
}
   }


The safety jams occasionally. How can I fix this?

ChrisB01
Advanced Member
Posts: 189
Joined: Sat Mar 15, 2008 12:19 pm
Location: UK

Postby ChrisB01 » Tue Jun 30, 2009 9:57 am

I would try changing the order of your code:

Code: Select all

import lejos.nxt.*;
import java.io.*;

public class MachineGun {

   public static void main(String [] args) {
   boolean safety = true; //Safety is on
   TouchSensor TS = new TouchSensor(SensorPort.S1);
   Motor MA = Motor.A;   

   while(!Button.ESCAPE.isPressed) {
     
      if (Button.ENTER.isPressed() && safety==true) {

         safety=false;

      } else if (Button.ENTER.isPressed() && safety==false) {
         
         safety=true;

      }   
     
      if (safety==false && TS.isPressed()) {
         MA.forward();
         LCD.clear();
         LCD.drawString("Firing", 5,4);
         LCD.refresh();   
       } else {
           
         MA.stop();
           
      if (safety==true) {
         LCD.clear();
         LCD.drawString("Safety On", 4, 4);
         LCD.refresh();
      } else if (safety==false) {
         LCD.clear();
         LCD.drawString("Safety Off", 3,4);
         LCD.refresh();   
      }

}
   }
   }
}


I haven't tested this but I think the order now makes more sense.

Chris

User avatar
gloomyandy
leJOS Team Member
Posts: 4605
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby gloomyandy » Tue Jun 30, 2009 10:17 am

Hi,
Remember that buttons can stayed pressed for a long time (in computer terms)... so work through what will happen if the button is pressed (and stays pressed) for several iterations of your loop.... You may want to consider waiting for the button to be released once you have detected that it has been pressed....

Andy

pegu
Novice
Posts: 42
Joined: Tue May 26, 2009 1:00 am

Postby pegu » Tue Jun 30, 2009 3:16 pm

@Gloomyandy I tried that but it didn't seem to help. I'm not quite sure where to put the pause.

User avatar
gloomyandy
leJOS Team Member
Posts: 4605
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby gloomyandy » Tue Jun 30, 2009 3:42 pm

Hi,
It shouldn't be a pause... Once you have seen the button go down, you should set the safety on or off and then loop waiting until the button is no longer down...

Andy

pegu
Novice
Posts: 42
Joined: Tue May 26, 2009 1:00 am

Postby pegu » Tue Jun 30, 2009 3:44 pm

I've decided to save my self some trouble and make the right button off and the left button on. I might do what you've suggested later, but for now this'll do. I have a question about something else though. Does the MA.forward() automatically run at full speed? If not, what is top speed for MA.setSpeed()?

lawrie
leJOS Team Member
Posts: 955
Joined: Mon Feb 05, 2007 1:27 pm

Postby lawrie » Wed Jul 01, 2009 5:19 am

The default speed for a motor is 360. The top speed is about 100 times the battery voltage or 900 for a fully charged 9V battery.

ChrisB01
Advanced Member
Posts: 189
Joined: Sat Mar 15, 2008 12:19 pm
Location: UK

Postby ChrisB01 » Wed Jul 01, 2009 11:34 am

To make it pause once a button has been pressed I just add loops:

Code: Select all

import lejos.nxt.*;
import java.io.*;

public class MachineGun {

   public static void main(String [] args) {
   boolean safety = true; //Safety is on
   TouchSensor TS = new TouchSensor(SensorPort.S1);
   Motor MA = Motor.A;   

   while(!Button.ESCAPE.isPressed) {
     
      if (Button.ENTER.isPressed() && safety==true) {

         safety=false;

           while (Button.ENTER.isPressed()) {
                  // Waits until the button lifted
              }


      } else if (Button.ENTER.isPressed() && safety==false) {
         
         safety=true;

              while (Button.ENTER.isPressed()) {
                      // Waits until the button lifted
                 }

      }   
     
      if (safety==false && TS.isPressed()) {
         MA.forward();
         LCD.clear();
         LCD.drawString("Firing", 5,4);
         LCD.refresh();   
       } else {
           
         MA.stop();
           
      if (safety==true) {
         LCD.clear();
         LCD.drawString("Safety On", 4, 4);
         LCD.refresh();
      } else if (safety==false) {
         LCD.clear();
         LCD.drawString("Safety Off", 3,4);
         LCD.refresh();   
      }

}
   }
   }
}

User avatar
gloomyandy
leJOS Team Member
Posts: 4605
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby gloomyandy » Wed Jul 01, 2009 4:22 pm

Yep, that would work... If you want to be nice to other threads you may want to add a Thread.yield() into the loop...

Andy


Return to “NXJ Projects”

Who is online

Users browsing this forum: No registered users and 1 guest