[Java] [ev3 × Java] Display, sound, LED control

2 minute read

This article is for people who want to operate ev3 with Java. This time, I would like to use intelligent blocks to display text, make sounds, and control LEDs.

table of contents

  1. Items to prepare 1 display 2 sound 3.LED Finally

0. Things to prepare

◯ ev3 (Intelligent block) ◯ PC (VS Code) ◯ bluetooth ◯ microSD ◯ API Documentation(Itisrecommendedtoproceedwhilewatchingthis.)

1.display

1-0. Display basic program ①

◯ It is a program that displays characters for 3 seconds.

display00.Java


import lejos.utility.Delay;

public class Display00
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!!");
        Delay.msDelay(3000);
    }
}

1-1. Display basic program ②

◯ It is a program that can change the place where the text is displayed and the font.

display01.java


import lejos.hardware.lcd.Font;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.ev3.LocalEV3;
import lejos.utility.Delay;


public class Display01 {

    public static void main(String[] args) {

    GraphicsLCD g = LocalEV3.get().getGraphicsLCD();
        final int SW = g.getWidth();
        final int SH = g.getHeight();
    It's a sequel.
g.drawString("Hello World", 5, 0, 0);
Delay.msDelay(3000);
g.setFont(Font.getSmallFont());
g.drawString("Programming is so fantastic!!", 2, 20, 0);
Delay.msDelay(3000);
        g.drawString("Good BYE", SW/2, SH/2, 0);
Delay.msDelay(1000);
    }
}

Point:drawString method void drawString(java.lang.String str,int x,int y,boolean inverted)


Point:Font


Point:** Interface GraphicsLCD**


1-2. Basic display program ③

◯It is a program that displays figures.

display02.java


import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.ev3.LocalEV3;
import lejos.utility.Delay;


public class Display01 {

    public static void main(String[] args) {

    GraphicsLCD g = LocalEV3.get().getGraphicsLCD();
        final int SW = g.getWidth();
        final int SH = g.getHeight();
  
        g.drawChar('A', 30, 30, 0);
        g.drawRect(0,0,100,100);
        g.fillRect(SW/2,SH/2,50,50);
        Delay.msDelay(3000);
    }
}

Point:getGraphicsLCD Get graphics access to the LCD Returns:the graphics LCD

Point:drawRect method void drawRect(int x,int y,int width,int height)


2.sound

2-0. Basic Sound Program ①

◯ A program that produces various sounds.

Sound00.java


import lejos.hardware.Sound;

public class Sound00
{
    public static void main(String[] args)
    {
        Sound.beep();
        Sound.buzz();
        Sound.systemSound(true,2);
    }
}

Point:** Sound class method**


2-1 .Basic Sound Program ②

◯ It is a program that adjusts the frequency and produces a sound.

Sound01.java


import lejos.hardware.Sound;

public class Sound01
{
    public static void main(String[] args)
    {
    Sound.playTone(262, 500); //do
    Sound.playTone(294, 500); //Le
    Sound.playTone(330, 500); // Mi
    Sound.playTone(349, 500); //Fa
    Sound.playTone(392, 500); //So
    }
}

Point:** Frequency of scale**

3.LED

3-0. LED basic program

◯ Colors can be displayed in various patterns.

led00.java


import lejos.hardware.Button;
import lejos.utility.Delay;

public class LED00 {

    public static void main(String[] args) {

    for (int i = 0;i <10;i++) {
    System.out.println(i);
    Button.LEDPattern(i);
    Delay.msDelay(3000);
    }
    }
}

Finally

Thank you for reading! !! Next time I would like to write about intelligent block buttons!

I want to make it a better article ◯ This is easier to understand ◯ It is difficult to understand here ◯This is wrong ◯I want you to explain more here Thank you for your opinions and suggestions.