[ev3 x Java] Intelligent block button

This article is for anyone who wants to work with ev3 in Java. This time, I would like to perform various operations using the intelligent block button.

table of contents

  1. What to prepare
  2. Program using Button class
  3. Program using the Key interface

0. What to prepare

◯ ev3 (tank) ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ API Documentation (It is recommended to proceed while watching this.)

1. Program using Button class

1-0. A program that starts when a specific button is pressed

◯ It is a program that makes a sound when you press the upper button and ends the loop when you press the right button.

button00.java


import lejos.hardware.Button;
import lejos.hardware.Sound;

public class Button00
{
    public static void main(String[] args)
    {
    	while(true) {
    		//If the right button is pressed
    		if(Button.getButtons() == 8) {
                //End loop processing
                break;
            //If the upper button is pressed
    		}else if(Button.getButtons() == 1) {
    			//Make a sound
    			Sound.beep();
    		}
    	}
    }
}

** Point **: Button class

** Point **: Button type Buttons are associated with numbers.

・ Upper button ID_UP: 1 ・ Lower button ID_DOWN: 4 ・ Right button ID_RIGHT: 8 ・ Left button ID_LEFT: 16 ・ Center button ID_ENTER: 2 ・ Escape button ID_ESCAPE: 32

Details: constant-values

1-1. A program that starts when you press any button

◯ The motor will rotate when any button is pressed. However, if the pressed button is an escape button, the program will be terminated.

button01.java


import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.hardware.Button;

public class Button01
{
	public static final RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
	
    public static void main(String[] args)
    {
        while(true) {
    		//If any button is pressed
    		Button.waitForAnyPress();
    		l_a.forward();
    			
    		//If the button is pressed and it is an escape button
    		if (Button.waitForAnyPress() == Button.ID_ESCAPE) {
    			//Exit the program
                System.exit(0);
            }
    	}
    }
}

1-2. Bump program

◯ It is a program that makes a sound when you press and release the button.

button02.java


import lejos.hardware.Button;
import lejos.hardware.Sound;

public class Button02
{
	public static void main(String[] args)
    {
    		//If any button is pressed
    		Button.waitForAnyPress();
    		//If something happens
    		Button.waitForAnyEvent();
    		Sound.beep();
    }
}

1-3. Program related to long press of button

◯ This program ends the program if you press and hold the right button for 3 seconds or longer, and makes a sound if you release the right button within 3 seconds.

button03.java


import lejos.hardware.Button;
import lejos.hardware.Sound;

public class Button03
{
	public static void main(String[] args)
    {
        while(true) {
            //If you press the right button
            if(Button.waitForAnyPress() == Button.ID_RIGHT) {
                //If you release the button within 3 seconds
    			if(Button.waitForAnyEvent(3000) != 0) {
    				Sound.buzz();
                //If you press and hold the button for more than 3 seconds
    			}else {
    				System.exit(0);
    			}
    		}
    	}
    }
}

2. Program using Key interface

2-0. A program that starts when a specific button is pressed

◯ It is a program that performs different operations when each button is pressed.

import lejos.hardware.Button;
import lejos.hardware.Sound;

public class Key00
{
	public static void main(String[] args)
    {
        while(true) {
    		//If you press the button above
    		if(Button.UP.isDown()) {
                Sound.beep();
    		//If you press the button on the right
    		}else if(Button.RIGHT.isDown()) {
    			Sound.buzz();
    		//If you press the escape button
    		}else if(Button.ESCAPE.isDown()) {
    			System.exit(0);
    		}
    	}
    }
}

◯ Point: ** Key interface **


2-1. Switch-case statement program

◯ Create a class to implement the method in the KeyListener interface so that it can be called at the time of an event.

Specifically, it should rotate forward only while the upper button is pressed, and rotate negatively only while the lower button is pressed. Then press the escape key to exit the program.

import lejos.hardware.Button;
import lejos.hardware.Key;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.hardware.KeyListener;

public class Key01 {

    private static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
    private static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);

    public static void main(String[] args) {
    	//Allows methods to be called when an event occurs
        Button.UP.addKeyListener(new ButtonEvent(l_a,l_b));
        Button.DOWN.addKeyListener(new ButtonEvent(l_a,l_b));
        Button.ESCAPE.addKeyListener(new ButtonEvent(l_a,l_b));
        while (true) {}
    }
}


class ButtonEvent implements KeyListener{ //Create a class to implement event handling
	//Declare a variable
    private RegulatedMotor lmotor_a;
    private RegulatedMotor lmotor_b;

    //Set the constructor
    public ButtonEvent(RegulatedMotor Motor_A,RegulatedMotor Motor_B) {
    	lmotor_a = Motor_A;
    	lmotor_b = Motor_B;
    }

    //Customize the method
    @Override
    public void keyPressed(Key k) { //Method called when the button is pressed
        switch (k.getId()){
        case Button.ID_UP:
             lmotor_a.forward();
             lmotor_b.forward();
             break;
        case Button.ID_DOWN:
             lmotor_a.backward();
             lmotor_b.backward();
             break;
        case Button.ID_ESCAPE:
             lmotor_a.stop(true);
             lmotor_b.stop();
             System.exit(0);
        }
    }

    //Customize the method
    @Override
    public void keyReleased(Key k) { //Method called when the button is released
    	switch (k.getId()){
        case Button.ID_UP:
             lmotor_a.stop(true);
             lmotor_b.stop();
             break;
        case Button.ID_DOWN:
             lmotor_a.stop(true);
             lmotor_b.stop();
             break;
        }
    }
}

◯ Point: switch-case statement Reference: Summary of how to use switch-case statements

Finally

Thank you for reading! !! Next time, I would like to write about touch sensors!

I want to make a better article ◯ This is easier to understand ◯ This is difficult to understand ◯ This is wrong ◯ I want you to explain more here We appreciate your opinions and suggestions.

Recommended Posts

[ev3 x Java] Intelligent block button
[ev3 x Java] Single motor control
Selenium x Java