Introduction: How to Write an Ardulink GUI, to Control Arduino

About: I was born in Rome in 1972. I have a degree in computer science but I am continuing to study it. My interest in this has also become my profession and I work for a IT multinational company. The most important …

In this tutorial you can read how to write, just in 10 steps, an Ardulink GUI like the one shown here. You need just five minutes to follow a video tutorial and just five minutes to read this tutorial.

Ardulink is a java open source library and/or a ready swing application able to control Arduino. Ardulink is a project of mine.

You can find more details about Ardulink here: www.ardulink.org

Step 1: Is You a Developer or an User?

Ardulink has a ready application called Ardulink Console with all swing components. So if your aren't interested in develop your own interface then you can use Ardulink Console as is.

Step 2: Ok, You Are a Developer

You can see the full five minutes video tutorial on youtube by following this link: http://youtu.be/2bwge-MrxLg
Now you can download the eclipse (kepler) workspace with the tutorial code following this link (the original tutorial):
http://www.ardulink.org/how-to-write-an-ardulink-gui-five-minutes-tutorial/

I used two Ardulink components and a simple SWING button.
  • org.zu.ardulink.gui.ConnectionPanel is able to search for serial ports connected to the Arduino and select one.
  • org.zu.ardulink.gui.PWMController is able to manage the power with modulation Arduino's pins.
  • The button handles the event for the connection.
To have a GUI like the one in this tutorial you only need to:
  • Use a JFrame (steps: 1,2 and 3)
  • Add inside a org.zu.ardulink.gui.ConnectionPanel (step 4)
  • Add a JButton (step 5)
  • Add inside a org.zu.ardulink.gui.PWMController (step 6)
  • Use a JButton to invoke the org.zu.ardulink.Link class (steps: 7, 8, 9 and 10)
The most important instruction of this tutorial is in fact:
boolean connected = Link.getDefaultInstance().connect(connectionPort, baudRate);

Using the default connection method Link class initializes a dialogue with Arduino board connected to a specific serial port with a given baud rate.
Here the source code:

package org.zu.ardulink.tutorial;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.zu.ardulink.Link;
import org.zu.ardulink.gui.ConnectionPanel;
import org.zu.ardulink.gui.PWMController;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;

public class Tutorial1 extends JFrame {

   private static final long serialVersionUID = -5884548646729927244L;

   // 2. Define the contentPane and an ardulink connection panel
   private JPanel contentPane;
   private ConnectionPanel connectionPanel;

   /**
    * Launch the application.
    */
   public static void main(String[] args) {
     EventQueue.invokeLater(new Runnable() {
       public void run() {
         try {
           // 1. Change Look and Feel
           UIManager.setLookAndFeel(NimbusLookAndFeel.class.getCanonicalName());

           Tutorial1 frame = new Tutorial1();
           frame.setVisible(true);
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
     });
   }

   /**
    * Create the frame.
    */
   public Tutorial1() {
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setBounds(100, 100, 475, 375);
     // 3. Setup the contentPane
     contentPane = new JPanel();
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
     setContentPane(contentPane);
     contentPane.setLayout(new BorderLayout(0, 0));

     // 4. Insert the defined connection Panel
     connectionPanel = new ConnectionPanel();
     connectionPanel.setLayout(null);
     contentPane.add(connectionPanel, BorderLayout.WEST);

     // 5. Insert a connection button
     JButton btnConnect = new JButton("Connect");
     // 7. Add an action listener when the connection button is pressed
     btnConnect.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         // 8. Take connection parameters
         String connectionPort = connectionPanel.getConnectionPort();
         int baudRate = Integer.parseInt(connectionPanel.getBaudRate());

         // 9. Let's go. Link it to Arduino.
         boolean connected = Link.getDefaultInstance().connect(connectionPort, baudRate);

         // 10. Just an information message
         if(connected) {
           JOptionPane.showMessageDialog(connectionPanel, "Arduino connected", "Connection Status", JOptionPane.INFORMATION_MESSAGE);
         } else {
           JOptionPane.showMessageDialog(connectionPanel, "Arduino NOT connected", "Connection Status", JOptionPane.ERROR_MESSAGE);
         }

       }
     });
     contentPane.add(btnConnect, BorderLayout.SOUTH);

     // 6. Insert an ardulink power with modulation pin controller
     PWMController powerPinPanel = new PWMController();
     powerPinPanel.setPin(11);
     contentPane.add(powerPinPanel, BorderLayout.EAST);

   }

}