How to read multiple lines
Hello,
I am trying to read 2 lines sent from the arduino my code is below. I am trying to create a function when the word ABLINK is received it prints out something....same with when ABO is read. Only the first IF statement works. How can I make the second one work? I am stumped. Thanks for the help!
if(n == 39) {
Serial.println("ABLINK");
for(int i = 0; i < 10; i++) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(150);
}
delay(1000);
Serial.println("ABO");
}
And my Java code is below:
String line = arduino.serialRead('6');
if(line.contains("ABLINK")) {
System.out.println("ALL LEDS BLINKING");
} else {
AnsiConsole.systemInstall();
System.out.println("ERROR!!");
}
String line2 = arduino.serialRead('4');
if(line2.contains("ABO")) {
System.out.println("ALL LEDS BLINKING OFF");
} else {
System.out.println("ERROR!! line="+ line +"<");
}
Hey @PilotinControl25, thanks for the continued engagement! And for providing both Arduino and Java code, this really helps in debugging! (I edited your question for code formatting, hope you don't mind haha).
So the first thing I would try is removing the ' (single quotation marks) from the argument to the arduino.serialRead(int limit) function in your Java code. This basically means changing the following lines:
String line = arduino.serialRead('6'); -> String line = arduino.serialRead(6);
String line = arduino.serialRead('4'); -> String line = arduino.serialRead(4);
This ensures that the argument you are passing is of data type int and NOT char. The reason this code still compiles despite the presence of ' (single quotation marks) on an argument that should be an integer is that Java implicitly typecasts chars to ints using the ASCII value of the given char. Try this first to see if this alone somehow fixes all your issues.
However, if you are planning to some additional complexity in this project, and in order to achieve a consistent, always-working solution, you're going to have to build this thing as a serial communication system (in addition to fixing the above mistake). Think about the code you're writing in terms of execution time. The first if (in the java section) works because the Arduino is immediately sending out the "ABLINK" serial communication. However, by the time the java program gets to reading line2, the arduino program is stiill going through the different delays. This means that there is yet no serial output available from the Arduino for the Java program to read as its serial input.
Now, with this in mind take a look at my updated documentation for the arduino.serialRead(int limit) function (point number 5-viii in my updated Instructions Wiki. In short, the function serialRead(int limit) is implemented as a SEMI_BLOCKING function, which means it only makes the thread wait for the first available character in the Serial Input stream. This is why you would rather implement your serial communication in a "packet" format with well-defined "packet-start" and "packet-end" characters. These characters will help your Java program know when to when to start and stop waiting for serial input. Let me know if you need any more help pursuing this idea.
This is correct. The java code needs to stop and wait for the Arduino to issue the next command in order to perform another action
In other words I need to "poll" for data? I need to remove the second if statement since it will never gather data and move it somewhere else and poll for the data? Although the Button invokes both codes to blink the leds and when done arduino sends back that its done and the java code uses that data to change the color of the button and so on.
Yes I would like more help in pursuing that idea. Thanks