Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Multiple Lines

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Group!

I want to thank you for all the help so far as it has been very helpful. Now I have my program connected to the arduino. Sending info back and forth has been trouble free. Now I have hit a minor road block. Part of my code turns and LED on and it blinks 10 times or how ever long the user chooses to make it blink. The command is issued and the LED blinks. Buttons on the Java program turns yellow. Now when the blinking is done the Arduino sends a message back to the java program saying I'm done blinking. I would like to turn the button back to normal color. How would I modify my code below to achieve that? I tried using a second IF statement to look for the phrase and change the button. That only worked for the first instance. Thanks for the help!



I tried this code however it does not do anything:

 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears you expected the "line" variable to contain the String "ABO" at some point. But that didn't happen. So perhaps that code wasn't executed when you had such a string? Or perhaps you never had such a string? You might consider putting some debugging code in to see when that code is called and what the variable contains, otherwise you're reduced to guessing.
 
Master Rancher
Posts: 4509
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is helpful when debugging code that finds an invalid value to print that  value in addition to the word: "ERROR"
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Clapham:

Here is the code on the Arduino side. As you can see the code sends the ABLINK when it starts...that is picked up fine. At the end when done blinking it sends ABO which ran from the serial monitor via the Arduino IDE it does show up. The java code isn't picking it up for some reason however it does pick up the ABLINK.

 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scott, that doesn't help at all. Your concern is why the Java program apparently isn't receiving the ABO, so you need to look at the Java program.
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the '6' parameter in the serialRead method mean?
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder: the 6 parameter is how many bytes to read from the serial....the first part works fine....the second IF statement does not....its not picking up the ABO for some reason...its picking up the ABLINK just fine
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

6 parameter is how many bytes to read from the serial......its not picking up the ABO  


What if there are only 3 bytes?  Should the control codes be the same length?
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changing the code to this:

Still doesnt do anything....now adding the updated error code spits out an error of: ERROR!! line=ABLINKABO< which is weird because in the Arduino serial monitor it prints on 2 separate lines and on my windows console it prints on one whole line. so something is amiss there.
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So your Arduino code is apparently sending a line ending after ABLINK (I say "apparently" because I can only guess that "println" acts like Java and appends a line ending), but your Java code doesn't read it.

Looks to me like that's because you read exactly 6 bytes in the Java code, i.e. only the "ABLINK" and not the line-ending code. Likewise reading exactly 6 bytes isn't so good if you expect to read "ABO". I think that using a method which read a whole line from Arduino would be more practical, but I don't know if there is such a method.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the more I think about it the more I think the problem is the code needs to continuously be looking for Data from the serial port(using a while statement instead of an IF statement??)...or maybe it already is? The other parts work fine. I push a button...the led comes on....the arduino sends back an acknowledgement and the java program reacts..either turning a button green or red....single action though. This would be a double action. Button pressed....LED blinks...when done blinking arduino sends back info to make the button go from yellow to no color etc. Double action not single actions like the others.
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you make all the command Strings the same length instead of 6 and then 3?
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder: tried that....still no go.
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code that shows what you have tried.

What values are returned by the serialRead method?  Add a print statement immediately after the read statement to print what was read.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder:

The code:



The error: ERROR!! line=ABLINK<
Second Read: ERROR!! line=ABO<
Third Read: ERROR!! line=ABOABLINK<
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You left off the printing of the contents of the values read on lines 1 and 2.  Please add those extra print statements and run it again.

line=ABOABLINK<


How did 9 characters get in the String if the read statement is only supposed to read 6?

The two Strings are of different lengths?  What happens when the two Strings have the same length?  EG "ABL" and "ABO"
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may be missing something, but what class is the arduino object?
Is there an API somewhere for it?

I'm curious what the serialRead method says it does.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder:

I'm not sure how 6 characters got in there. I do know on the Arduino Serial monitor each ABL and ABO is printed on 2 separate lines....which is what it is suppose to do so the java program can read from 2 separate lines. It's baffling because the other buttons and feedback work just fine. I added the system.out.println lines to both if statements to read the line and line2...both read the same thing one says ABL and the other says ABO
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ERROR!! line=ABLINK<  


I don't see how that print out is possible from the posted code.  The code on line 3 would pick out that value and print the message on line 4


Please copy the full contents of what is printed and paste it here so we can see it.
Also post the code.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dave Tolls:

arduino.jar is what is being used....this jar makes it easy to communicate with an arduino simplifying the read/write process works very well
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code I am currently using:


And attached is a screenshot of the output in the windows console as well as the output in the serial monitor of the arduino ide...As you can see the ABL and ABO are printed on separate lines....on the windows console it is not. Ignore the CON....its the acknowledgement of being connected to the java program
JAVA-ERROR.jpg
[Thumbnail for JAVA-ERROR.jpg]
JAVA-ERROR
JAVA-ARDUINO.jpg
[Thumbnail for JAVA-ARDUINO.jpg]
JAVA-ARDUINO
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please add a statement after line 1 to print the value in the line variable.
Also add a statement after line 9 to print the value of the line2 variable.

Why is are the print statements at lines 4 and line 12 saying ERROR?  Those print statements are not needed.  Add the print statements I suggested above after line 1 and line 9.

The object is to see what was read by printing it out IMMEDIATELY after it was read.

Note in the image that the < after the command is on a new line.  That means the String that was read ended with a new line character.

Also the print statement on line 14 prints line not line2
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder:

Ok I made the changes you suggested and here is the current screen shot. As you can see it looks like a newline has been sent and looks to be messing things up.
JAVA-ERROR-2.jpg
[Thumbnail for JAVA-ERROR-2.jpg]
JAVA-ERROR-2
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you also post the code that is used to create what is shown on the image in your last post?

I do not see where the values that were read are being printed immediately after they were read.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder:

here is the code used to generate the last image:

 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See comments I added:

Can you copy the contents of the console and paste it here so parts of its contents can be included in a response?

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

About 5 lines from the end of the image shows that two Strings with an intervening new line character:
ABO
ABL
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder:

Error...to say something is wrong as the code isnt working as it should...and line is just stating what the line is...no bearing on what teh main issue is
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

something is wrong as the code isnt working as it should


The idea of debugging is to see what the code is doing.  Well commented print outs can make it easier to see what happens when the code is executed.
The current print outs make it harder to see what is happening and the order it is happening in.
Can you fix the print outs to shown that?
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C:\Users\Scott\JARD>java GUI
COM STATUS: STARTING ARDUINO TEST PROGRAM ON JAVA!!
COM STATUS: CONNECTED!!
ARD STATUS: CONNECTED!!
COM STATUS: ALL LEDS BLINKING
Read line=ABL
<
ARD STATUS: ALL LEDS BLINKING
Read line2=
COM STATUS: ALL LEDS BLINKING
Read line=ABOABL
<
ARD STATUS: ALL LEDS BLINKING
Read line2=
COM STATUS: DISCONNECTED!!
COM STATUS: CLOSING MAIN PROGRAM!!

C:\Users\Scott\JARD>
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the print out from this statement shown in the code?
I do not see it in the last post.


Where is the ending < for this print out shown in  your post?


Read line2=



Where do the other printed values come from that are shown?  The posted code does not have print statements for what is shown.


Observations from the posted output:


Read line=ABL   <<<<<<<<< Read ABL and newline
<
ARD STATUS: ALL LEDS BLINKING
Read line2=     <<<<<<<<<<<<< Read an empty String
COM STATUS: ALL LEDS BLINKING
Read line=ABOABL    <<<<<<<<<< Read ABOABL and newline ??? Why isn't there a newline between ABO and ABL?
<
ARD STATUS: ALL LEDS BLINKING
Read line2=     <<<<<<<<<< Read an empty String

 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm Radder:

So It just clicked. The line2 will always be blank because it has nothing to read from the serial input. ABL gets sent first....the leds blink 10 times....once done blinking the ABO gets sent. How the code is written right now BOTH IF statements get read on the initial JButton push at the SAME time. The second IF statement needs to be invoked AFTER the LEDS blink 10 times....so some sort of timing issue has to be inserted so one IF statement gets read....the time passes the second IF statement gets read. Or am I completely wrong in my thinking?
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

some sort of timing issue  


How does the readSerial method work?
Does it block waiting for data  (if so how much data is needed for it to return that data?)
or does it return the data it has collected up to the time of the read?  No blocking
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java: button press sends command to Arduino - serial write
Arduino: reads command, executes command, sends back acknowledgement - serial send Java
Java: Java reads arduino feedback - serial read - executes Java command

I think if Arduino sends 2 commands back it sends one...java has to read one, Arduino sends another - Java pause then read another

I think that is how it needs to be in this case
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java reads arduino feedback


What happens if there is nothing for java to read when it tries to read?  What if arduino hasn't sent anything before java tries to read?
Will java wait until there is something available to read (block)
or will it read an empty String?
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:I may be missing something, but what class is the arduino object?
Is there an API somewhere for it?

I'm curious what the serialRead method says it does.


Can you answer Dave's questions?  Where is the API doc for the classes you are using?
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dave Tolls: here is the info on using arduino.jar

https://github.com/HirdayGupta/Java-Arduino-Communication-Library/wiki/Instructions.
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String serialRead(int limit) - returns a string containing as many readings as the value of limit. recommended for reading. Please note that this function is implemented in a SEMI_BLOCKING manner. This means that this function will NOT wait for as many characters as specified in the limit. Instead, think of this function as one that reads AT LEAST one and AT MOST limit number of characters from the serial buffer. The SEMI_BLOCKING behaviour ensures that the thread waits for at least one character, but not for all of them.


Note: serialRead takes an int value.  The int value of '6' is 54 and '3' is 51

Use 6 not '6' to read 6 characters and 3 not '3' to read 3

The doc says that the method will read at least 1 character.  But your print outs show that some times it read no characters for line2
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think one important thing to take away from the API text which Norm just posted is this: The Java code listening to the Arduino does not know when the Arduino is going to send data. And not only that, it doesn't know how much data it is going to send, or how fast it's going to send it, and so on.

What the Java code really wants to do (at least in your application) is to read lines of text as they become available. And the serialRead method is not powerful enough for that. Sure, it can be used to wait for text but it doesn't do anything in terms of splitting and lumping that text into lines. Now, not every application would want that but yours does, and that would be a common requirement.

So it would be nice if the writers of this communication code had written an implementation of the InputStream class which used the serialRead method under the covers to get data from the Arduino and collect it together. Then you could just wrap that InputStream into a BufferedReader and read lines of text from the Arduino with no worries. Maybe they have. So I would certainly look for that and use it if you find it.

Otherwise you're going to have to write some really ugly code which loops around, reading Arduino data and buffering it as it comes in and then splitting it up into lines. And calling something else to deal with those lines. Ugly code and frustrating for somebody who isn't that skilled with Java yet.
 
Scott Eric Catalano
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Clapham: What you're saying is that I need to set it up to poll for data? The other functions which use JButtons works fine for single actions: Button Pressed..sends command to arduino...arduino sends back and acknowledgement...Java code interprets the acknowledgement and changes the color of the Button and so on. Single actions work...double actions do not work and that requires "polling" of data...or am I missing something?
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Eric Catalano wrote:@Paul Clapham: What you're saying is that I need to set it up to poll for data?



No, I'm just suggesting that based on what I've seen so far of your application. If you've got Arduino working as a server to which you send commands and receive responses then polling wouldn't be necessary. But it seems like that isn't entirely the case?
reply
    Bookmark Topic Watch Topic
  • New Topic