Interfacing GSM, servo motor, IR and Arduino

I am right now working on simple project. It is an automted gate using IR sensor, servo motor and GSM.

When the IR senses, GSM should send a message to a phone number and when it receives a call from that number it should hang and the arduino should run the motor.

#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial mySerial(2,3);
Servo servo1;
char read;
int ir;
int position=0;

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
servo1.attach(9);
pinMode(7,INPUT); //IR
}

void loop()
{
ir=digitalRead(7);
if(!ir)
{
Serial.println("Presence is detected");
Sendmessage(); // function to send sms when ir is low
Serial.println("AT+CPAS;");
Serial.println(mySerial.read());
delay(2000);

read=mySerial.read(); // when GSM receives are call, it returns RING
if(read=='G')
{
for(position = 90; position>0; position -= 2)
{
servo1.write(position);
delay(200);
}
delay(2000);;
for(position = 0; position < 90; position += 2)
{
servo1.write(position); // Move to next position
delay(200); // Short pause to allow it to move
}

}
else
Serial.println("No call received");
}
else
{Serial.println("No object detected"); delay(2000);}
}

Sendmessage()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CMGS="+91xxxxxxxxxx"\r"); // Replace x with mobile number
mySerial.println("You have a visitor");// The SMS text you want to send
delay(1000);
mySerial.write((byte)0x1A);
Serial.println("Message sent");
}

problems:

  1. The motors keep jerking when idle.
  2. If Sendmessage() is removed, the code stops at Serial.println("AT+CPAS;");
    Serial.println(mySerial.read());
    The motor doesnt run when the GSM receives a call. But instead of the servo motor code, i write a print statement, it gets executed.
  3. If the Sendmessage() is used, i dont receive a message but message sent print statement gets executed and the code stops.
  4. The GSM module catchs network sometimes. Is it coz of power supply?

Is there any problem in the code

A couple of things I've noticed, that may have a bearing on your issues:

  1. There is no return type specified for method Sendmessage. This should start with 'void' I suspect. As it stands it won't compile so I suspect this is a copy/paste error.

  2. You have several long delays around the servo. I know it sounds strange but the servo requires a command every 20ms to keep it from 'jitter' - what are you hoping the delays are doing in your code? To ensure the serial buffer is empty, use Serial.flush() which waits until the buffer has been processed.

  3. you've got the servo positioning in a loop, moving it a bit at a time, it appears. Maybe you need this but why not just move the servo to the required position in one command?

Anyway, this may help you in resolving some of the issues. BTW next time you paste your code here, do so in the accepted manner which is placing it within code blocks (use the button above which looks like: </>

//Your code then goes here!

Good luck with your project.