Making one code from three separate codes

Hello there, I made 3 codes and I have been having trouble on making them into one. My project is a device that controls motor through buttons or via text message. It also sends a message when a condition is met (in this case, when my float switch is triggered). I used L298N for controlling motors, GSM Sim800L for receiving and sending sms and buttons.

This is the code where I just copy pasted everything and arranged it a little bit.

 // remember to remove rx tx before uploading code
 //gsm keeps on restarting so the other messsages dont get to be read.

#include <Sim800l.h>
SoftwareSerial mySerial(10,11);  // (Rx,Tx  > Tx,Rx) 

char incomingByte; 
String inputString;
int relay1=3; // Output for Relay Control
int relay2=4;

int button1=8;
int val;
int buttonOld=1;
int state=0;
int wait=2500;

int button2=7;
int val2;
int buttonOld2=1;
int state2=0;

int FLOAT_SENSOR=2;
int sms_count=0;

void setup() 
{
      pinMode(relay1, OUTPUT);
      pinMode(relay2, OUTPUT);
      pinMode(button1,INPUT);
      pinMode(button2,INPUT);
      pinMode(FLOAT_SENSOR, INPUT_PULLUP);
      digitalWrite(relay1, LOW); // Initial state of the relay
      digitalWrite(relay2, LOW);
      Serial.begin(9600);
      mySerial.begin(9600); 

     while(!mySerial.available()){
        mySerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      mySerial.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      mySerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
     }

void loop()
{  
   if(digitalRead(FLOAT_SENSOR) == HIGH) 
  {

 mySerial.println("AT+CMGF=1"); 
 mySerial.println("AT+CMGS=\"+6xxxx\"\r"); // change to the phone number you using 
  delay(1000);
  mySerial.println("Water is Leaking!");//the content of the message
  delay(200);
  mySerial.println((char)26);//the stopping character
  delay(1000);
  sms_count++;
  }
  if(mySerial.available()){
      delay(100);

      // Serial Buffer
      while(mySerial.available()){
        incomingByte = mySerial.read();
        inputString += incomingByte; 
        }

        delay(10);      

        Serial.println(inputString);
        inputString.toUpperCase(); // Uppercase the Received Message

        //turn RELAY ON or OFF
        if (inputString.indexOf("NORMAL") > -1){
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, LOW);
          }
         if (inputString.indexOf("REVERSE") > -1){
          digitalWrite(relay2, HIGH);
          digitalWrite(relay1, LOW);
          }          

        delay(50);

   
     

        inputString = "";
  }

  
  val=digitalRead(button1);

if(buttonOld==0 && val==1){
  if(state==0){
    digitalWrite(relay1,HIGH);
    state=1;
  }
  else{
    digitalWrite(relay1,LOW);
    state=0;
  }
}
buttonOld=val;
val2=digitalRead(button2);
if(buttonOld2==0 && val2==1){
  if(state2==0){
    digitalWrite(relay2,HIGH);
    state2=1;
  }
  else{
    digitalWrite(relay2,LOW);
    state2=0;
  }
}
buttonOld2=val2;
  }

This is the float switch code:

#include <Sim800l.h>
SoftwareSerial mySerial(10,11);  // (Rx,Tx  > Tx,Rx) 
int FLOAT_SENSOR=2;
int sms_count=0;
void setup() 
{
  // initialize the pushbutton pin as an input:
  pinMode(FLOAT_SENSOR, INPUT_PULLUP);
      Serial.begin(9600);
      mySerial.begin(9600); 
}

void loop() 
{
  if(digitalRead(FLOAT_SENSOR) == HIGH) 
  {
    // turn LED on:
 mySerial.println("AT+CMGF=1"); 
 mySerial.println("AT+CMGS=\"+6xxxxx\"\r"); // change to the phone number you using 
  delay(1000);
  mySerial.println("Water Leaking!");//the content of the message
  delay(200);
  mySerial.println((char)26);//the stopping character
  delay(1000);
  sms_count++;
  }
}

This is the button toggle code:

int normal=3;
int reverse=4;

int button1=8;
int val;
int buttonOld=1;
int state=0;
int wait=2500;

int button2=7;
int val2;
int buttonOld2=1;
int state2=0;
void setup() {
 
  // put your setup code here, to run once:
 Serial.begin(9600);
pinMode(normal,OUTPUT);
pinMode(reverse,OUTPUT);
pinMode(button1,INPUT);
pinMode(button2,INPUT);


}

void loop() {
  // put your main code here, to run repeatedly
val=digitalRead(button1);
Serial.println(val);
if(buttonOld==0 && val==1){
  if(state==0){
    digitalWrite(normal,HIGH);
    state=1;
  }
  else{
    digitalWrite(normal,LOW);
    state=0;
  }
}
buttonOld=val;
val2=digitalRead(button2);
if(buttonOld2==0 && val2==1){
  if(state2==0){
    digitalWrite(reverse,HIGH);
    state2=1;
  }
  else{
    digitalWrite(reverse,LOW);
    state2=0;
  }
}
buttonOld2=val2;
}

This is the receive code where you can control the motors with the GSM:

 // remember to remove rx tx before uploading code
 //gsm keeps on restarting so the other messsages dont get to be read.

#include <Sim800l.h>
SoftwareSerial mySerial(10,11);  // (Rx,Tx  > Tx,Rx) 

char incomingByte; 
String inputString;
int relay1=3; // Output for Relay Control
int relay2=4;

void setup() 
{
      pinMode(relay1, OUTPUT);
      pinMode(relay2, OUTPUT);
      digitalWrite(relay1, LOW); // Initial state of the relay
      digitalWrite(relay2, LOW);
      Serial.begin(9600);
      mySerial.begin(9600); 

     while(!mySerial.available()){
        mySerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      mySerial.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      mySerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
     }

void loop()
{  
  if(mySerial.available()){
      delay(100);

      // Serial Buffer
      while(mySerial.available()){
        incomingByte = mySerial.read();
        inputString += incomingByte; 
        }

        delay(10);      

        Serial.println(inputString);
        inputString.toUpperCase(); // Uppercase the Received Message

        //turn RELAY ON or OFF
        if (inputString.indexOf("NORMAL") > -1){
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, LOW);
          }
         if (inputString.indexOf("REVERSE") > -1){
          digitalWrite(relay2, HIGH);
          digitalWrite(relay1, LOW);
          }          

        delay(50);

        //Delete Messages & Save Memory
        if (inputString.indexOf("OK") == -1){
        mySerial.println("AT+CMGDA=\"DEL ALL\"");
        }
     

        inputString = "";
  
  }

So when I use the code that I made, it keeps on deleting messages, and it also keeps restarting the GSM Sim800L (not sure if this is a hardware problem but I’ll look into it.). Sometimes the whole device doesn’t work, GSM wont send me SMS or even the buttons wont work. I hope that I made it clear and I’m hoping that someone would help me. Thanks in advance!

Any time I see "relay" and "restarting" together, I suspect a hardware problem. We see a lot of those here, it's usually something to do with the relays sharing a power supply with the Arduino, or some other lack of isolation. Have you tried running the sketch with the relays disconnected?

The easiest way to combine separate Arduino Sketches is to bring them into a master project file as functions.
The best way to make the functions is for each individual sketch to look like this:

void loop(){
 myFunctionA();
}

Then in your new sketch:

void loop(){
 myFunctionA();
 myFunctionB();
 myFunctionC();
}

Above is good, but be careful of using delay() because a delay() in one function and you stop them all.

-jim lee