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:
- The motors keep jerking when idle.
- 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. - If the Sendmessage() is used, i dont receive a message but message sent print statement gets executed and the code stops.
- The GSM module catchs network sometimes. Is it coz of power supply?
Is there any problem in the code