We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
We are trying to read the data in the .txt file which include
1 0 0 0 1549,01050
1 0 0 0 1549,08360
1 0 0 0 1549,07956
1 0 0 0 1549,07956
1 0 0 0 1549,07552
1 0 0 0 1549,03956
1 0 0 0 1549,07956
1 0 0 0 1549,08158
1 0 0 0 1549,08360
1 0 0 0 1549,07956
1 0 0 0 1549,04148
1 0 0 0 1549,07350
1 0 0 0 1549,02442
such values in it.
After doing that, what we would like to do is to compare the values just in the 5th column with 1549.05 one by one. If any decimal number in the 5th column is bigger than 1549.05, then in the processing, we may create a array or string or something(Since I am new in Processing and Arduino, I may not know true term ) like that which just includes 1s or 0s.
for example
arry=[]
for i=1
1549.05>1549,01050 => make that; arry=[1]
for i=2
1549.05<1549,08360 => make that; arry=[1 0] . . .
The following code is what I found in the web and try to modify it for my purpose.
After obtaining such an array which I called above "arry", the Processing code will send that data to Arduino and it will turn on or off the pin13 depending on which number(0 or 1) is being sent from Processing to Arduino.
The Processing code that we try to modify
import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter=0;
int i=0;
String [] subtext;
Serial myPort;
void setup() {
mySwitch=1;
myPort = new Serial(this, "COM12", 9600);
myPort.bufferUntil('\n');
}
void draw() {
if (mySwitch>0) {
readData("C:/Python32/BridgeStrain.txt");
mySwitch=0;
}
if (counter<subtext.length) {
myPort.write(subtext[counter]);
delay(500);
myPort.write('0');
delay(100);
counter++;
} else {
delay(5000);
mySwitch=1;
}
}
void readData(String myFileName) {
File file=new File(myFileName);
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader(file));
String text=null;
while ((text=br.readLine())!=null) {
subtext = splitTokens(text, ",");
}
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
if (br != null) {
br.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Arduino Code
int outPin = 13; //define outpin
float signal;
void setup() {
Serial.begin(9600);
pinMode(outPin,OUTPUT); //set output pin
}
void loop() {
while (Serial.available()>0) {
signal = Serial.parseFloat();
if(signal>1){
digitalWrite(outPin, LOW);
}
else{
digitalWrite(outPin,HIGH);
}
}
}
Your valuable comments for solving our problem are very appreciated.
Bests, Çağdaş
Answers
Please, format your code, here's how.
Thanks for your warning. I just corrected it.
when you just want to send ONE value at a time (per line in the text file), use String
valueString = "1";
when you analyze MANY lines in one go, use array
What you have to do
so you could read the entire txt in
setup()
in one go, write the arrayand in draw() send the array item by item to the arduino
Let me sent the codes again with the explanation below.
Processing Code
Arduino Code
However rest of the story is the same. I need to modify that code to be able to read a .txt file including that data such that
After that, you may not need to consider my first message. Sorry for the confusion that I may have made in your mind.
Bests, Çağdaş
Dear Chrisir,
First of all, I would like to thank you so much for your help and interest.
When I run the Processing code, it gives a warning, saying "cannot convert from String[] to String".
Also, when the cursor is over the following lines, it shows the warnings that I put one line below of each code line.
type mismatch, "java.lang.String[]" does not match with "java.lang.String"
The global variable "lenght" does not exist.
The type of the expression must be an array type but it resolved to String.
Thanks again, Çağdaş
please press ctrl-t in the IDE to auto-format your code (indents)
then post your entire code please
this
String txtFile = loadStrings("C:/Python32/BridgeStrain.txt");
must be
String[] txtFile = loadStrings("C:/Python32/BridgeStrain.txt");
Dear Chrisir,
Everything looks fine before running. However, when I run the code, it gives the screens in the attachment.
post your entire code please
as text not image
use some
println
to see what's going one.g.
println (txtFile[i]);
before the error line etc.
I did what you have said. It again gives the same warning which says ""ArrayIndexOutOfBoundsException:". Only difference is that it shows just the first line(which is 1 0 0 0 1549.01050) in the .txt file on message screen.
Bests, Çağdaş
ok
after the line with split
when you type
println(thisLine.length);
what do you get?
Are there space " " between the columns or tabs? how many space?
then you need to fix your split line please
almost there now....
In red warning line, it again says "ArrayIndexOutOfBoundsException:4". After terminating the little window, it gives:
Bests, Çağdaş
println(thisLine.length);
should give you 4 or 5, not 1
so you need to fix your split line please
String[] thisLine = split(txtFile[i] , "\t" );
\t is for tab iirc
Dear Chrisir,
Thank you so much for your help. I just tried that one
Resulting that
according to the comparison that you've made in the code. It works well. Again Thank you so much. I also have to sent this array, including [0 1 1 0 1 1 1 0 0 1 0] to the Arduino, I think, through what they call Serial Port.
Let me guess what changes I should make in Arduino Code here.
I may make the following changes on it, but I am not sure about the 2nd one.
Please let me know how we can fix that one.
the code looks ok, without the changes
the error is of course
instead of
if (signal = 1) {
you need
if (signal == 1) {
comparison is always ==
see reference
the delays are different but I guess you want that
I do not understand why it does not work. As you said, everything looks good. But, do not know why?
The coming data to Arduino is not what they called "float". They are actually 1 or 0 s. We may also need to change the line
with something else.
Also, I did what you have said in your last message related with comparison in Arduino.
Bests, Çağdaş
you get a
String
in arduinoso why parse to float?
can't signal be of type String you read a String - I fon't know the command but like
signal = Serial.getString();
then the if-clause would be:
if (signal.equals("1)) {
Maybe these forum threads might help ya out: ~O)
Dear Chriris,
For the line, you offered
gives the warning
I realized that the line was missing ". So this is why it gives the warning above. The correct one should be
Cheers
And does it work now completely?
Dear Chrisir,
Unfortunately, it does not. But as I see on the web, what they used to read the strings is "Serial.read" .
I changed the Arduino code with the following one.
Then, open "Serial Monitor" under "Tools" on Arduino window and entering 1 turn the pin13 on, and entering other than 1 turn pin13 off.
Also, as I am searching online what I realized is that we have to modify the code for the serial communication by adding the following bunch of lines(or something like that) to the Processing code Chrisir offered before.
After the addition of above lines and also the lines related with serial communication in the header, the complete Processing code looks like that:
However, it gives the following error and I could not find why it gives that?
When it is ready to running in the red stripe:
After the attempt of running:
Sincerely, Çağdaş
Hey,
you are closing the brackets of your setup-function in line 11. The rest of your code should also be inside of setup(), so try to move that closing bracket from line 11 to the bottom of your sketch.
(I did not try to run your code, because i dont have your file and an arduino right now)
void draw(){
missing in line 12
Firstly, thank you very much for your warnings and interest, Shifting curly bracket to the end of the code terminates the error it gives.
I also attached the "draw" function and last view of the code is the following.
As usual, I got the warning, saying that "NullPointerException " as also seen below.
Actually the part after the draw function, as I guess, will read from the string that we called "arry" earlier.
Please let me know did I proceed in a correct way or not?
problem is arry
you declare it before setup and then again in setup which is wrong
remove the String[] in setup()
with String [] in front you make a new array that is just visible inside setup()
bad.
Dear Chrisir,
Removing the line
causes the same error "NullPointerException" and also indicating the line
??
no, no........
don't remove the line
just remove the beginning of it:
String[]
thus avoiding that you declare
arry
anewwhat you want is referring to the one you declared before
setup()
sorry, it's taking you so long
let's hunt the beast down....
Dear Chrisir,
Sorry for my misunderstanding. I have changed the code as what you said above. Then, it works perfectly. Thank you so much again for your help.
For the purpose of showing it as a source for others, I am sending both Processing and Arduino codes here.
Working Processing code :
Working Arduino Code:
Cheers, Çağdaş
great!
It was really working. I tried it many times. But, it suddenly started giving the error(ArrayIndexOutOfBoundsException:4) below.
By the way, the data in the txt file the Processing code is the following.
Other than this, I also need to ask that how I would have to change the code to read the data with commas(,) instead of dotes(.) such that:
When it was working perfectly, I tried changing the line
with
But it has not helped at all.
1st question
when the line
txtFile[i]
is empty or has not enough tabs in it, thesplit()
result won't be as expectedwe expect it to be an array of length 5 (one item for each column within the line)
you can check this by asking
what have you changed so that the error occured?
one empty line at the end of the text file would be enough to let that error occur.....
2nd question
processing only operates with . not comma
you need to replace the comma with dot
you can replace
with
afaik
maybe you have to look it up (javadoc under String)
Dear Chrisir,
You are a great programmer.
Your indication
"one empty line at the end of the text file would be enough to let that error occur....."
was true. That was exactly what it was missing.
Also,
using the line
fixed it.
Thank you so much again again. By the way, where are you from?
I am from Germany, we have the comma in numbers instead of the decimal point as well....