Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JAVA to control LED light connecting to PC via USB/SerialPort/etc.? What device should I use?

I would like to write a program in JAVA which can control LED lights connecting to "something" that is connected to the computer.

JAVA program will consist of 8 toggle buttons in JFrame. The buttons are numbered from 1 to 8, and when the button with number X is pressed, the LED with number X will be turned on; when that button is clicked(toggled) again, that LED will be turned off.

Unlike microcontrollers that we have to load code into them, "something" I've mentioned just receives signals from JAVA (maybe via USB/SerialPort/...) to control lights. For example, JAVA sends 0000_0101(turns on LED no.1 and 3) to "something" via SerialPort and then "something" will 'keep' that value until a new signal is sent. Each digit of the value represents HIGH/LOW of a pin on "something" which I will connect the pin to the LED.

"Something" may be something like a device that can convert 'serial port signal' into 'binary' and keep that 'binary' in a register which has output pins that I can connect them to LEDs.

Is that possible? Is there a device like "something"? What is it? Does anyone have any suggestion? or some better ways to control lights from PC?

like image 382
Northnroro Avatar asked Mar 14 '14 10:03

Northnroro


3 Answers

I've done something similar using the BeagleBone Black running an Android port. I designed an Android application that used a custom Bluetooth Low Energy API that I created to communicate with a TI CC2541 running the BLE Stack.

What you'll need to do is write functions in C/C++ to make the necessary platform hardware calls for toggling I/O. Something like ToggleLed(led) would suffice. Then you hook up the native calls to Java using Java Native Interface (JNI). JNI creates a library that you can statically load.

Once you have created the library you can make calls to it in Java.

For instance, in my situation, I created a BLE API that talked to native C++ that would perform serial read/write to my embedded CC2541 BLE chip to command it into different states. These commands were connect/disconnect, write data, and read data. The BLE API was written in Java and interfaced with the hardware through JNI and driver calls. I then wrote applications that used the BLE API.

like image 125
bblincoe Avatar answered Oct 24 '22 08:10

bblincoe


You can control the GPIO pins on a Raspberry Pi with File operations. See for example this: https://blogs.oracle.com/hinkmond/entry/rpi_and_java_embedded_gpio3 where an LED blinking is done by alternating the output of a pin. So the "something" is the Pi, which you can get for very little money from many different distributors.

Typically though, Java is not the language of choice for low level bit control. Especially with the Raspberry Pi, use Python instead.

like image 4
Martin Dinov Avatar answered Oct 24 '22 08:10

Martin Dinov


Arduino will be an easy and economical choice. Which comes with a large community support.

like image 1
Manu Viswam Avatar answered Oct 24 '22 08:10

Manu Viswam