tags:

views:

489

answers:

2

Hello everyone

I am trying to control 4 LEDs and getting analog input from 4 contacts. The program is written in java, so to gain acces to the functions of arduino, such as AnalogRead() and setting an LED to high or low, would importing the processing library let the program use those functions?

I was also wondering, if the program, will be transferred to the arduino it self, or the java program will just pull the data from the pins?

A: 

The idea with using the processing library is that you upload a standard firmware program to the arduino and then use serial comms to access arduino functions using java and processing.

This article should get you going: Arduino and Processing

So in your Java program you could read the analog value from an input and change the value on an output pin based on that reading.

Binary Nerd
+1  A: 

Update: My suggestion is to first try communicating the Arduino with Processing by youself. This is what I describe below. If you want to jump straight to controlling the Arduino directly with Processing, the link provided by Binary Nerd is your best choice to get you started.

Update 2: Try this link as well: Netbeans and Processing


Arduino code runs on the Arduino, and Processing code runs on your computer. If you want to control your Arduino through Processing, you will most likely use the serial port, and create two programs. One, on the Arduino, might receive commands and perform actions (turn LEDs on or off), or send back answers. The other, in Processing, might send the Arduino the necessary commands and process it's answers in some way.

Here is a quick example I made for one LED and one analog input. This is untested code. Follow the steps. Once this has worked, you could try using Processing directly with the Arduino in Netbeans.

Step 1. Arduino

  1. Buy an Arduino board.
  2. Download the Arduino IDE (http://www.arduino.cc)
  3. Connect the Arduino to you computer.
  4. Copy the Arduino code (below) into the Aruino IDE.
  5. Upload to the Arduino.

Step 2. Processing

  1. Download the Processing IDE.
  2. Copy the Processing code (below) into the Processing IDE.
  3. Make sure the COM port in the code is the one the Arduino is connected to.
  4. Run the Processing code.

Arduino code:

int ledPin = 13;
int analogPin = 0;
char c = 0;

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

void loop()
{
    // Wait for a character to arrive at the serial port.
    if( Serial.available() > 0 )
    {
        // Read one byte (character).
        c = Serial.read();
        switch( c )
        {
            case '1':
                // Turn LED on.
                digitalWrite( ledPin, HIGH );
                break;
            case '0':
                // Turn LED off.
                digitalWrite( ledPin, LOW );
                break;
            case 'q':
            case 'Q':
                // Send the reading from the analog pin throught the serial port.
                Serial.println( analogRead( analogPin ) );
                break;
        }
    }
}

Processing code (runs on your computer).

import processing.serial.*;

Serial serial;
String str;

void setup()
{
    size(400, 400);
    serial = new Serial(this, "COM1", 9600);    // Use the serial port connected
                                                // to your Arduino.
    while( true )
    {
        serial.write( '1' );        // Turn LED on.
        delay( 1000 );              // Wait one second
        serial.write( '0' );        // Turn LED off.
        delay( 1000 );
        serial.write( 'Q' );        // Get analog reading
        serial.bufferUntil( 10 );   // Wait for the data from the Arduino.
                                    // This captures characters until a newline
                                    // is received, the runs serialEvent()...
    }
}

void draw()
{
    background(0);
}

void serialEvent(Serial s)
{
    println( s.readString() );
}
Philippe Signoret
Thank you very much, the answer was very useful, but it makes we wonder how to handleSo if i import the processing library, i would gain acces to the functions of processing, and thus read the analog, and control the LEDs. But the arduino program, how would that be used? would it have to run simultaneously to work? Since the processing is based upon Java, i was wondering, if the syntax and fuctions, wouldn't be very alike? So the actual function, is written in java, and the calls such as analogread is a specefic processing function?
Casper Marcussen
I added some steps to my answer. Let me know if I answered you question.
Philippe Signoret
I have succesfully connected the arduino and processing, but i want to use the Netbeans IDE, so i could load the processing jar files, and the entire library, to do it in that IDE, but the functions of java, would they work together, with the processing code? And thanks alot for the code examples, they made things very clear (:
Casper Marcussen
Yes, you can. I've never done it in NetBeans, but I have in Eclipse. The idea here is that you are running Processing code in Java, not the other way around. Try the new link I added at the top.
Philippe Signoret
I read the link you provided, and thanks for it, but i still cant use the serial.read and serial.write. I imported the RXTX library, and all the files in the arduino and processing lib directory, but none of them seem to allow me to use those functions.
Casper Marcussen
I'll try it out tonight and let you know if I managed to do it.
Philippe Signoret