J-M-L:
Here is some code I had written to show how to use a circular buffer to wait for a specific string or timeOutYou can test this code and it gives you 5 seconds to say ‘Hello’
#define ESPSEPRIAL Serial
// --------------------------------------
// waitForString wait max for duration ms whilst checking if endMarker string is received
// on the ESP Serial port returns a boolean stating if the marker was found
// --------------------------------------
boolean waitForString(const char * endMarker, unsigned long duration)
{
int localBufferSize = strlen(endMarker); // démo Use of a circular buffer
char localBuffer[localBufferSize];
int index = 0;
boolean endMarkerFound = false;
unsigned long currentTime;
memset(localBuffer, '\0', localBufferSize); // clear buffer
currentTime = millis();
while (millis() - currentTime <= duration) {
if (ESPSEPRIAL.available() > 0) {
if (index == localBufferSize) index = 0;
localBuffer[index] = (uint8_t) ESPSEPRIAL.read();
endMarkerFound = true;
for (int i = 0; i < localBufferSize; i++) {
if (localBuffer[(index + 1 + i) % localBufferSize] != endMarker[i]) {
endMarkerFound = false;
break;
}
}
index++;
}
if (endMarkerFound) break;
}
return endMarkerFound;
}
void setup() {
Serial.begin(115200);
}
void loop() {
if (waitForString("Hello", 5000ul)) {
Serial.println("Good Morning!");
} else {
Serial.println("be polite please!”);
}
}
This is a solid start and the most progress I have made in months. He's what I've done just to see how it progresses and that each line was working. I used your code and added this in Void loop.
void loop() {
Serial.println("");// Clear the input field
Serial.println("ATI");//Send the 1st command to get things rolling
if (waitForString("Elm327 v1.3", 5000ul)) {Serial.println("AT AL"); }
//Using ANY part of this works, tested "Elm327 v1.3" -Works, "7 v.1" - Works, Elm326 calls the
//Else part seems to be working as intended
else {Serial.println("ATZ"); }//ATZ is reset of the shield
if (waitForString("OK", 5000ul)) {Serial.println("AT H1"); }
//Changed to AT H3(not a valid command) and it calls the Else block
else {Serial.println("ATZ"); }
if (waitForString("OK", 5000ul)) {Serial.println("AT MA"); }
else {Serial.println("ATZ"); }
delay(10000);
}
No I should leave well enough alone here since this works but it still has a lot of room for improvement since technically I only need to reset the shield if the ATI command fails. I could create several functions and call them in order, if one fails then let the else block call the previous function to move back one step. I'm not all that well versed in the arduino code and have been doing a lot of android lately but I know from previous experience the methods I would use for Android don't work in arduino.
What would be the best way to separate these IF statements so I could step back one statement at a time if something were to not respond with the expected response? I feel this is important since my code in the void loop could call one of the blocks if it encountered an error. I would rather call the last of the IF,Else statements and let it work it's way though them in reverse rather then a total reset right off.