Need Help merging 2 codes

Im newbie here , i just want to know how to merge the 2 codes that i have , i'd already try to merge it but I think its far from reality lol. sorry for my bad english. Thanks in Advance by the way :slight_smile:

#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial sim808(7, 8); // RX, TX
int Relay = 10;
String data[5];

char coming;
char phone_no[] = "09480875916";
String text = "";
String state, timegps, latitude, longitude;
const int buzzer = 3;
int vibrationPin = 2;
bool flag=false;
String ph1="+639480875916"; //Enter your phone number to call #01



void setup() {
  pinMode(vibrationPin,INPUT);
digitalWrite(vibrationPin,LOW);
//System Calibration
for(int i=0;i<10;i++)
{
  delay(200);
}
  pinMode(buzzer, OUTPUT);
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
  sim808.begin(9600);
  Serial.begin(9600);// Start software serial
  delay(100);
  sim808.println("AT+CMGF=1");      // Iset ang GSM para sa text
  delay(100);
  sim808.println("AT+CNMI=2,2,0,0,0"); // mag notify pag may bagong dating na message
  delay(1000);
  sim808.println("AT+CMGD=1,4"); // AT+CMGD=1,4 Range ng sms ng SIM card na pwedeng idelete
  delay(100);
  sendData( "AT+CGNSPWR=1", 1000, DEBUG);     // pag power on ng GPS
  delay(100);
  sendData( "AT+CGNSSEQ=RMC", 1000, DEBUG);   // Pag send ng isang line NMEA sentence
  delay(100);
  SMSsend("SYSTEM IS TURN ON");
  delay(100);
  Serial.println("System OK, Waiting for text Message");
  delay(50);
  sim808.flush();
  delay(50);
  

}


void loop() {
        int a=vibration();
  if(a>15000){
    flag=true;   
  }

if (flag) {
  call1();
  message1();    //Generate the Alert Message First Mobile
  //delay(1000);
  //message2();   //Generate the Alert Message Second Mobile
  //delay(1000); //1 minute delay
        //Call to First mobile
  //delay(1000);
  //call2();      //call to Second mobile
  }
  flag=false;
   sms();
  

 



}

void SMSsend(String massage) {

  sim808.print("AT+CMGF=1\r");                  //  Carriage return
  delay(100);
  sim808.println("AT+CMGS=\"09480875916\"");
  delay(100);
  sim808.println(massage);
  delay(100);
  sim808.println((char)26);                    //ctrl+z pag send ng message
  delay(100);
  sim808.println();
  delay(100);
  sim808.println("AT+CMGD=1,4");
  delay(100);
  sim808.println("AT+CMGF=1");
  delay(100);
  sim808.println("AT+CNMI=1,2,0,0,0");
  delay(200);
  text = "";

}

void sms() {

  while (sim808.available()) {
    delay(10);
    coming = sim808.read();
    if (coming == '#') {
      if (sim808.available()) {
        delay(10);
        coming = sim808.read();
        if (coming == 'R') {
          if (sim808.available()) {
            delay(10);
            coming = sim808.read();
            if (coming == 'L') {
              if (sim808.available()) {
                delay(10);
                coming = sim808.read();
                if (coming == 'Y') {
                  if (sim808.available()) {
                    delay(10);
                    coming = sim808.read();
                    if (coming == 'o') {                   
                         
                          sendData( "AT+CGNSPWR=1", 1000, DEBUG);     // pag power on ng GPS
                          delay(100);
                          sendData( "AT+CGNSSEQ=RMC", 1000, DEBUG);   // Pag send ng isang line NMEA sentence
                          delay(100);
                          sendTabData("AT+CGNSINF", 1000, DEBUG);
                          delay(100);

                          if (state != 0) {
                            Serial.println("State  :" + state);
                            Serial.println("Time  :" + timegps);
                            Serial.println("Latitude  :" + latitude);
                            Serial.println("Longitude  :" + longitude);

                            sim808.print("AT+CMGS=\"");
                            sim808.print(phone_no);
                            sim808.println("\"");

                            delay(300);

                            sim808.print("http://maps.google.com/maps?q=loc:");
                            sim808.print(latitude);
                            sim808.print(",");
                            sim808.print (longitude);
                            delay(200);
                            sim808.println((char)26); // End AT command with a ^Z, ASCII code 26
                            delay(200);
                            sim808.println();
                            delay(10000);
                            sim808.flush();




                        
                  

                    }
                    }




                    else if (coming == 'c') {

                   
                      digitalWrite(Relay, HIGH);     // pag off ng makina
                      Serial.println("Relay Off");
                      //SMSsend("Motorcycle has been Shutdown");
                      Serial.println("Waiting for Text Message...");

                    }
                    else if (coming == 't') {
                     
                      digitalWrite(Relay, LOW);    // pag on ng makina
                      Serial.println("Relay On");
                      //SMSsend("Motorcycle is Starting again");
                      Serial.println("Waiting for Text Message...");
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

}












void sendTabData(String command , const int timeout , boolean debug) {

  sim808.println(command);
  long int time = millis();
  int i = 0;

  while ((time + timeout) > millis()) {
    while (sim808.available()) {
      char c = sim808.read();
      if (c != ',') {
        data[i] += c;                // SA PAG DUKOT NG MGA STRING SA NMEA DATA NA BINATBATO
        //        delay(100);
      } else {
        i++;
      }
      if (i == 5) {
        //        delay(100);
        goto exitL;
      }
    }
} exitL:
  if (debug) {
    state = data[1];
    data[1] = "";
    timegps = data[2];
    data[2] = "";                  // PARA IDELETE YUNG MGA LUMANG DATA, AT IPASOK YUNG MGA BAGONG DATA
    latitude = data[3];
    data[3] = "";
    longitude = data[4];
    data[4] = "";
  }

}


void message1()  //Method to send message #01
{
    sim808.println("AT+CMGS=\"");
    sim808.println(ph1);
    sim808.println("\"\r\n");
    delay(2000);
    sim808.println("---ALERT---\n Security Breach");
    sim808.write(0x1A);
}

void call1()   //Method to get call #01
{
  sim808.println("ATD");
  sim808.println(ph1);
  sim808.println(";\r\n");
  delay(30000);
  sim808.println("ATH");
}

int vibration(){
  delay(10);
  int vibrationDetection=pulseIn(vibrationPin,HIGH);  //wait for the pin to get HIGH and returns measurement
  return vibrationDetection;
}

String sendData (String command , const int timeout , boolean debug) {
  String response = "";
  sim808.println(command);
  long int time = millis();
  int i = 0;

  while ( (time + timeout ) > millis()) {
    while (sim808.available()) {
      char c = sim808.read();
      response += c;
    }
  }
  if (debug) {
    Serial.print(response);
  }
  return response;
}

2codes.ino (7.08 KB)

putting the separate progams into separate sub-functions and calling them from loop() seems find

but your sm() looks awkward. why not read and buffer characters when they become available. you can check for a particular string after receiving each new characters. it looks like you can reset the buffer when you received a '#'.

I just cant seem to figure out how to merge this 2 codes, what i mean is, i'm waiting for the Vibration sensor's activity, while waiting to receive text message to GSM Module at the same time.

i assume the problem is the awkward code waiting for serial data.

that should just be a subfunction that checks if data is available, appends it to a buffer and checks (strcmp) for a complete message. there's no need to have the nested ifs

2 sketches with delay() functions will generally step all over each other when combined.

This tutorial by Nick Gammon (not me) shows how to write code that doesn't do that.

About halfway down this tutorial is a simple State Machine example. State Machines make real-time coordination much easier.
Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking <-- techniques howto

If you learn these (start simple and work up) then you can write tasks that work together smoothly.

In Project Guidance the lessons are in a sticky thread up top, Demonstration code for several things at the same time

I have an example of changing code with delays to code without. It's simple and only shows one level to get people started. It's part of a set of sketches that can all run combined.

gcjr:
i assume the problem is the awkward code waiting for serial data.

that should just be a subfunction that checks if data is available, appends it to a buffer and checks (strcmp) for a complete message. there's no need to have the nested ifs

GoForSmoke:
2 sketches with delay() functions will generally step all over each other when combined.

This tutorial by Nick Gammon (not me) shows how to write code that doesn't do that.
Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

About halfway down this tutorial is a simple State Machine example. State Machines make real-time coordination much easier.
Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking <-- techniques howto

If you learn these (start simple and work up) then you can write tasks that work together smoothly.

In Project Guidance the lessons are in a sticky thread up top, Demonstration code for several things at the same time

I have an example of changing code with delays to code without. It's simple and only shows one level to get people started. It's part of a set of sketches that can all run combined.

Thank you so much for the help. I truly appreciate the help from both of you. God bless :smiley:

If you're going to try the multiple things at once approach, you will get a lot of help and be sure of this: it's not so hard as it sounds. People who have a lot of experience in top-down coding (what we all begin with) have a harder time than beginners comfortable with basic C (Arduino is C/C++ with that setup() and loop() front-end added) who don't have to unlearn "The RIGHT Way" to get into this other way.

Picture void loop() as a wheel since whenever it finishes it runs again.

Inside of loop() you put functions to do everything needed, and triggers on those that only run on certain conditions like "has 100 ms gone by?" or "does Serial have a char to read?" or "was that char a number?".

So the loop() runs and every time some bits are done and progress is made --- with non-blocking code the loop() wheel may turn 50,000 or more times a second. That is fast enough to keep up with real world events like "was the button pressed" very closely, very smoothly.

Take your time. When you learn this you will be able to combine and reuse code easily. Until then, delays will keep you from getting even 1% of the speed possible. If you are a programmer, learning this will change your life. It's like never having to stand in line again.

GoForSmoke:
If you're going to try the multiple things at once approach, you will get a lot of help and be sure of this: it's not so hard as it sounds. People who have a lot of experience in top-down coding (what we all begin with) have a harder time than beginners comfortable with basic C (Arduino is C/C++ with that setup() and loop() front-end added) who don't have to unlearn "The RIGHT Way" to get into this other way.

Picture void loop() as a wheel since whenever it finishes it runs again.

Inside of loop() you put functions to do everything needed, and triggers on those that only run on certain conditions like "has 100 ms gone by?" or "does Serial have a char to read?" or "was that char a number?".

So the loop() runs and every time some bits are done and progress is made --- with non-blocking code the loop() wheel may turn 50,000 or more times a second. That is fast enough to keep up with real world events like "was the button pressed" very closely, very smoothly.

Take your time. When you learn this you will be able to combine and reuse code easily. Until then, delays will keep you from getting even 1% of the speed possible. If you are a programmer, learning this will change your life. It's like never having to stand in line again.

Thank you very much for your help. Im just a beginner in this platform. And your words, helped me a lot. Thank you very much again ! God bless :smiley:

The Nick Gammon tutorials were written for beginners using beginner level code. Before the code he gives explanation of what it is about and then with the code every part is explained. This is to get people new to the idea started.