20

I am looking to turn an LED on and off with a Java program. I did the project in C# in roughly 5 minutes, but it seems to be somewhat more challenging in Java. I had the Arduino wait for a 1 or 0 to be written to the COM port and would change the LED based on that. The code I am using for the Arduino is as follows.

int LedPin = 13;
char data;

void setup()
{
    Serial.begin(9600);
    pinMode( LedPin , OUTPUT );
}

void loop()
{
    data = Serial.read();
    if (Serial.available() > 0)
    {
        if(data == '1' )
        {
            digitalWrite(LedPin,HIGH);
        }
        else if(data == '0' )
        {
            digitalWrite(LedPin,LOW);
        }
    }
    else
        if (Serial.available()<0)
        {
            digitalWrite(LedPin,HIGH);
            delay(500);
            digitalWrite(LedPin,LOW);
            delay(500);
        }
}

How would I do this with a Java application?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Dandrews
  • 598
  • 2
  • 9
  • 17
  • 1
    @Jon the Ardunio language is certainly not Java. The Arduino language is based off of [Wiring](http://wiring.org.co/) and it's implemented in C/C++. You might be thinking of [Processing](http://processing.org/). – Jeffrey Aug 19 '12 at 02:40
  • @Jeffrey, you're right. Sorry about that... – Jon Egeland Aug 19 '12 at 02:54

3 Answers3

17

You can use the JArduino (Java-Arduino) library, which provides a Java API to control your Arduino using serial port (using a USB cable, or wireless devices behaving as serial ports from a software point of view), UDP (via an ethernet shield). All the code related to communication between Java and Arduino is managed internally by the library.

Here is a Java sample to blink an LED:

public class Blink extends JArduino {

public Blink(String port) {
    super(port);
}

@Override
protected void setup() {
    // initialize the digital pin as an output.
    // Pin 13 has an LED connected on most Arduino boards:
    pinMode(DigitalPin.PIN_12, PinMode.OUTPUT);
}

@Override
protected void loop() {
    // set the LED on
    digitalWrite(DigitalPin.PIN_12, DigitalState.HIGH);
    delay(1000); // wait for a second
    // set the LED off
    digitalWrite(DigitalPin.PIN_12, DigitalState.LOW);
    delay(1000); // wait for a second
}

public static void main(String[] args) {
    String serialPort;
    if (args.length == 1) {
        serialPort = args[0];
    } else {
        serialPort = Serial4JArduino.selectSerialPort();
    }
    JArduino arduino = new Blink(serialPort);
    arduino.runArduinoProcess();
}
}

JArduino is available at: JArduino

Greenonline
  • 1,330
  • 8
  • 23
  • 31
bmorin
  • 645
  • 5
  • 12
9

You can easily build Arduino programs in Java, thanks to the excellent HaikuVM.

Here is an example:

import static processing.hardware.arduino.cores.arduino.Arduino.*;

public class Blink {
    static byte ledPin = 13;            // LED connected to digital pin 13

    public static void setup() {
        pinMode(ledPin, OUTPUT);        // sets the digital pin as output
    }

    public static void loop()           // run over and over again
    {
        digitalWrite(ledPin, HIGH);     // sets the LED on
        delay(500);                    // waits for a second
        digitalWrite(ledPin, LOW);      // sets the LED off
        delay(500);                    // waits for a second
    }
}
niutech
  • 28,923
  • 15
  • 96
  • 106
8

In order to communicate with a comm port in Java, you need some implementation of the Java Communications API. I can attest to RXTX, I have used it before to communicate with an Arduino.

Once you have your Java Communications implementation, it becomes fairly simple to communicate with an Arduino:

CommPort arduino = getArduinoPort();
arduino.getOutputStream().write(1);

public CommPort getArduinoPort() {
    Enumeration ports = CommPortIdentifier.getPortIdentifiers();
    while(ports.hasMoreElements()) {
        CommPortIdentifier identifier = (CommPortIdentifier) ports.nextElement();
        if(isArduino(identifier)) {
            return identifier.open(getClass().getName(), 2000); // 2 second timeout
        }
    }
    return null;
}

public boolean isArduino(CommPortIdentifier identifier) {
    // if you know the name of the port ahead of time you can
    // compare it here with identifier.getName(), otherwise
    // you can interface with the user like the Arduino IDE's
    // serial monitor
}

The RXTX website also has other examples [2] which you might find useful.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 1
    Syntax error on token "getOutputStream", Identifier expected after this token – Dandrews Aug 19 '12 at 02:46
  • Also I seem to be getting this a lot "gnu.io.rxtx.properties has not been detected." – Dandrews Aug 19 '12 at 02:55
  • @user1116969 You are supposed to put the first two lines in a method body. I'm not getting that error, I'm using Ubuntu 12.04 x86_64 – Jeffrey Aug 19 '12 at 03:35
  • [Code](http://pastebin.com/cncQDB87) My code... What it outputs [Output](http://imgur.com/uYLdF) – Dandrews Aug 19 '12 at 03:50
  • @user1116969 It would appear that RXTX doesn't support your operating system. What OS are you using? – Jeffrey Aug 19 '12 at 03:52
  • @user116969 The download page states that the prebuilt binaries will not work on x64 computers, and if you want the x64 binaries you need to go to [this](http://www.cloudhopper.com/opensource/rxtx/) site to get them – Jeffrey Aug 19 '12 at 04:12
  • @Dandrews I am more and more relying on JSSC instead of directly RxTx. JSSC typically manages some of the hassles one might have with RxTx (it will deploy the proper DLLs, SO, etc). See https://github.com/scream3r/java-simple-serial-connector – bmorin Oct 22 '15 at 08:58