hi there
i got problem in combining two arduino programs
the first program is for motion detector …when the sensor detected any movement the arduino will send massage to the cell phone via arduino GSM shield
the second program will enable the cell phone to control the LEDs that are connected to the arduino
when i load each program it works fine but when i combine both program together it is not working fine
when combine both programs the motion detector will detect only in first time and send the message but when there is an other motion it won’t sen anything and i cannot control the LEDs (the second program function).
so the problem in combining i think
these are the programs
first program (motion detactor )
#include <GSM.h>
#include <SIM900.h>
#include <sms.h>
#include <SoftwareSerial.h>
SoftwareSerial GPRS(7, 8 ); // RX, TX pins
void setup() {
pinMode(5, INPUT); // Our PIR sensor
Serial.begin(9600); // Open serial connection at baud rate of 9600
}
void loop(){
if (digitalRead(5) == HIGH){ // PIR sensor detects motion
digitalWrite(13, HIGH); // Turn LED on.
Serial.println(“AT”); // Sends AT command to wake up gsm shield
delay(500);
Serial.println(“AT+CMGF=1”); // Puts gsm shield into SMS mode
delay(1000); // Wait a second
Serial.println(“AT+CMGS="+1xxxxxxxxxx"”); // YOUR NUMBER HERE; Creates new message to number
delay(1000);
Serial.print(“Motion Detected.”); // Message contents
delay(1000);
Serial.write(byte(26)); // (signals end of message)
delay(1000);
Serial.println(“AT+CMSS=1”); // Sends message at index of 1
digitalWrite(13, LOW); // Turn LED off
delay(250);
digitalWrite(13, HIGH); // Turn LED on.
delay(10000); // Give the gsm shield time to send the SMS
Serial.println(“AT+CMGD=1”); // Deletes message at index of 1
digitalWrite(13, LOW); // Turn LED off.
delay(250);
}
}
the second program ( control leds via cell phone)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8 );
// EN: String buffer for the GPRS shield message
// FR: Mémoire tampon de type string pour les messages du shield GPRS
String msg = String(“”);
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
// FR: Est mis à 1 quand le prochain message du shield GPRS contiendra le contenu du SMS
int SmsContentFlag = 0;
// EN: Pin of the LED to turn ON and OFF depending on the received message
// FR: Pin de la LED a allumer/éteindre en fonction du message reçu
int ledPin = 13;
// EN: Code PIN of the SIM card (if applied)
// FR: Code PIN de la carte SIM (si applicable)
String SIM_PIN_CODE = String( “XXXX” );
void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(9600); // the GPRS baud rate
// Initialize la PIN
pinMode( ledPin, OUTPUT );
digitalWrite( ledPin, LOW );
}
void loop()
{
char SerialInByte;
if(Serial.available())
{
mySerial.print((unsigned char)Serial.read());
}
else if(mySerial.available())
{
char SerialInByte;
SerialInByte = (unsigned char)mySerial.read();
// EN: Relay to Arduino IDE Monitor
// FR: Relayer l’information vers le moniteur Serie Arduino
Serial.print( SerialInByte );
// -------------------------------------------------------------------
// EN: Program also listen to the GPRS shield message.
// FR: Le programme écoute également les messages issus du GPRS Shield.
// -------------------------------------------------------------------
// EN: If the message ends with then process the message
// FR: Si le message se termine par un alors traiter le message
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
// FR: Stocké le caractère dans le buffer de message
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
// EN: Skip Line feed
// FR: Ignorer les Line Feed
}
else {
// EN: store the current character in the message string buffer
// FR: stocker le caractère dans la mémoire tampon réservé au message
msg += String(SerialInByte);
}
}
}
// EN: Make action based on the content of the SMS.
// Notice than SMS content is the result of the processing of several GPRS shield messages.
// FR: Execute une action sur base du contenu d’un SMS.
// Notez que le contenu du SMS est le résultat du traitement de plusieurs messages du shield GPRS.
void ProcessSms( String sms ){
Serial.print( “ProcessSms for [” );
Serial.print( sms );
Serial.println( “]” );
if( sms.indexOf(“on”) >= 0 ){
digitalWrite( ledPin, HIGH );
Serial.println( “LED IS ON” );
return;
}
if( sms.indexOf(“off”) >= 0 ){
digitalWrite( ledPin, LOW );
Serial.println( “LED IS OFF” );
return;
}
}
// EN: Send the SIM PIN Code to the GPRS shield
// FR: Envoyer le code PIN de la carte SIM au shield GRPS
void GprsSendPinCode(){
if( SIM_PIN_CODE.indexOf(“XXXX”)>=0 ){
Serial.println( “*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***” );
Serial.println( “*** Please, define the SIM_PIN_CODE variable . ***” );
return;
}
mySerial.print(“AT+CPIN=”);
mySerial.println( SIM_PIN_CODE );
}
// EN: Request Text Mode for SMS messaging
// FR: Demande d’utiliser le mode Text pour la gestion des messages
void GprsTextModeSMS(){
mySerial.println( “AT+CMGF=1” );
}
void GprsReadSmsStore( String SmsStorePos ){
// Serial.print( "GprsReadSmsStore for storePos " );
// Serial.println( SmsStorePos );
mySerial.print( “AT+CMGR=” );
mySerial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
// FR: efface le contenu de la mémoire tampon des messages du GPRS shield.
void ClearGprsMsg(){
msg = “”;
}
// EN: interpret the GPRS shield message and act appropiately
// FR: interprete le message du GPRS shield et agit en conséquence
void ProcessGprsMsg() {
Serial.println(“”);
Serial.print( “GPRS Message: [” );
Serial.print( msg );
Serial.println( “]” );
if( msg.indexOf( “+CPIN: SIM PIN” ) >= 0 ){
Serial.println( “*** NEED FOR SIM PIN CODE ***” );
Serial.println( “PIN CODE *** WILL BE SEND NOW” );
GprsSendPinCode();
}
if( msg.indexOf( “Call Ready” ) >= 0 ){
Serial.println( “*** GPRS Shield registered on Mobile Network ***” );
GprsTextModeSMS();
}
// EN: unsolicited message received when getting a SMS message
// FR: Message non sollicité quand un SMS arrive
if( msg.indexOf( “+CMTI” ) >= 0 ){
Serial.println( “*** SMS Received ***” );
// EN: Look for the coma in the full message (+CMTI: “SM”,6)
// In the sample, the SMS is stored at position 6
// FR: Rechercher la position de la virgule dans le message complet (+CMTI: “SM”,6)
// Dans l’exemple, le SMS est stocké à la position 6
int iPos = msg.indexOf( “,” );
String SmsStorePos = msg.substring( iPos+1 );
Serial.print( "SMS stored at " );
Serial.println( SmsStorePos );
// EN: Ask to read the SMS store
// FR: Demande de lecture du stockage SMS
GprsReadSmsStore( SmsStorePos );
}
// EN: SMS store readed through UART (result of GprsReadSmsStore request)
// FR: Lecture du stockage SMS via l’UART (résultat de la requete GprsReadSmsStore)
if( msg.indexOf( “+CMGR:” ) >= 0 ){
// EN: Next message will contains the BODY of SMS
// FR: Le prochain message contiendra le contenu du SMS
SmsContentFlag = 1;
// EN: Following lines are essentiel to not clear the flag!
// FR: Les ligne suivantes sont essentielle pour ne pas effacer le flag!
ClearGprsMsg();
return;
}
// EN: +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
// FR: le message +CMGR précédent indiquait que le message suivant du Shield GPRS
// (ce message) contient le corps du SMS
if( SmsContentFlag == 1 ){
Serial.println( “*** SMS MESSAGE CONTENT ***” );
Serial.println( msg );
Serial.println( “*** END OF SMS MESSAGE ***” );
ProcessSms( msg );
}
ClearGprsMsg();
// EN: Always clear the flag
// FR: Toujours mettre le flag à 0
SmsContentFlag = 0;
}
after combine both program
#include <SoftwareSerial.h>
SoftwareSerial gsmSerial(7, 8 );
SoftwareSerial mySerial(7, 8 );
String msg = String(“”);
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
// FR: Est mis à 1 quand le prochain message du shield GPRS contiendra le contenu du SMS
int SmsContentFlag = 0;
// EN: Pin of the LED to turn ON and OFF depending on the received message
// FR: Pin de la LED a allumer/éteindre en fonction du message reçu
int ledPin = 11;
// EN: Code PIN of the SIM card (if applied)
// FR: Code PIN de la carte SIM (si applicable)
String SIM_PIN_CODE = String( “XXXX” );
void setup()
{
gsmSerial.begin(19200);
Serial.begin(9600); //Default serial port setting for the GPRS modem is 19200bps 8-N-1
gsmSerial.print(“\r”);
pinMode( ledPin, OUTPUT );
pinMode(5,INPUT);
digitalWrite( ledPin, LOW );
}
void loop()
{
delay(10000)
if (digitalRead(5) == HIGH){ // PIR sensor detects motion
digitalWrite(11, HIGH); // Turn LED on.
//We just want to send the SMS only once, so there is nothing in this loop.
gsmSerial.begin(19200); //Default serial port setting for the GPRS modem is 19200bps 8-N-1
gsmSerial.print(“\r”);
delay(1000); //Wait for a second while the modem sends an “OK”
gsmSerial.print(“AT+CMGS=1\r”); //Because we want to send the SMS in text mode
delay(1000);
//mySerial.print(“AT+CSCA="+919032055002"\r”); //Setting for the SMS Message center number,
//delay(1000); //uncomment only if required and replace with
//the message center number obtained from
//your GSM service provider.
//Note that when specifying a tring of characters
// " is entered as "
gsmSerial.print(“AT+CMGS="+96892077117"\r”); //Start accepting the text for the message
//to be sent to the number specified.
//Replace this number with the target mobile number.
delay(1000);
gsmSerial.print(“there is an intruder\r”); //The text for the message
delay(1000);
gsmSerial.write(0x1A); //Equivalent to sending Ctrl+Z
sullieman();
}
//If we put the code for SMS here, it will be sent again and again and cost us a lot.
}
void sullieman()
{
char SerialInByte;
if(Serial.available())
{
mySerial.print((unsigned char)Serial.read());
}
else if(mySerial.available())
{
char SerialInByte;
SerialInByte = (unsigned char)mySerial.read();
// EN: Relay to Arduino IDE Monitor
// FR: Relayer l’information vers le moniteur Serie Arduino
Serial.print( SerialInByte );
// -------------------------------------------------------------------
// EN: Program also listen to the GPRS shield message.
// FR: Le programme écoute également les messages issus du GPRS Shield.
// -------------------------------------------------------------------
// EN: If the message ends with then process the message
// FR: Si le message se termine par un alors traiter le message
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
// FR: Stocké le caractère dans le buffer de message
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
// EN: Skip Line feed
// FR: Ignorer les Line Feed
}
else {
// EN: store the current character in the message string buffer
// FR: stocker le caractère dans la mémoire tampon réservé au message
msg += String(SerialInByte);
}
}
}
// EN: Make action based on the content of the SMS.
// Notice than SMS content is the result of the processing of several GPRS shield messages.
// FR: Execute une action sur base du contenu d’un SMS.
// Notez que le contenu du SMS est le résultat du traitement de plusieurs messages du shield GPRS.
void ProcessSms( String sms ){
Serial.print( “ProcessSms for [” );
Serial.print( sms );
Serial.println( “]” );
if( sms.indexOf(“on”) >= 0 ){
digitalWrite( ledPin, HIGH );
Serial.println( “LED IS ON” );
return;
}
if( sms.indexOf(“off”) >= 0 ){
digitalWrite( ledPin, LOW );
Serial.println( “LED IS OFF” );
return;
}
}
// EN: Send the SIM PIN Code to the GPRS shield
// FR: Envoyer le code PIN de la carte SIM au shield GRPS
void GprsSendPinCode(){
if( SIM_PIN_CODE.indexOf(“XXXX”)>=0 ){
Serial.println( “*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***” );
Serial.println( “*** Please, define the SIM_PIN_CODE variable . ***” );
return;
}
mySerial.print(“AT+CPIN=”);
mySerial.println( SIM_PIN_CODE );
}
// EN: Request Text Mode for SMS messaging
// FR: Demande d’utiliser le mode Text pour la gestion des messages
void GprsTextModeSMS(){
mySerial.println( “AT+CMGF=1” );
}
void GprsReadSmsStore( String SmsStorePos ){
// Serial.print( "GprsReadSmsStore for storePos " );
// Serial.println( SmsStorePos );
mySerial.print( “AT+CMGR=” );
mySerial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
// FR: efface le contenu de la mémoire tampon des messages du GPRS shield.
void ClearGprsMsg(){
msg = “”;
}
// EN: interpret the GPRS shield message and act appropiately
// FR: interprete le message du GPRS shield et agit en conséquence
void ProcessGprsMsg() {
Serial.println(“”);
Serial.print( “GPRS Message: [” );
Serial.print( msg );
Serial.println( “]” );
if( msg.indexOf( “+CPIN: SIM PIN” ) >= 0 ){
Serial.println( “*** NEED FOR SIM PIN CODE ***” );
Serial.println( “PIN CODE *** WILL BE SEND NOW” );
GprsSendPinCode();
}
if( msg.indexOf( “Call Ready” ) >= 0 ){
Serial.println( “*** GPRS Shield registered on Mobile Network ***” );
GprsTextModeSMS();
}
// EN: unsolicited message received when getting a SMS message
// FR: Message non sollicité quand un SMS arrive
if( msg.indexOf( “+CMTI” ) >= 0 ){
Serial.println( “*** SMS Received ***” );
// EN: Look for the coma in the full message (+CMTI: “SM”,6)
// In the sample, the SMS is stored at position 6
// FR: Rechercher la position de la virgule dans le message complet (+CMTI: “SM”,6)
// Dans l’exemple, le SMS est stocké à la position 6
int iPos = msg.indexOf( “,” );
String SmsStorePos = msg.substring( iPos+1 );
Serial.print( "SMS stored at " );
Serial.println( SmsStorePos );
// EN: Ask to read the SMS store
// FR: Demande de lecture du stockage SMS
GprsReadSmsStore( SmsStorePos );
}
// EN: SMS store readed through UART (result of GprsReadSmsStore request)
// FR: Lecture du stockage SMS via l’UART (résultat de la requete GprsReadSmsStore)
if( msg.indexOf( “+CMGR:” ) >= 0 ){
// EN: Next message will contains the BODY of SMS
// FR: Le prochain message contiendra le contenu du SMS
SmsContentFlag = 1;
// EN: Following lines are essentiel to not clear the flag!
// FR: Les ligne suivantes sont essentielle pour ne pas effacer le flag!
ClearGprsMsg();
return;
}
// EN: +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
// FR: le message +CMGR précédent indiquait que le message suivant du Shield GPRS
// (ce message) contient le corps du SMS
if( SmsContentFlag == 1 ){
Serial.println( “*** SMS MESSAGE CONTENT ***” );
Serial.println( msg );
Serial.println( “*** END OF SMS MESSAGE ***” );
ProcessSms( msg );
}
ClearGprsMsg();
// EN: Always clear the flag
// FR: Toujours mettre le flag à 0
SmsContentFlag = 0;
return;
}
please i need help