I'm working on a project for school. The idea is to make a device that will be installed on a train that will take a measurement of the gap from the train car to the platform and also take a fix of where it is (which station it is at) and then I will log this data on an SD card reader/writer. I have the ultrasonic sensor and the GPS module set up correctly; they both work perfectly individually. However, when I try to include both of them in the same sketch, the readings that I'm getting in the serial monitor are not consistent for the distance measurement. I do not need them running at the same time, I just need them to take accurate readings one after the other, it does not matter which order. Here is the code (it's long but any help would be much appreciated);
#include <Adafruit_GPS.h> //Install the adafruit GPS library
#include <SoftwareSerial.h> //Load the softwareserial library
SoftwareSerial mySerial(3,2); //Sets up new serial port on pins 3 and 2
Adafruit_GPS GPS(&mySerial); //Create the GPS object
int trigPin = 11; //trig pin connected to pin 11
int echoPin = 12; //echo pin connected to pin 12
float pingTime; //time for ping to travel from sensor to target AND return
float targetDistance; //distance to target in inches
float speedofSound= 776.5; //speed of sounds in mph when temp is 77 degrees
String NMEA1; //Variable for first NMEA sentence
String NMEA2; //Variable for second NMEA sentence
char c; //to read characters coming from the GPS
float deg; //will hold pisition data in degree format
float degWhole; //variable for the whole part of position
float degDec; //variable for decimal part of degree
unsigned long previousMillis = 0; //Will store time that the sensor last ran
const long interval = 3000; //interval at which the sensor will start
void setup() {
Serial.begin(9600); //turn on serial monitor
GPS.begin(9600); //turn on GPS at 9600 baud rate
GPS.sendCommand("$PGCMD,33,0*6D"); //Sending command to turn off status updates about external antenna
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //set update rate to 1Hz
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //request RMC and GGA sentences only
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
delay(1000);
}
void loop() {
unsigned long currentMillis = millis(); //currentMillis is the time that the arduino has been running in milliseconds
if(currentMillis - previousMillis>interval){ //should be a 3 second delay without the delay function
previousMillis = currentMillis;
ultrasensor(); //run the ping sensor
} else {
readGPS();
}
}
void readGPS() {
clearGPS();
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
NMEA1=GPS.lastNMEA();
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
NMEA2=GPS.lastNMEA();
//Serial.println(NMEA1);
//Serial.println(NMEA2);
//Serial.println("");
Serial.print(GPS.latitude);
Serial.print(",");
Serial.print(GPS.longitude);
Serial.print(",");
Serial.println(GPS.speed);
}
void clearGPS() { //Clear old and corrupt data from serial port
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
}
void ultrasensor() { //makes function outside of the loop so when commanded in the loop, it will perform this function
digitalWrite(trigPin, LOW); //set trigger pin low
delayMicroseconds(2000); //let signal settle
digitalWrite(trigPin, HIGH); //set trigger high
delayMicroseconds(15); //delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //delay in low state
pingTime = pulseIn(echoPin, HIGH); //in microseconds
pingTime = pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime = pingTime/3600; //convert pingTime to hours by dividing by 3600 (seconds in one hour)
targetDistance = speedofSound * pingTime; // this will be in miles, since speed of sound was mph
targetDistance = targetDistance/2; //ping travels to target and back, so must divide by 2 for actual target distance
targetDistance = targetDistance*63360; //convert miles to inches by multiplying by 63360 (inches per mile)
Serial.print("The Distance to Target is: "); //Serial monitor will display this
Serial.print(targetDistance); //serial monitor will display this after previous message
Serial.println(" inches"); //last part of the line in the serial monitor (thats why its a println)
}