When checking value of smsvalid you use assignment operator = instead of comparison operator ==
else if (serialdata[pointingfinger] == '$') {
if (smsvalid == 1) {
searchcharpos = pointingfinger;
searchchartype = 2;
futstat0 = 2;
}
if (smsvalid == 0) {
stat0 = futstat0;
pointingfinger = 0;
}
}
Those AT state machines aren't easy to finetune on Arduino. I spent a lot of time debugging similar code and found out that the best way is to unit test all possible situations in visual studio or xcode. Arduino is just C++ and if you copy/paste some portions of your code to C++ IDE, after implementing few functions or classes it should work. For example, to make your code working in XCode, I had to implement some dummy Serial1 class and simplify the loop code:
class _Serial1
{
std::string buffer{"OK\r\n"};
public:
int available()
{
return (int)buffer.size();
}
char read()
{
char c = buffer[0];
buffer.erase(0, 1);
return c;
}
} Serial1;
void loop() {
if (enabled == 1) {
if (stat0 == 0) {
if (Serial1.available () > 0) {
pointingfinger++;
serialdata[pointingfinger] = Serial1.read();
if (serialdata[pointingfinger] == '+') {
searchcharpos = pointingfinger;
searchchartype = 1;
futstat0 = 1;
}
else if (serialdata[pointingfinger] == '$') {
if (smsvalid == 1) { // ==
searchcharpos = pointingfinger;
searchchartype = 2;
futstat0 = 2;
}
if (smsvalid == 0) { // ==
stat0 = futstat0;
pointingfinger = 0;
}
}
else if (serialdata[pointingfinger] == '\n') {
stat0 = futstat0;
pointingfinger = 0;
enabled = 0;
}
else if (serialdata[pointingfinger] == '\r') {
stat0 = futstat0;
pointingfinger = 0;
enabled = 0;
}
else {
stat0 = 0;
pointingfinger = 0;
}
}
if (pointingfinger == 255) {
pointingfinger = 0;
enabled = 0;
}
}
}
}
And the compiler immediatelly pointed me at the incorrect assignments and also I was able to debug the code step by step and see what is happening inside.
In my situation I was interfacing SIMCOM900 gprs module and I made a state machine that allows me to write code like this:
virtual void Program()
{
switch (Pc())
{
case 0: _ASSERT(strlen(mApn)>0);
Stream() << "AT+CSTT=\"" << mApn << "\",\"\",\"\"\r\n";
break;
case 1: Expect("OK\r\n");
if (Expect("+CME ERROR")) { Error("CME Error when attaching apn"); Return(false);}
if (Timeout(25000)) Return(false);
break;
case 2: Stream() << "AT+CIICR\r\n"; break;
case 3: Expect("OK\r\n");
if (Timeout(15000)) Next(); break;
case 4: Stream() << "AT+CIFSR\r\n"; break;
case 5: if (ReadIpAddress()) Next();
if (Timeout(2000)) Return(false); break; // longer?
default: Return(true);
}
}
Here is a bit older version of it:
https://gist.github.com/gabonator/88b80d9c6b16d699d6f4234ebc3a2587