Sms Controlled Servo Motor | Code stuck in sending STATE block

Hello Everyone,

I need a small help.
I have a project where I need to make a gsm controlled servo motor.
Program has three stages:
ON should turn on the servo motor in nonstop fashion.
OFF should turn off the servo motor.
STATE should send the current state of servo motor and resume/redirect the flow to ON state or OFF state based on current state.

I am able to accomplish the ON and OFF state but I am stuck in STATE part.

THE REQUIREMENT THAT I FAIL TO ACHIEVE:
If state is ON the flow should reply with ON as sms and the flow should go back to ON bracket instead of exiting.
If state is OFF the flow should reply with OFF as sms and the flow should go back to OFF bracket instead of exiting.
If state is OFF due to first run it should be the same as that when State is OFF (as above)

However, when i send the STATE sms the code kind of stops executing. I am attaching the code below. Could someone advice the correct part of the code.
(I am kind of newbie with basic programming skillset and trying to experiment different strategies using google. Appreciate if someone can help me with correct code of STATE.)
MY CODE BELOW:


// Include Software Serial library to communicate with GSM
#include <SoftwareSerial.h>
#include <Servo.h>

// Configure software serial port
SoftwareSerial SIM900(10, 11);

// Variable to store text message
String textMessage;
String flag;
String flag1 = "startup";

// Create a variable to store Motor state
String motorState = "HIGH";

// Relay connected to pin 12
const int relay = 2;

// Servo related parameters
int angle = 150;
int servoPin = 5;
Servo servo;

void setup() {
// Automatically turn on the shield
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);

// Set relay as OUTPUT
pinMode(relay, OUTPUT);

// By default the relay is off
digitalWrite(relay, HIGH);

// Initializing serial commmunication
Serial.begin(19200);
SIM900.begin(19200);

// Give time to your GSM shield log on to network
delay(5000);
Serial.print("SIM900 ready...");

// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}

void loop(){
if(SIM900.available()>0)
{
textMessage = SIM900.readString();
Serial.print(textMessage);
flag = textMessage;
Serial.println("Relay set to onetime");
delay(10);
}
TURNON:
if(textMessage.indexOf("ON")>=0)
{
// Turn on relay and save current state
digitalWrite(relay, LOW);
motorState = "on";
flag1 = "dupon";
Serial.println("Relay set to ON");
if(flag = textMessage)
{
{
// 58 is the deep angle press
for(angle = 86; angle < 180; angle++)
{
servo.write(angle);
delay(20);
}

              delay (3000);

             // 58 is the deep angle press
             for(angle = 180; angle > 86; angle--)    
                {                                
                    servo.write(angle);          
                     delay(20);      
                }
               }
             }

}
TURNOFF:
if(textMessage.indexOf("OFF")>=0)
{
// Turn off relay and save current state
digitalWrite(relay, HIGH);
motorState = "off";
Serial.println("Relay set to OFF");
flag1 = "dupoff";
textMessage = "";
}
if(textMessage.indexOf("STATE")>=0){
Serial.println("Motor state resquest");
SIM900.println("AT+CMGF=1"); // Replace x with mobile number
delay(1000);
SIM900.println("AT+CMGS= "xxxxxxxxxx"\r"); // Replace * with mobile number
delay(1000);
SIM900.println("Motor is "+ motorState);// The SMS text you want to send
delay(100);
SIM900.println((char)26);// ASCII code of CTRL+Z
if(flag1 = "original");
{
textMessage = "OFF";
goto TURNOFF;
}
if (flag1 == "dupon" && flag == "ON");
{
textMessage = "ON";
goto TURNON;
}
if (flag1 == "dupoff" && flag == "OFF");
{
textMessage = "OFF";
goto TURNOFF;
}
}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

to help yourself, yo might find else if works better in your case.

Then some well placed Serial.print() will help you see where the code gets stuck.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.