Search code examples
javanetbeansarduinorxtxdiode

How do you light up the Arduino Mega diode using Java?


I would like to write a GUI in Java, in which there will be a button. Pressing the button will illuminate the diode connected to the Arduino. I'm using the RXTXcomm.jar library.

For now, I wrote code that displays the COM21 port because that's how my Arduino is connected to and opens it. Here's the code:

private String name;
private String portName;
private CommPortIdentifier portIdentifier = null;
private boolean staPort;
private void getPorts () throws PortInUseException {
    List <String> list = new ArrayList ();
    CommPortIdentifier serialPortId;
    Enumeration enumComm;
    enumComm = CommPortIdentifier.getPortIdentifiers ();
    while (enumComm.hasMoreElements ()) {
        serialPortId = (CommPortIdentifier) ​​enumComm.nextElement ();
        name = serialPortId.getName ();
        if ("COM21" .equals (name)) {
            if (serialPortId.isCurrentlyOwned ()) {
                System.out.println ("Port is open");
            } Else {
                serialPortId.open (name, WIDTH);
            }
        } else {
            System.out.println ("error");
        }
    }
}

I would like to ask how to now ignite a diode connected to eg pin1? What method to use? I use an Arduino Mega. I found a few posts on this subject, unfortunately no specific answer matching my problem. I will be grateful for any help, materials or links.


Solution

  • Understand that you'll need two programs to do this. The first is similar to your Java program. But the second is the program that runs on the Arduino itself.

    Here is a link that should give you an idea. The code is repeated below in case the link goes stale:

    int led = 13; // Pin 13
    
    void setup()
    {
        pinMode(led, OUTPUT); // Set pin 13 as digital out
    
        // Start up serial connection
        Serial.begin(9600); // baud rate
        Serial.flush();
    }
    
    void loop()
    {
        String input = "";
    
        // Read any serial input
        while (Serial.available() > 0)
        {
            input += (char) Serial.read(); // Read in one char at a time
            delay(5); // Delay for 5 ms so the next char has time to be received
        }
    
        if (input == "on")
        {
            digitalWrite(led, HIGH); // on
        }
        else if (input == "off")
        {
            digitalWrite(led, LOW); // off
        }
    }
    

    This is the C code that needs to run on the Arduino. In this case, as you can see, it is using pin 13. You'll need to get an Arduino development environment setup to get this part working. See the Arduino Software page for information on how to setup the Arduino IDE. That will be different from your Netbeans IDE but the concepts are similar.

    After you've got your sketch uploaded to your Arduino you'll connect to it at 9600 baud as shown in the Arduino code. Your Java code isn't setting communication parameters like baud rate so you'll need to update it for that. I found several links for setting the serial communication parameters in RXTX so take a look around.

    Good luck - it seems like alot at first but it's really not too bad.