Simple in process Arduino usage example

The following java code worked for me to flash an LED on pin 13 of an arduino connected to my Linux computer.  Simply start a new project in eclipse, import the myRobotLab.jar file, (I went ahead and added the other jar files in the myRobotLab libraries folder)  and I also added the RXTX.jar and serial.jar form the Arduino IDE; Then use the code below.


import org.myrobotlab.service.Arduino;

public class MRL1  {

    public static void main(String[] args) throws InterruptedException {
        Arduino arduino = new Arduino("arduino");
        arduino.setSerialPort("/dev/ttyUSB1");
        int i=0;
        while(i<5)
        {
        arduino.digitalWrite(13,1); // turn on LED
        Thread.sleep(500);        
        arduino.digitalWrite(13,0); // turn on LED
        Thread.sleep(500);    
        i++;
        }//end while
        arduino.releaseSerialPort();
    } //end main
}//end Class
 

GroG's picture

Great example Simple1

I have not found a good code plugin for Drupal at the moment, but you might want to check out Java2HTML .  I like to use it for indentation and syntax highlighting  (see below).  Oh, and I'll mention, the .pde which needs to be loaded and running on the Arduino is here http://code.google.com/p/myrobotlab/source/browse/trunk/src/resource/arduinoSerial.pde .
Great example, Thanks !

 

 

 


package org.myrobotlab.service;

import org.myrobotlab.service.Arduino;

public class MRL1  {

    public static void main(String[] argsthrows InterruptedException {
        Arduino arduino = new Arduino("arduino");
        arduino.setSerialPort("/dev/ttyUSB1");
        int i=0;
        while(i<5)
        {
        arduino.digitalWrite(13,1)// turn on LED
        Thread.sleep(500);        
        arduino.digitalWrite(13,0)// turn on LED
        Thread.sleep(500);    
        i++;
        }//end while
        arduino.releaseSerialPort();
    //end main
}//end Class