Ev3 with joystick in Java

This is where you talk about the EV3 hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: roger, gloomyandy, skoehler

New User
Posts: 2
Joined: Wed Mar 19, 2014 10:59 am

Ev3 with joystick in Java

Postby tala1990 » Wed Mar 19, 2014 11:15 am


I am using EV3 now and everything is fine, But I want to use a joystick to control the movements of the motors.

For example when I press a button on the joystick , the corresponding motor will move.

I tried a code for a joystick code and it was working ok. But when I add the function to move a motors it did respond to that.

please anyone could help me :( ! I tried to fix it but nothing moves when I press a button on the joystick.

this is my program, the main class

Code: Select all

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import lejos.remote.ev3.RMIRegulatedMotor;
import lejos.remote.ev3.RemoteEV3;

public class EV3FirstProgram {
   public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
      RemoteEV3 ev3 = new RemoteEV3("");
      Joystick joy = new Joystick(0);
      RMIRegulatedMotor left = ev3.createRegulatedMotor("A",'L');
      while (true) {
         if( joy.getButtons() == 8 ){

         else {
         }//end of while

   }//end main
}//end class

This is the joystick class

Code: Select all

*   Copyright (C) Satoshi Konno 1999
*   File : Joystick.java

public class Joystick {

   public static final int BUTTON1 = 0x0001;
   public static final int BUTTON2 = 0x0002;
   public static final int BUTTON3 = 0x0004;
   public static final int BUTTON4 = 0x0008;

   static {
   private int joyID = 0;
   public Joystick(int id) {
      joyID = id;

   public native int getNumDevs();
   public native float getXPos(int id);
   public native float getYPos(int id);
   public native float getZPos(int id);
   public native int getButtons(int id);

   public float getXPos() {
      return getXPos(joyID);
   public float getYPos() {
      return getYPos(joyID);
   public float getZPos() {
      return getZPos(joyID);
   public int getButtons() {
      return getButtons(joyID);
   public String toString() {
      return "Joystick";

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

Re: Ev3 with joystick in Java

Postby gloomyandy » Wed Mar 19, 2014 4:52 pm

You can't issue commands to a motor after it has been closed. I don't think you should be closing the motor if no button is pressed.

New User
Posts: 2
Joined: Wed Mar 19, 2014 10:59 am

Re: Ev3 with joystick in Java

Postby tala1990 » Wed Mar 19, 2014 5:56 pm

I have tried it with and without the close()

but still no action is performed, the motor do not rotate at all

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

Re: Ev3 with joystick in Java

Postby lawrie » Thu Mar 20, 2014 1:39 pm

It might be worth running EV3Control to check that Java RMI is working OK. You can try the motors from that. You can then also look in the Console tab to see if any errors occur on the EV3.

It might be worth putting some diagnostics in your program to check that the commands are being sent.

Similar programs work fine for me.

Return to “EV3 Hardware”

Who is online

Users browsing this forum: No registered users and 0 guests