question with controlling motor

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

question with controlling motor

Postby rubberduck » Thu Oct 03, 2013 1:40 pm

today i started with programming my new nxt (because my old was defect)

i want to programm something, where i can controll the motor with the left, the orange and the right key.

my code is here:

Code: Select all
import lejos.nxt.Button;
import lejos.nxt.Motor;

 
public class testMotor
{
   
   public static void main(String[] args)
   
   {
      while (true)
      {
         Button.LEFT.waitForPress();
         Motor.A.forward();
         Button.ENTER.waitForPress();
         Motor.A.stop();
         Button.RIGHT.waitForPress();
         Motor.A.backward();

      }
   }
}


the problem is that i have to press the keys in the right order.
i want that the motor rotates / stopps everytime when i press the key for that.
rubberduck
New User
 
Posts: 17
Joined: Sun Aug 18, 2013 12:22 pm

Re: question with controlling motor

Postby fuzzycow » Thu Oct 03, 2013 4:00 pm

The quick way would be something like

(pseudo-code)
Code: Select all
loop forever
   wait for any press
   check which button was pressed
   clear all pending button events
   determine to which motor this button maps to
       get motor status
       if motor is running
             stop it
       else // motor is stopped
             call the motor method per button pressed, with immeidateReturn=true


I would also add a check for escape button and use it stop all motors, and exit the app.

However this approach is not scalable. If you want application skeleton to build upon - use behavior pattern, as described in lejos tutorial.
fuzzycow
New User
 
Posts: 20
Joined: Sun Sep 08, 2013 4:33 pm

Re: question with controlling motor

Postby rubberduck » Sat Oct 05, 2013 12:03 pm

the problem is solved now

here is the code:

Code: Select all
import lejos.nxt.Button;
import lejos.nxt.Motor;

 
public class testMotor
{
   
   public static void main(String[] args)
   
   {
      while (true)
      {
         int b = Button.waitForAnyPress();
         switch(b)
         {
         case Button.ID_LEFT:
            Motor.A.forward();
            break;
         case Button.ID_RIGHT:
            Motor.A.backward();
            break;
         case Button.ID_ENTER:
            Motor.A.stop();
            break;   
         }
         
      }
   }
}
 


but i have a second problem now
i want to controll the motor with a touch-sensor
it gives no errors when compiling, but it doesn't work on the nxt.
what is wrong here
(i copied a bit code from http://www.lejos.org/nxt/nxj/tutorial/W ... hicles.htm)

Code: Select all
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;

 
public class testMotor2
{
   TouchSensor sensor1 = new TouchSensor(SensorPort.S1);
   
   public void stopRotate()
   {
      if (sensor1.isPressed())
      {
         Motor.A.stop();
      }
   }
   
   public static void main(String[] args)
   {
      Motor.A.forward();
      testMotor2 testm = new testMotor2();
      testm.stopRotate();
   }
}
 
rubberduck
New User
 
Posts: 17
Joined: Sun Aug 18, 2013 12:22 pm

Re: question with controlling motor

Postby gloomyandy » Sat Oct 05, 2013 7:05 pm

What do you expect this code to do? What does it actually do?

Looking at the code I would guess that it does pretty much nothing. Your program will start to run, set the motor to forward it will call your method with the touch sensor in it, the sensor will probably not be pressed the method will exit, the program will exit, stopping the motor probably before it has really got started.

If you want to understand what is happening with your programs, learn how to add code to your program to display things on the LCD, or even better learn how to use the leJOS remote console and how to print things to it. Information on how to do all of this is in the tutorial.

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

Re: question with controlling motor

Postby rubberduck » Sun Oct 06, 2013 4:41 pm

i want to know how to use the touchsensor, but before i want to learn basic things now
rubberduck
New User
 
Posts: 17
Joined: Sun Aug 18, 2013 12:22 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff