Problem - I'm using an example sketch as the basis (Serial Event example). There is a problem setting a msg in my function Vobcabulary(). if the msg is 2 or 3, msg 1 will never be shown correctly as seen in Test#2-4.
Test#1
Vocabulary CORRECT
start
Started Here i've started in the correct sequence
Vocabulary Key word 'start' diplays msg 1. 'end' diplays msg 2
end Msg 3 is for any other entry
ending...
Test#2
Vocabulary FAULTY
end
ending... I've started with 'end' and it displays the correct msg#2
Vocabulary the 1st recognized word 'start' now displays msg#3
start
Not in my Vocabulary
Test#3
Vocabulary FAULTY
test
Not in my Vocabulary I've started with 'test' and it
Vocabulary displays the correct msg#3
end Now the word 'start or'end' displays
Not in my Vocabulary msg#3
Test#4
Vocabulary FAULTY
test
test As in end#3, same results. I've added an extra print
Not in my Vocabulary and it shows the Second print.
Vocabulary Then on the second input on the Second print it has
end altered into an unprintable character( )
Not in my Vocabulary (Note the unprintable character)
Problem - The code only runs for 2 inputs loops and as you there are no counters. - unresolved as of yet
Here is what I've tried so far-
-switched from a Arduino Nano to a Arduino Uno.
-changed if statement detection, I've tried almost all of the String manipulations and settled on startsWith.
- i don't think that there is a memory issue. Here is the report after uploading:
Sketch uses 3690 bytes (11%) of program storage space. Maximum is 32256 bytes.
Global variables use 253 bytes (12%) of dynamic memory, leaving 1795 bytes for local variables. Maximum is 2048 bytes.
Here is my code:
/*-----------------credits---------------------
Serial Event example
created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
//-------------------------------------------------------------------------------
const int LED = 13;
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
//-------------------------------------------------------------------------------
void setup() {
// initialize serial:
Serial.begin(115200);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Vocabulary(inputString); //my Vocaulary() function
// clear the string:
inputString = "";
stringComplete = false;
}
}
//-------------------------------------------------------------------------------
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
//-------------------------------------------------------------------------------
String Vocabulary(String RxStr) {
Serial.println("Vocabulary"); //fault-finding, remove later
Serial.print(RxStr); //First print. fault-finding, remove later
if (RxStr.startsWith("start")){
Serial.print(RxStr); //Second Print. fault-finding, remove later
digitalWrite(LED, HIGH); //fault-finding, remove later
Serial.println("Started"); //msg#1
//call function
}else if (RxStr.startsWith("end")){
Serial.print(RxStr); //Second Print. fault-finding, remove later
digitalWrite(LED, LOW); //fault-finding, remove later
Serial.println("ending..."); //msg#2
//call function
}else {
Serial.print(RxStr); //Second Print. fault-finding, remove later
Serial.println("Not in my Vocabulary");//msg#3
}
RxStr = ""; //null the string
}
//-------------------------------------------------------------------------------
Any Help would be Greatly appreciated.