Trouble synchronizing GPS with other sensors on Arduino UNO
SRN
Posts: 15
I am using an optical sensor to count the number of times the beam is broken over a period of time, then recording that information onto an SD card. I am trying to link this with certain GPS information (date, time, latitude, longitude, maybe elevation) to keep up with when and where the breaks occured. (GPS receiver is a discontinued model, part # 28146.) I have an Arduino sketch that works for each component (recording the optical sensor data onto an SD card, writing all GPS information to the monitor), but when I try to put them together it doesn't work. Even simply writing the two processes separate from each other just results in neither working. The best I have managed so far is to put the optic sensor loop inside the GPS logging loop; this at least has the sensor functioning. (Original sketches were called GPS_sketch and Termite1; long story.)
I was wondering if it might not be a better idea to try and use Smart mode and only call for the data I need; the only problem with that is figuring out how to code *that* correctly (I haven't had much luck). I have done some programming and electronics work before but am new to both Arduino processors and Parallax equipment. Any help would be much appreciated.
I was wondering if it might not be a better idea to try and use Smart mode and only call for the data I need; the only problem with that is figuring out how to code *that* correctly (I haven't had much luck). I have done some programming and electronics work before but am new to both Arduino processors and Parallax equipment. Any help would be much appreciated.
/* This program is a compilation of "GPS_sketch" and "termite1"
with excess information removed; only one input sensor is used, the
GPS only records (time,date,longitude,latitude,elevation), and the
GPS information is written into the data file rather than simply being
serial printed. */
#include <SD.h>
#include <Wire.h>
#include <string.h>
#include <ctype.h>
//termite
const int chipSelect = 4;
int sensorPin4 = A3; // only one input pin (not 4)
int ledPin = 13; // select the pin for the LED
int x,o;
int sensorValue4 = 0; // variable to store the value coming from the sensor
float voltage4;
String voltageString4 = "default";
int time;
int time2;
//gps************************************************************
int rxPin = 0; // RX PIN
int txPin = 1; // TX TX
int byteGPS=-1;
int snoopy = 0;
char linea[300] = "";
char comandoGPR[7] = "$GPRMC";
int cont=0;
int bien=0;
int conta=0;
int indices[13];
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(4800);
pinMode(10,OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
for (int i=0;i<300;i++){ // Initialize a buffer for received data
linea=' ';
}
if(!SD.begin(chipSelect)){
Serial.println("Card Failed");
return;
}
else {
Serial.println("card initialized");
}
}
void loop() {
o=0;
for(x=0; x< 10; x++){
// read the value from the sensor:
sensorValue4 = analogRead(sensorPin4);
// convert to voltage
voltage4 = sensorValue4*(5.0/1023.0);
delay(500);
if(voltage4 > 1.0) {
o+=1;
}
}
//******Original GPS commands with serial.print*********************
digitalWrite(ledPin, HIGH);
byteGPS=Serial.read(); // Read a byte of the serial port
if (byteGPS == -1) { // See if the port is empty yet
delay(100);
}
else {
linea[conta]=byteGPS; // If there is serial port data, it is put in the buffer
conta++;
Serial.write(byteGPS);
if (byteGPS==13){ // If the received byte is = to 13, end of transmission
digitalWrite(ledPin, LOW);
cont=0;
bien=0;
for (int i=1;i<7;i++){ // Verifies if the received command starts with $GPR
if (linea==comandoGPR[i-1]){
bien++;
}
}
if(bien==6){ // If yes, continue and process the data
for (int i=0;i<300;i++){
if (linea==','){ // check for the position of the "," separator
indices[cont]=i;
cont++;
}
if (linea=='*'){ // ... and the "*"
indices[12]=i;
cont++;
}
}
Serial.println(""); // ... and write to the serial port
Serial.println("");
Serial.println("
");
for (int i=0;i<12;i++){
switch(i){
case 0 :
Serial.print("Time in UTC (HhMmSs): ");
break;
/* case 1 :
Serial.print("Status (A=OK,V=KO): ");
break; */
case 2 :
Serial.print("Latitude: ");
break;
case 3 :
Serial.print("Direction (N/S): ");
break;
case 4 :
Serial.print("Longitude: ");
break;
case 5 :
Serial.print("Direction (E/W): ");
break;
/* case 6 :
Serial.print("Velocity in knots: ");
break;
case 7 :
Serial.print("Heading in degrees: ");
break;*/
case 8 :
Serial.print("Date UTC (DdMmAa): ");
break;
/* case 9 :
Serial.print("Magnetic degrees: ");
break;
case 10 :
Serial.print("(E/W): ");
break;
case 11 :
Serial.print("Mode: ");
break;
case 12 :
Serial.print("Checksum: ");
break; */
}
for (int j=indices;j<(indices[i+1]-1);j++){
if (i==0 || i==2 || i==3 || i==4 || i==5 || i==8)
Serial.print(linea[j+1]);
Serial.println(""); }
}
Serial.println("
");
}
conta=0; // Reset the buffer
for (int i=0;i<300;i++){ //
linea=' ';
}
}
//Termite1 data writing
File dataFile = SD.open("GPSBugs.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
Serial.print("Counts: ");
Serial.print(o);
Serial.println(" ");
dataFile.print("Counts: ");
dataFile.print(o);
dataFile.println(" ");
//Write GPS data to file*******************************************
for (int i=0;i<12;i++){
switch(i){
case 0 :
dataFile.print("Time in UTC (HhMmSs): ");
break;
case 2 :
dataFile.print("Latitude: ");
break;
case 3 :
dataFile.print("Direction (N/S): ");
break;
case 4 :
dataFile.print("Longitude: ");
break;
case 5 :
dataFile.print("Direction (E/W): ");
break;
case 8 :
dataFile.print("Date UTC (DdMmAa): ");
break;
}
for (int j=indices;j<(indices[i+1]-1);j++){
if (i==0 || i==2 || i==3 || i==4 || i==5 || i==8)
dataFile.print(linea[j+1]);
dataFile.println(""); }
}
//Close the data file******************************************
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening GPSBUGS.txt");
}
}
}
Comments
Here's a link to a code tags tutorial.
I notice you have a pause after you read the sensor. Would this pause combined with all the other SD writes and serial output interfere with the timing of the loop (for example UART buffer overflow)?