0

I am able to interface gps and the ultrasonic sensor and obtain data successfully for individual interfacing with the Arduino UNO. When I combined the code for these two, I only get the data values from gps but no measurement values are obtained for the ultrasonic sensor.In the variable data, I have stored the gps data packet. I have read the solution for the same problem but none of them worked. I have attached the code below:

    #include <SoftwareSerial.h>
    #include "TinyGPS++.h"

    SoftwareSerial serial_connection(8, 9);

    TinyGPSPlus gps;
    TinyGPSCustom Trackingangle(gps, "GPRMC", 8);
    TinyGPSCustom Dir_NS (gps, "GPRMC", 4);
    TinyGPSCustom Dir_EW (gps, "GPRMC", 6);

    String data,tr,ew,ns,d1;
  int a=0;
    const int trigPin = 6;
    const int echoPin = 7;
    long duration;
    int distance;
    int buttonPin = 12;
    String s="",str_dist="",str_button="";
    int buttonPushCounter = 0;  
    int buttonState = 0;        
    int lastButtonState = 0;  

    void setup()
    {
      pinMode(trigPin, OUTPUT); 
    pinMode(echoPin, INPUT);
    pinMode(buttonPin, INPUT);
      Serial.begin(9600);
            serial_connection.begin(9600);
    }

    void loop()
    {
      data="";tr="";d1="";
    serial_connection.listen();
     dat_gps();
       if(data!="")
    {
    Serial.println("GPS DATA= "+data);
    ultra();
      }
   }
        void ultra()
    {
distance=0;duration=0;
         digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    //if(duration!=0)
    {
      distance= duration*0.034/2;
        Serial.print("Distance= ");
    Serial.println(String(distance));
    }
    }

    void dat_gps()
    {
     {
      while(serial_connection.available())
      {
        gps.encode(serial_connection.read());
      }

    if (Trackingangle.isUpdated())
    {
        tr=String(Trackingangle.value()); //    Serial.println(tr);
    }
    if (Dir_EW.isUpdated())
    {

     ew=String(Dir_EW.value());  
    }
    if (Dir_NS.isUpdated())
    {

      ns=String(Dir_NS.value());  
    }
    if(tr!="")
    { if(gps.location.isUpdated())
     {       delay(2000);
        data=String(gps.satellites.value());
       data+=",";
       if(data!="0,")
       {
        data+=String(gps.location.lat(), 6);
        data+=",";
        data+=String(gps.location.lng(), 6);
        data+=",";
           data+=String(gps.speed.mph());
              String a=String(gps.altitude.feet());
         if(a!="")
       {   data+=",";
       data+=a;
         data+=",";
          data+=String(gps.hdop.value());  
         data+=",";
         data+=tr;
         data+=",";
         data+=ns;
         data+=",";
         data+=ew;
         data+="";
            }
       }}
      }
     }
    }
  • You need to explain your hardware setup and your code. What type of communication do your different devices use? I2C? SPI? What pins do they use for their interfaces? What model of GPS and ultrasonic sensor are you using? Where can your readers find documentation and specs on those devices? What do the different commands to the devices do? Also, you need to fix the formatting on your code. The indentation is a mess, and makes it very hard to follow the flow. – Duncan C Oct 17 at 13:02
  • You can't. An Arduino Uno is a a single processor. It can do only one thing at once. Please can you have a go at formatting your code, because its not very reader friendly. – Code Gorilla Oct 17 at 14:43
  • Put Serial.println("GPS DATA= "+data); right after the call to dat_gps() and tell us what it prints. – Sim Son Oct 17 at 16:27
  • I am using HC SR04 as ultrasonic sensor and Quectel L80 as gps. I want serial comunication on pins 8&9. – Muskaan Oct 18 at 3:56
  • @SimSon, some values are shown as 0 as gps takes time to set. Once its set, all the values are printed – Muskaan Oct 18 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.