Turn on the LED on Arduino with Java - java

I installed the Java serial RXTX libraries and got my Arduino set up right. I even have the sample code up and running, but I can't seem to understand how to access the different Arduino pins in order to turn a LED for example. How can I access the pins with the OutputStream or any other way?
Do I need to send something special to the stream in order to tell it about a specific pin?

Out of the box the Arduino doesn't provide any way to control the I/O ports from the serial link.
You will need to obtain or write a program which runs on the Arduino and which listens for commands from the serial port to control the I/O pins. Because the Arduino only has a small microcontroller on it, you probably cannot write this program in Java.
Here is an example program which runs on the Arduino and reads numbers spelt out in decimal ASCII characters over the serial line, then uses those to control an RGB LED. You should be able to use this example to understand how to approach the Arduino side of the problem, and your Java RXTX code can send the integers to control this once it's working.
http://arduino.cc/en/Tutorial/ReadASCIIString

I am not sure how readable my code is, or how easily you can install it, but I wrote a small project to talk to the Arduino via RXTX. You can see my code here:
https://github.com/p90puma/SerialToIR
It should give you some ideas.
These three files should give you everything you need:
RXTX Code
Interface
Arduino Sketch

No, the Arduino itself accesses its own pins. You use Java to send serial data to the Arduino, and it then decides which pin(s)/code to use.
So in your Arduino code you start with:
Serial.begin(9600);
...
...
And to make a serial connection from Java, you can use the SerialPortEventListener interface.

All of the information in the prior answers is correct. The Arduino does not by itself, talk to a computer running Java and do anything. You need to put code onto the Arduino that will talk to your computer with some kind of protocol.
It turns out that a standard set of code for this exists. It is called Firmata. See http://firmata.org/wiki/Main_Page for the Firmata home page. See http://playground.arduino.cc/Interfacing/Processing for a discussion of Firmata and Processing.
Processing is a PC/Mac/Linux programming environment based on Java. The Processing and Arduino projects are closely related.
Try the various examples. Put Processing and the Arduino IDE on your computer. Upload Firmata onto your Arduino and the use the Processing / Firmata examples to try everything out.

Related

How to connect a PC with an RS323C interface?

I want to control a circulator (Thermo Haake DC30/DL30) which has got a RS232c interface. My first problem is that my computer has got only USB connections. Is it possible to use a RS323 to USB adapter and transmit commands and which libraries could you recommend me to use?
Also I don't know which language would be recommended. I am experienced in C# and Java. But I wanted to try out Python. I heard that it is less code to write and it would be a great opportunity to learn Python at the same time.
At the end I want a GUI in which I can set values like temperature, interval, read the current temperature or any fault messages.
I think what a USB to COM adapter does is create a virtual COM-Port on your PC, which you can then use like a regular one. Just make sure to get the right drivers, if you even need them.
I wrote a small app that required barcode scanners via RS232 in Python and I think I used PySerial, it is also definitely possible with C#, I tried that but gave up, since the application was no longer needed.

Using Java to send data to arduino sketch

I'm new into arduino programming and robotics, and I want to use Java to send data to the arduino sketch, to notify it when to use the robots motors. In particular, I have already written a working A* algorithm in Java and would really like to apply this to my arduino robot, so in my java code, when the best state is found, I want to then send data to arduino sketch to notify it what direction to go. Anyone have and ideas as to the best way? I've looked online but haven't anything that really answers this specific question.
Also, I realize I'll need to have the arduino connected via USB to use Java, but for now I'm fine with it
You are going to need JAVA USB API in order to send the data to the USB port where your Arduino is connected. From there you just read the input in your Arduino sketch and you are done.
You can also use a Java Serial Port library (like jssc).
Personally I use Processing (which uses Java) and it's quite easy to do Serial communication with the serial library it ships with.
Check out the SimpleWrite example (which includes example Processing-java code and Arduino code)

Combining Java + bluetooth + arduino

Today i came up with an idea to controle a arduino board or any other micro controller wirelessly using a java program. Im somewhat of an intermediate java programmer, meaning i can make basic applications. I know next to nothing about arduino, or the programming of microcontrollers in general, and i dont want to start to learn until i know i can link java with arduino, meaning running a program on the computer that, lets say, turn on a led. Ive done some research, and thats where i got the bluetooth from, but it seemed like the only videos were in spanish, and not much documentation about it, as far as i can see. I was just wondering how easy is it to link these two, is it possible, any links to tutorials of sorts, thanks. P.S. it doesn't have to be arduino, im just familier with that name, im just interested in controlling something physical through a java app, thanks.
Yes you can,
If you can program in java you will find arduino fairly easy but here is a arduino based tutorial with all the code your need:
http://playground.arduino.cc/Learning/Tutorial01#.UxClcvl_tFs
A majority of bluetooth modules will spit out serial data and this simply interacts with the RX/Tx (receive/transmit) inputs of the arduino. They are usually configured to standard board rates mainly 9600 to start.
All you have to do is spit a string out via bluetooth, an java based bluetooth example is below but you can also find bluetooth modules for PC that interact and install as a CDC (serial port).
Sending a string via bluetooth from a PC as client to a mobile as server.
Serial ports are a little easyier :)
http://www.java2s.com/Code/Java/Development-Class/COM-Port.htm
Cheers,
Chris

Linking an Arduino program in Java

I have written a program in Arduino to detect intrusion using an ultrasonic sensor. And when it detects intrusion, a camera must be triggered. I have written the triggering program in Java in the NetBeans IDE. But I don't know how to link the Arduino program with Java. I browsed many sites and came across the blog post Arduino and Java, but I got an error while running the program.
The error is at the line Preference.init() How can I fix this problem?
Your Java application running on some computer or board can communicate with your Arduino application via serial communication link. This can be RS232 for shorter distances (up to 30m if 9600bps is used), or RS485 for up to 1200m. You need touse some known protocol, or make your own. This link can also be wireless if needed. More complex would be to put bluetooth on both sides, if that is more convinient for some reason. Alternatively, if you have an ethernet shield for Arduino you can implement UDP or TCP messaging to notify Java application about Arduino events. Instead of sending an event only when it happens, you can also think about periodical polling from Java application.

Make a USB Device, Control It In Java

I'm thinking about making a physical controller (device?) with knobs, buttons, and LEDs. I'd like to interact with it using Java (respond to the knobs, light up LEDs, etc). The reason I mention Java is two-fold: first, I know Java well1. Second, I've written the rest of the program I need to interface with in Java (though there are ways to talk to the Java program from another language).
I would like the device to connect via USB and be (computer-)platform independent.
I haven't the slightest idea of where to start, except to start reading the Arduino website. Is this my best/only option? Is there something better suited for communicating with Java?
Note: I know that Arduino has something to do with Java (not sure what), but it seems like code must be written in a subset of C.
How would I get moving on this topic?
1 - No laughter, please.
The Arduino development environment is written in Java.
But the standard language you write a program for the Arduino platform is effectively C++.
The Arduino platform is based on an Atmel AVR chip. There is at least one Java VM for AVR chips. There are other languages available for the AVR such as Forth and BASIC (although I could only find commercial versions, so I'll if you want to find them, search for "AVR BASIC").
The Arduino uses a virtual COM port to communicate between the host computer and it. A virtual COM port emulates an old style serial line but is done with USB. You can use the Java communication API to then have a Java program running on the host computer communicate with your physical device.
For some encoders and buttons, you probably want to implement a USB HID device. If you're going to produce more than a couple of them, you'll want to do a custom board. Check out V-USB, an open-source library for making USB HID devices using Atmel microcontrollers. They have a bunch of examples of projects that use this library.
You could probably make this look like a HID joystick, using the encoders to produce X/Y axis information and having the buttons act like buttons. They you could use standard Java gaming APIs to read the joystick values.
Can you expand on your need for a custom device? It seems to me that designing hardware has a pretty high barrier to entry and that most applications I can think of would be better resolved by repurposing an existing piece of game controller hardware. If you really need new hardware, then i suggest you start by googling 'USB development kit' or
'USB development board' which will get you links like this, this and this.
As for working with USB hardware from Java, I've played around with the JUSB library a bit and it looks promising, but appears to be limited to Linux only because the Windows version of the native library half of the library hasn't been written. Alternatives can by found by googling 'HID java'.
Ok, computer-platform independant? What platforms are you targetting? That would depend on the driver and support for that said platform, what does the usb device do? Is it a mass storage device...You may have to look around and see if you can find a device driver that can talk to the device...
Hope this helps,
Best regards,
Tom.
I know for the serial port there were libraries that existed for interacting with it (rs232 library). Googling for java and USB returned several answers (the first was called jUSB). That would be the first type of thing I would be looking for.
sample for java usb connection to freescale microcontroller:
http://javausbapi.blogspot.com/

Categories

Resources