Another approach is use JArduino, which allows you to write Java code to directly interact with arduino! What you should do is follow the procedure from README file in JArduino page to install the firmware in your arduino board, then build a maven project that has jarduino as a maven dependency to start building Java applications that uses arduino. Since it is Java we can use JavaFX, JavaEE and all the Java APIs!
Problems
I always start writing before having something working (that's the reason why I had more than 50 draft posts on this blog). So I faced some unwanted issue and I had to figure out a few things to get this working (in fact, it is still not working, but if someone is reading this, it worked).
The error that took me a few minutes was:
java.lang.UnsatisfiedLinkError: no rxtxSerial64 in java.library.path thrown while loading gnu.io.RXTXCommDriver
This comes from the missing native rxtx library. Just download the one required for your system here and place in your JDK lib directory, in my case it is: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64
That was the major issue, a simple one to be honest.
Reading a light sensor (LDR) from Java
The Hello World jArduino tutorial is the classic blink app. I wanted something a little more interactive, so I started creating a simple Java application that reads an analog input connected to a LDR, just like described in this arduino tutorial, but with Java. Things to note about the code:
- The JArduino class mimics an Arduino sketch. You just have to extend JArduino and implement your stuff on loop and setup methods, just like I meant to this on Quick visual effects apps development with JavaFX but with Processing;
- I am using Maven and I had to add some extra repositories to my pom.xml because some dependencies are not available in maven central;
- The only thing I did to prepare my arduino to be used with jArduino was upload the jArduino sketch to it. But remember to provide the USB port where arduino is connected when creating the jArduino application
- The physical circuit is the same from the Using an LDR Sensor with Arduino: A Tutorial for Beginners tutorial, the only difference is that I am using a brazilian arduino called "garagino", which I bought in a promotion years ago! See:
And here's the code:
The result is simple the light intensity on console. If I cover the sensor, the number decrease, see:
Now let' s add something visual to it, let's use JavaFX! Next post I will share a version with JavaFX.
Nenhum comentário:
Postar um comentário