GSM sms switching

Guys I am trying to make something that turns an led on when I send "0" to a gsm module connected to my arduino nano. But the code doesn't seem to work. Please help. I m using sim900 module. And I can successfully see the msg on the serial monitor.

My code is:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
char incomingByte; // for incoming serial data
int msg;

void setup()
{

Serial.begin(9600);
mySerial.begin(2400); // opens serial port, sets data rate to 9600 bps
pinMode(13, OUTPUT);
mySerial.println("AT+CMGD=255,4");

}

void loop() {

// send data only when you receive data:
while (mySerial.available() > 0)
{
// read the incoming byte:
incomingByte = mySerial.read();

// say what you got:

Serial.println(incomingByte);

if(mySerial.available()>0)
{
delay(1000);
msg = mySerial.read();
Serial.print(msg);
if (msg == '0')
{
digitalWrite(13, HIGH);
}
}
}

}

How to use this forum

Code tags.

Sorry I didn't get you.

Is the msg variable an int or a char ? There seems to be some confusion. It is declared as an int then later you check to see if it is equal to '0'

Nick means please put code tags round your code as described in the link he gave you. It makes code easier to read and prevents it being scrambled when rendered as HTML.

YA sorry i was trying so by mistake i have put it as int. It was originally a char.

meet4108:
Sorry I didn't get you.

Did you read the link I gave you?

        while (mySerial.available() > 0)
        {
                // read the incoming byte:
                incomingByte = mySerial.read();

                // say what you got:
             
                Serial.println(incomingByte);
               
        if(mySerial.available()>0)
        {
          delay(1000);
        msg = mySerial.read();
        Serial.print(msg);
         if (msg == '0')
        {
            digitalWrite(13, HIGH);
        }
        }
        }

You're formatting sucks. Use Tools + Auto Format in the future before posting code.

You are reading a character from the phone port, and printing it. Then, you are reading another character, printing it, and acting on it. Why? Why are you reading and discarding every other character (or more)?

Why are you printing stuff to the serial port with no clue as to what that data is, or where it came from?

What ARE you seeing in the serial port? Don't you think it would help if WE saw it, too?