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

4

6

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?

Nimiety answered 14/3, 2014 at 10:1 Comment(0)
C
5

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.

Cilia answered 14/3, 2014 at 11:46 Comment(0)
K
3

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.

Kikelia answered 14/3, 2014 at 10:17 Comment(0)
C
1

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

Countermeasure answered 14/3, 2014 at 11:37 Comment(0)
F
0

I have done something similar when the PRINTER DB25 parallel port is available in the computer. However since the parallel port is no longer comes with PCs, laptops, You can use U"SB to PRINTER DB25 25-Pin Parallel Port Cable Adapter" as a alternative.

this parallel port has 8 output lines which can represent from 0 to 255 values in binary.

  1. write a c++ library which will pass a byte value to Parallel port output. since java cannot directly access your hardware.

  2. Using JNI you can access that library from you java code.

  3. Now connect 8 LEDs to parallel port output pins.

NOTE: 1 & 2 steps are already done for you. by the following library. http://web.archive.org/web/20080523134328/http://www.geocities.com/Juanga69/parport/

to light up only 1st led, set value 00000001b => 1 decimal

to light up only second led 00000010b => 2 decimal

to light up 4th and 8th led => 10001000b => 136 decimal

Flyn answered 14/3, 2014 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.