We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Full disclosure, I'm very new to programming and very green.
What I'm attempting to do is build a program that has a series of "code words" it searches for in a standard text file. If those words appear, some kind of function happens. Here's the basic layout.
Read a text file. Search the text file for the word "on". When "on" is found print a message. From this point forward in the text document, search for "off". When "off" is found print a message. Go back to searching for "on".
When end of the document is reached, I want the program to only search for new data and not keep rereading preexisting data. Essentially I want the program to "remember where it is" so if something updates the text document it doesn't start from the beginning again.
Here is the code I have so far. I've tested it a few times and have run into a problem. The entire file is searched for "on" before the program searches for "off." I want the program to search for "on" until an instance is found in the text file and then from that instance moving forward search for "off."
String codeword1 = "on";
String codeword2 = "off";
int mySwitch = 0;
void setup(){
}
void draw(){
String [] myfile = loadStrings("C:/Steven/Twitch/Testing Arduino Communication/String Test.txt");
if (mySwitch == 0){
for(int i = 0; i < myfile.length; i++){
String tobesearched = myfile[i];
int index1 = tobesearched.indexOf(codeword1);
if (index1 >= 0){
println("Light will be on");
delay(1000);
mySwitch = 1;
}
else {
println("String not found");
delay(1000);
}
}
}
if (mySwitch == 1){
for(int i = 0; i < myfile.length; i++){
String tobesearched = myfile[i];
int index2 = tobesearched.indexOf(codeword2);
if (index2 >= 0){
println("Light will be off");
delay(1000);
mySwitch = 0;
}
else{
println("I don't want this text to appear");
delay(1000);
}
}
}
}
The text file I used to test this program looked like this with each word on its own line in the text file:
Hello
There
on
RandomEntry
off
And the outputs of the programs were:
String not found.
String not found.
Light will be on.
String not found.
String not found.
I don't want this text to appear.
I don't want this text to appear.
I don't want this text to appear.
I don't want this text to appear.
Light will be off.
And then looped back to the beginning. Thanks in advance for any help.
Answers
You need to have only 1 for loop, if you want to go through the file only once.
if (mySwitch == 0)
should be inside for loop, so as you find one word it would continue to search for another. I hope you got this logic:Using
delay
by the way is not a good thing, as it makes entire program to sleep. You can read about proper timing in processing here: https://forum.processing.org/two/discussion/8084/how-do-i-display-a-message-for-a-few-seconds#latestThanks for the help Ater. I feel like I understand this a little better now. I made some changes to the code and added comments to reflect what each line is doing. I now have the following code.
NOTE: I know I haven't fixed delay() yet, still looking through that thread you gave me.
Now that I have the code working for strings that only have one value on each line I have a new question. The ultimate purpose of this code is to read text files that have more than one word on each line for these "code words."
Is there a way to have the program look for "on" and then "off" in a single line of the array?