Execute two instructions at the same time

Hello
Could you help me with my code? I'm doing a project where I'm using a FONA 808 Cellular + GPS Breakout module from Adafruit to create a panic button and a Max Botix HRLV-EZ0 ultrasound sensor with a vibrating motor to be able to perceive obstacles , the 2 codes work very well separately, here the problem I have is that by joining the two codes, the signals are combined and pressing the panic button sends only half of the coordinates I need and the ultrasonic sensor turns around unstable and the engine does not stop vibrating strongly while the SMS is being sent, so I would like to know if there is any method so that these interruptions do not occur between the two codes
Beforehand thank you very much.
I show the code that I am using

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
 
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

// Usa esto para FONA 800 y 808s
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

// para respuestas más tarde
char replybuffer[255];
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
 
const int buttonPin = 8;  
const int BuzzerPin = 11;
int pwm = 10;
float motor;

 
void setup() {
  {
    Serial.begin(115200); // Inicia la comunicación serial
    pinMode(pwm,OUTPUT);
    pinMode(motor,OUTPUT);
}
  while (! Serial);
  Serial.println(F("Inicializando FONA... (Puede tardar algunos segundos)"));
 
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("No se puede encontrar FONA"));
    while(1);
  }
  Serial.println(F("FONA esta BIEN"));
  // Intenta habilitar GPRS
 
  Serial.println(F("Habilitando GPS..."));
  fona.enableGPS(true);
  delay(4000);
  
  pinMode(BuzzerPin, OUTPUT);
}
 
void loop() {
  {
   int analogValue = analogRead(0);
   int analogValue1= analogValue*1;
   motor = (analogRead(0));
   Serial.print("Distancia ");
   Serial.println(motor);
   analogWrite(pwm,-(analogRead(0)));
   delay(150);
   Serial.println(analogValue);
   delay(10);
}
  
  int buttonState;    
  buttonState = digitalRead(buttonPin);
  
 // si se mantiene presionado el botón (es decir, el pánico)
  if (buttonState == LOW)  {
 
      // obtener la ubicación gps
      float latitude, longitude;// variables para mantener lecturas GPS iniciales
      boolean gps_success = fona.getGPS(&latitude, &longitude);
      if (gps_success) {
        Serial.print("GPS lat:");
        Serial.println(latitude, 6);
        Serial.print("GPS long:");
        Serial.println(longitude, 6);
 
      //enviar SMS
      char message[141];  
      char LAT1[10];// cadena de latitud larga sin analizar y desbordante
      char LAT[10];
      char LONG[10];
      dtostrf(latitude, 10, 7, LAT1); // recopilar datos de GPS en un formato que se puede enviar
      dtostrf(longitude, 10, 7, LONG);
      
     // inicializa la matriz deseada de una matriz no analizada
      for(int i = 0; i < 9; i++) {
      LAT[i] = LAT1[i];
      }
      LAT[9] = '\0';// truncar la matriz al último valor deseado
      sprintf(message, "¡PULSE MI BOTON DE PANICO!¡NECESITO AYUDA! https://www.google.com/maps?q=%s,%s", LAT, LONG);
      
      Serial.println(LAT);Serial.println(LAT1);Serial.println(LONG);
      Serial.println(message) ;    // imprime el mensaje en el monitor serial antes de enviar
 
      char sendto[21] = "+523334492748"; // pone el número de teléfono de destino deseado para sms aquí
      fona.sendSMS(sendto, message) ; // envía el mensaje por SMS}
 
     // SE ENCIENDE EL BUZZER DURANTE 5 SEGUNDOS PARA NOTIFICAR QUE EL TEXTO ENVIADO
      for (int i = 0; i < 5; i++){
      digitalWrite(BuzzerPin, HIGH);
      delay(1000);
      digitalWrite(BuzzerPin, LOW);
      delay(1000);
      }
      } else {
        Serial.println("Esperando el arreglo FONA GPS 3D ...");
      }
      
     // Verifica la red, luego GPRS
      Serial.println(F("Comprobando la red celular ..."));
      if (fona.getNetworkStatus() == 1) {
       // red y GPRS? ¡Estupendo! Imprima la ubicación GSM para comparar
        float latitude, longitude;
        boolean gsmloc_success = fona.getGSMLoc(&latitude, &longitude);
        if (gsmloc_success) {
          Serial.print("GSMLoc lat:");
          Serial.println(latitude, 6);
          Serial.print("GSMLoc long:");
          Serial.println(longitude, 6);          

      } else {
        Serial.println("La ubicación GSM falló ...");
        Serial.println(F("Desactivando GPRS"));
        fona.enableGPRS(false);
        Serial.println(F("Habilitando GPRS"));
        if (!fona.enableGPRS(true)) {
        Serial.println(F("Error al encender GPRS "));  
          }
        }
      }
   }
}

I don't expect you will find the Forum sympathetic to your plea of urgency.

Post the program that represents your best attempt at combining the two programs.

...R

I'm sorry, post corrected

I can't tell what your program is doing - your coding style and my poor Spanish put me at a disadvantage. You should give a lot more information about exactly what you are doing, and how you are using the peripherals, OR post this in the Español section of the forum.

Axel0119:
I'm sorry, post corrected

Not really.

You seem to have removed the two separate programs and just shown your attempt at combining them. When I asked you to post your best attempt I assumed it was not necessary to tell you to post it in a new Reply and leave the Original Post unchanged so that we could view all three programs.

Please now post the two separate programs so we can understand what you are trying to combine.

...R

codes separately

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
 
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

// Usa esto para FONA 800 y 808s
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

// para respuestas más tarde
char replybuffer[255];
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
 
const int buttonPin = 8;  
const int BuzzerPin = 11;
 
void setup() {
  while (! Serial);
  Serial.begin(115200);
  Serial.println(F("Inicializando FONA... (Puede tardar algunos segundos)"));
 
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("No se puede encontrar FONA"));
    while(1);
  }
  Serial.println(F("FONA esta BIEN"));
  // Intenta habilitar GPRS
 
  Serial.println(F("Habilitando GPS..."));
  fona.enableGPS(true);
  delay(4000);
  
  pinMode(BuzzerPin, OUTPUT);
}
 
void loop() {
  
  int buttonState;    
  buttonState = digitalRead(buttonPin);
  
 // si se mantiene presionado el botón (es decir, el pánico)
  if (buttonState == LOW)  {
 
      // obtener la ubicación gps
      float latitude, longitude;// variables para mantener lecturas GPS iniciales
      boolean gps_success = fona.getGPS(&latitude, &longitude);
      if (gps_success) {
        Serial.print("GPS lat:");
        Serial.println(latitude, 6);
        Serial.print("GPS long:");
        Serial.println(longitude, 6);
 
      //enviar SMS
      char message[141];  
      char LAT1[10];// cadena de latitud larga sin analizar y desbordante
      char LAT[10];
      char LONG[10];
      dtostrf(latitude, 10, 7, LAT1); // recopilar datos de GPS en un formato que se puede enviar
      dtostrf(longitude, 10, 7, LONG);
      
     // inicializa la matriz deseada de una matriz no analizada
      for(int i = 0; i < 9; i++) {
      LAT[i] = LAT1[i];
      }
      LAT[9] = '\0';// truncar la matriz al último valor deseado
      sprintf(message, "¡PULSE MI BOTON DE PANICO!¡NECESITO AYUDA! https://www.google.com/maps?q=%s,%s", LAT, LONG);
      
      Serial.println(LAT);Serial.println(LAT1);Serial.println(LONG);
      Serial.println(message) ;    // imprime el mensaje en el monitor serial antes de enviar
 
      char sendto[21] = "+523334492748"; // pone el número de teléfono de destino deseado para sms aquí
      fona.sendSMS(sendto, message) ; // envía el mensaje por SMS}
 
     // SE ENCIENDE EL BUZZER DURANTE 5 SEGUNDOS PARA NOTIFICAR QUE EL TEXTO ENVIADO
      for (int i = 0; i < 5; i++){
      digitalWrite(BuzzerPin, HIGH);
      delay(1000);
      digitalWrite(BuzzerPin, LOW);
      delay(1000);
      }
      } else {
        Serial.println("Esperando el arreglo FONA GPS 3D ...");
      }
      
     // Verifica la red, luego GPRS
      Serial.println(F("Comprobando la red celular ..."));
      if (fona.getNetworkStatus() == 1) {
       // red y GPRS? ¡Estupendo! Imprima la ubicación GSM para comparar
        float latitude, longitude;
        boolean gsmloc_success = fona.getGSMLoc(&latitude, &longitude);
        if (gsmloc_success) {
          Serial.print("GSMLoc lat:");
          Serial.println(latitude, 6);
          Serial.print("GSMLoc long:");
          Serial.println(longitude, 6);          

      } else {
        Serial.println("La ubicación GSM falló ...");
        Serial.println(F("Desactivando GPRS"));
        fona.enableGPRS(false);
        Serial.println(F("Habilitando GPRS"));
        if (!fona.enableGPRS(true)) {
        Serial.println(F("Error al encender GPRS "));  
          }
        }
      }
   }
}
int pwm = 10;
float motor;

void setup() 
{
    Serial.begin(115200); // Inicia la comunicación serial
    pinMode(pwm,OUTPUT);
    pinMode(motor,OUTPUT);
}
void loop() 
{
   int analogValue = analogRead(0);
   int analogValue1= analogValue*1;
   motor = (analogRead(0));
   Serial.print("Distancia ");
   Serial.println(motor);
   analogWrite(pwm,-(analogRead(0)));
   delay(150);
   Serial.println(analogValue);
   delay(10);
}

This sort of thing suggests that you have not given much thought to what is going on

void setup() {
  {
    Serial.begin(115200); // Inicia la comunicación serial
    pinMode(pwm,OUTPUT);
    pinMode(motor,OUTPUT);
}
  while (! Serial);
  Serial.begin(115200);

Why would you have the line Serial.begin(115200); twice in the same program?

Go through your program and make sure it is sensible.

...R