My project involves the hc-sr04, adafruit ultimate gps module, and an sd card reader/writer. I have been doing testing of this device; I upload two text documents onto the SD card, one for the GPS fix, and one for the hc-sr04 distance. Everything works, except that the values I get on the SD card from the hc-sr04, there are around 10% outliers. I’m not sure if this is normal or not since it is a very cheap sensor, but if anyone could look at my code and make any recommendations that would be awesome! Thanks
#include <NewPing.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
SoftwareSerial mySerial(11,10);
Adafruit_GPS GPS(&mySerial);
char c;
String NMEA1;
String NMEA2;
#define TRIGGER_PIN 31
#define ECHO_PIN 33
#define MAX_DISTANCE 200
#define pingSpeed 2000
#define chipSelect 53
NewPing sonar(31,33,65);
File myFile;
void setup() {
Serial.begin(115200);
GPS.begin(9600);
GPS.sendCommand("$PGCMD,33,0*6D");
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
if(!SD.begin(53)) {
Serial.println(F("not beginning"));
return;
}
Serial.println(F("its working"));
if(! SD.remove("ultrad.txt")){
Serial.println(F("ultra not working"));
}
if(!SD.remove("gps.txt")){
Serial.println(F("gps not working"));
return;
}
}
void loop() {
Ping();
delay(1000);
GPSdata();
delay(1000);
SDCARD2();
delay(1000);
}
void Ping(){
Serial.print(F("Ping: "));
Serial.print(sonar.ping_cm());
Serial.println(F("cm"));
if((sonar.ping_cm()<= 65) && (sonar.ping_cm()>0)){
myFile = SD.open("ultrad.txt", FILE_WRITE);
myFile.print(sonar.ping_cm());
myFile.print(",");
myFile.print("cm");
myFile.print(",");
myFile.print(GPS.hour, DEC);myFile.print(':');
myFile.print(GPS.minute, DEC);
myFile.print(",");
myFile.print(GPS.month, DEC);
myFile.print("/");
myFile.print(GPS.day, DEC);
myFile.print("/");
myFile.println(GPS.year, DEC);
myFile.close();
}
}
void GPSdata() {
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.print(GPS.latitude);
Serial.print(F(","));
Serial.println(GPS.longitude);
Serial.print(GPS.hour, DEC);Serial.print(':');
Serial.println(GPS.minute, DEC);
}
void 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
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();
}
}
void SDCARD2(){
myFile = SD.open("gps.txt", FILE_WRITE);
myFile.print(GPS.latitude);
myFile.print(",");
myFile.print(GPS.longitude);
myFile.print(",");
myFile.print(GPS.hour, DEC);myFile.print(':');
myFile.print(GPS.minute, DEC);
myFile.print(",");
myFile.print(GPS.month, DEC);
myFile.print("/");
myFile.print(GPS.day, DEC);
myFile.print("/");
myFile.println(GPS.year, DEC);
myFile.close();
}