[adrotate banner=”7″]

How to write the simple java program and make a basic graphical user interface to show the value of tenshi ometer. So we have an Arduino here and I have a potentiometer hooked up, and so you have coarse one edge of the potentiometer goes to ground. One edge goes to five volts and then the wiper AV is the actual reading which goes to a zero. The first analog pin and so it’s a simple potentiometer with a knob on it and let’s get going all I’m. Assuming is, do you have the Arduino software installed we’re going to need to install a few items, we’re going to need to install a JDK, a Java development kit, which will let you actually make Java programs? You probably already have a JRE a runtime environment installed, but that will not that you make Java programs. So we need the JDK I’m also going to use Eclipse, which is a Java IDE to make the Java program and then we’re going to use a simple. A very simple and easy to use library for Java called serial comm and let’s go ahead and download those items. So if you bring up a web browser, you type in JDK it’ll. Take you to the Oracle website, go ahead and download the latest version. I’Ll have to accept the license and download it while that’s downloading we’re also going to need eclipse, so you go to download, download it and now that’s done money.

And finally, we name serial comma, which is a library for Java success in Java serial comma go here. If you go to downloads cereal, cambiar click on the file – and it will say it can harm your computer well, that’s because it’s a program it’s, fine, so we’re going to keep it and we’re almost done downloading. So everything is downloaded we’re going to install the JDK. First, just realized I have my duster in frame don’t need that all right, so Java is now installed. I’Ll bring up Eclipse, which is just a compressed folder and we’ll, move that folder to the desktop Eclipse. Doesn’T only have an installer just a folder of files, and so we copy to the desktop, and we can help any clips from within that folder all right, so let’s close that and let’s put the serial comm file on the desktop so alright. So now we have all the software we do let’s go ahead and bring up the Arduino software and there’s a really simple sketch. You can use that they provide you go to examples: basics, analog, green serial. We have a simple sketch that it will configure the serial port for 9600 baud, which is the most common bond rate for a serial port. It will then enter an infinite loop where it reads from the a0 pin. Excuse me and then it will write that out to the serial port and it will repeat infinitely so if we go ahead and upload that to your Arduino, all right, so it’s done uploading.

We can see here if we bring up the serial, monitor, you’ll, see values streaming out, and so, if I get this a frame as I rotate the potentiometer, those values change, so one extreme is 10 23 and the other extreme is 0. And, of course, everything in between so that’s great, but this isn’t all that useful let’s write a Java program that will perhaps show the value in a graphical user interface, or you could even send the value to a web server or do any other interesting thing with It so we’ll close out of the Arduino software let’s bring up eclipse. So if you double click on the folder double click on eclipse, we can then close the folder it’s going to ask about a workspace, which is where all of your Java programs will be placed. The default value is fine and we’ll tell it to use that from there long. So this is the equips, OEE integrated development environment. We can close the welcome screen and we’ll create a java program. If you go to file new Java project, we’ll give it a project. Name I’m, going to call it test 1 and all the other values are fine. So you click finish and we see a folder here and there’s no source code in there. Yet so, if we right click new class I’m going to call the class main and we want to have it – create the main method so check that box and then finish – and we have a empty Java program, now we’re going to use a serial port.

And there is a library that we’re using to access that and we need to add that to our Java program. So if you right click on project, if you go to build path, configure build path, go to the libraries tab, click on add external jars. A jar is a Java archive which is like the Java equivalent of a Exe that is the complete Java program and I put that file on the desktop. So if we go to the desktop we’ll see serial comm, so double click on that and click. Ok. Well now see over here we have a reference libraries menu and it will list the jar file and the stuff that contains it is cross platform, so you can run on a Windows, Linux or OSX. All the code is in a package called Jade extensions. Comm will need to import that so we can actually use it in our program. So at the very top of the file I can import J dot. Extensions and it’ll bring up a few options for you. So we have now connected our Java program to that library. Let’S create a very simple graphical user interface, so we’re going to create what’s called a drape edge, a frame which is job as a word for a window. We need to import J frames if you hover over it and then import it in that frame. We’Re going to put a what’s called a J slider, which is a little slider which you’ll see in a minute, and we need to import that and let’s configure that slider.

The arduino analog pins will give you a value from 0 to 1023 because it has a 10 bit ADC. So we want to set these sliders Max value maximum, see the documentation right here. That’S, the minimum sorry set maximum will set the maximum value that the slider will show so we’re going to set that to 10 23, because we’re gon na get values from 0 to 1023. Now we need to put that slider in the window. So if you do window dot, add slider. What did I mess up? Oh, I called it windows up here. Let’S call that window there we go so we now have put the slider in the window, went on you to size the window so that it is an appropriate size. So you can actually see the contents of the window leave that with the window dot pack and finally, we need to show the window. The window dot set visible, make it true. So we’ve created the graphical user interface right now. It doesn’t do anything but I’ll show you what it looks. Like click the play button up here. We’Ll run the program. We want to run main dot Java. We want to always save before running and you see up here. We have a very primitive window with a slider and our goal is to make that slider move when you move the potentiometer on your Arduino, so we’re going to close the window and go back to our code here so now we need to interact with the serial Port, there is a class in our serial comm library called serial comm, and one of the methods in that class will give you an array of serial ports, because your computer, even though it may not might not appear like it.

It actually has multiple serial ports. In most cases, so we’re going to do a serial, comm ports array and we’re going to call a static method called get comm ports. So now we have an array of ports that represent each serial port on your computer. We need to show them to the user. So that they can pick the appropriate port now, ideally you do it in a graphical user interface to make it really easy and convenient, but to keep this example short I’m going to do with a command line interface. So let’s pray now to the command line system. Dot out dot print line, select a port, and then now we need to now. I need to show the actual ports an easy way to loop through an array in Java is with a special type of for loop like that and what it does is you have an array called ports, and this loop will iterate through it and pull out a Port and give that to you in your loop and of course you need to give it a data type which is serial comm. So we need to print this out. We should print a number before the port, so the user can specify a number let’s create an integer make one. If we print out that number increment it and then we’re going to put a colon and a space so let’s do it period n, space and you’ll see why I want to print some don’t worry it’s kind of confusing right now and then we’re going to call Port dot get, and now you might be tempted to use, get descriptive port name but that’s actually not as useful as as you might think.

If you do get system, port name, it’ll show like com1 com2, the name you’re, probably thinking of and so that’ll print out. Our menu, so if we run the program again, you’ll see there printed out, select a port and then we have numbers and that without that’s, what the period in space is for it’s just to make it easier to understand the menu now let’s close the program. So you click the terminate button. Now we need to read from user from the user as a input. Easy way to do that with is is with a scanner. Well, my camera battery died but we’re back now. Ok, so we need to read from the command line to get the users choice meal with a scanner which is a class in Java. So we need to import that scanner. S is equal to new scanner. We need to connect it to system dot in which is basically the command line keyboard. If you want to think of it like that, and then we need to get the value of the user typed in so we’re, going to call that an integer chosen port we’re going to do s dot next it so now we know what they typed in. So they typed in either a 1 or a 2 in this particular case, and we need to get that port and so we’re going to do serial, comm, port and we’re going to do ports shows in port 1.

Now we need to do the minus 1, because the user typed in a 1 over 2, however arrays start at 0, so that will actually be a 0 or a 1 index for the array. So we now have the port, and one of the important things you need to do is set. The timeouts I’ve noticed that this isn’t, actually all that important in Linux, but in Windows it will not work. If you fail to do that, so let’s open the port and set the timeouts now you can do port dot open port. However, it returns the boolean to let you know if it succeeded or not so let’s put that in a if statement. So if that return to true, we were able to open the port, I sure could there and then, if it return false, we will print that out and for this simple program, we’re just going to exit we’re not going to do a loop to let them try Again so by now we have hopefully opened the port and we need to set the timeouts. So if we do port dot set got a bunch of options here, one of them is set comport timeouts. So double click on that timeout mode is a constant that is defined in the class. So if you do serial calm dot, we scroll down time up, we want read semi blocking so double click on that, and then we have the two timeout values we can set them to 0, which essentially means there is no timeout just keep waiting, which is what You probably wanted so now we have the port and it’s all set up, and we can do now is we can actually read from it so we’re going to use another scanner to read from the court I’m going to call it let’s, say well, let’s call it Data such we’re getting data from it for the new scanner and this time instead of using system dot in we’re, going to do port dot, get get input stream and then we need to enter a loop to print out or dwell to read out the value.

So we create a while loop I’m going to data dot has next line, and what it’s going to do is this loop is going to keep executing it’s going to wait for input from your serial port from your Arduino. Now what you can do, just as a really simple demo, is print out the values to the command line, which will be very similar to the Arduino serial monitor, and so we can then do data dot. Next line and let’s see what happens. So if we quick click play, we have our our little graphical program here, but it’s not actually going to work yet go to the command line. I happen to know that my Arduino is on comm 3, but you can always guess and check and if you guess wrong, come back later and so it said successfully open the port and then now it’s, printing out all the values. So if I move the potentiometer the values change as you would expect, okay, but that’s really boring it isn’t, really much more different than what the Arduino software provided. So let’s actually have it update that graphical interface we’re going to need to extract that number as an integer, so we’re going to do int on the column number and if you do data dot, you might be tempted to use like next int. But I’ve found that to me a little bit: buggy I’m, not sure if it’s, the serial port library or or what, but I find it works much better just to go a line at a time.

So that’s gon na return a string. But obviously we don’t want to string. We know on an integer, so we’re going to do integer, dot, parse, int and we’re going to put that in there. So now we have it as an integer, and the only issue is sometimes when you first connect to the serial port. The first reading will be corrupt, and so, if you leave it like this, you can actually get an exception where it was not able to convert the corrupted value to an integer. So we want to do is put this in a try catch block so that if it cannot convert it to an integer, it will just try again instead of crashing the whole program. So here put that on that’s on line and we’re going to do a try. Catch block. Try, then we need to catch an exception and we’re not going to do anything with it, because we don’t care to try to recover from this corrupt data. We’Re just going to ignore it, and so now in our loop. It will try, if at all possible, to update the number now let’s use that number to update that slider in our graphical program. So our slider is called slider, so we do slider dot, set value and we’re going to pass it. The number that we just read – oh yeah, so you probably should initialize not to zero. Okay, so there’s your code and let’s see if it works.

So we have our graphical program right here. It isn’t doing anything. Yet we need to tell it what comport to listen to press too and it connected to the comport. And if we now look our program as I turn on the potentiometer, the program updates so it’s a really simple example. But you should get the idea of how to go from here. If you are familiar with Java, it should be trivial and if you’re, not I’ll, probably post a few videos later on, showing how to interact with websites to, for example, upload sensor data to a website, or perhaps maybe even a database local database. You can maybe show like a line graph of values anyway, if you liked the video, let me know, if not, let me know comments down below. Hopefully this was helpful. I know that a lot of people would like to communicate between they’re doing on a computer.


[adrotate banner=”1″]

does arduino use java Video

[mam_video id=8B6j_yr9H8g]



[adrotate banner=”2″]


[mam_tag id=5512]



[adrotate banner=”3″]


does arduino use java news






[adrotate banner=”4″]



does arduino use java Social



[adrotate banner=”5″]




Last weekend we announced that we’re working on a new development environment with advanced features. Let’s take a deeper look at what is in store for the Arduino Pro IDE!

“Let us change the world by making technology accessible to everyone and put it into the hands of every student and educator.”


[adrotate banner=”6″]