One of the most difficult and frustrating parts of working with Arduino devices is that there is no easy visual interface besides LCD screens. Working with Java though, creating basic interfaces to control Arduinos is fairly straightforward if you’re comfortable working with Java. I used the Eclipse IDE and jSerialComm external library created by fazecast which helps utilize the SerialPorts with Java. I also used WindowBuilder with Eclipse to create the GUI which helps alleviates many of the pains of creating Java graphic user interfaces. Below I have created a guide to help Arduino enthusiasts get off and running with a Java GUI.
We need to open up the serial port on the Arduino and also set up the speed at which it will communicate at. This speed is called the baud rate. You will want to make sure the Java code and Arduino code have the same baud rate so they speak properly. I am using a Arduino Uno which has an LED on pin #13 so you will need to set the pinMode for #13. If you’re using a different Arduino you may have to change the pin to utilize the on-board LED.
After getting the setup() portion completed, we will now start with the loop(). The items being passed over from Java are characters so we will want to create a character variable for the input. Initialize the input variable to reading from the serial port. Then you will need if loops or a switch statement for what the Arduino should do based on the character input value. I have assigned 1 to be sent from Java to turn the LED on, 2 to be sent from Java to have the LED flash 10 times, and a 3 will shut the led off.
An important note about the blink portion and why I didn’t use a while loop. If the Arduino gets into a while loop it can be difficult to get the Arduino to respond again from the Java interface. The Uno only has 1 serial port so I try to write the Arduino Uno to have specific actions that will stop.. That way it should always respond as it may ignore commands from the GUI if it is in a while loop.
Link to Arduino code: Arduino Code
Purchase an Arduino: Arduino Uno
WindowBuilder, seen in the photo above, in Eclipse helps to create GUI interfaces quickly and easily. There are many tutorials regarding using WindowBuilder although it may take some time to learn how to use. You will want to import the external jar file jSerialComm.jar into your resources as it makes serial communication much easier.
Under the fields I have created variables that will be used throughout the program. I have tried to create variables with names that should help identify what they are/do and have also commented the Java code explaining what each section/method does. If you have questions or have problems getting this to run please let me know.
I decided to keep this one simple and then build off of it for future programs that are more complicated.
Link to Java code: Java Code