Shop OBEX P1 Docs P2 Docs Learn Events
Trouble synchronizing GPS with other sensors on Arduino UNO — Parallax Forums

Trouble synchronizing GPS with other sensors on Arduino UNO

SRNSRN Posts: 15
edited 2013-07-24 12:27 in Accessories
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.
/* 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

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-11 14:36
    It would be easier to read your code if you used code tags rather than quote tags.

    Here's a link to a code tags tutorial.

    attachment.php?attachmentid=78421&d=1297987572

    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)?
  • FranklinFranklin Posts: 4,747
    edited 2013-07-12 14:23
    If you could tell us how you are connecting things it would also help. Which pins are connected where?
  • SRNSRN Posts: 15
    edited 2013-07-13 12:23
    With this version I have the SIO pin connected to pin 0, since in regular mode you just have to receive data from the receiver. When I tried hooking it up for Smart Mode, I used pins 2 and 3 initially, but I'm not sure whether I'm doing it right. Duane, I tried running the GPS sensor code in a loop (300 iterations when I tried it) and putting a small delay between it and the optic sensor loop. What I got was the strings of data ($GPRMC and friends) but it didn't spit out the organized data from the case/break commands, and the SD card started having trouble opening after about the fifth run. I don't have access to the equipment over the weekend but I think my next step might be to try putting both programs as subroutines and just calling them as functions. I am, of course, open to suggestions; someone with more experience with these systems may have better ideas and I wouldn't mind having four or five things to try Monday.
  • SRNSRN Posts: 15
    edited 2013-07-15 08:23
    Okay, I tried running the GPS code as a function and calling it in my void loop; if I can get that to work I can just use the data variable I defined to log my GPS information. Unfortunately, when I put this inside a function it started doing wierd things with my string data. The GPGGA string, for example, would start randomly showing up in my GPRMC data (which I was using to define the longitude, latitude, etc.) resulting in information that told me things like "Direction(E/W): W$GPGGA" followed by the GGA data in places that weren't supposed to have numbers. I am posting this code; the for loop (130 iterations) is due to the fact that when I ran the function with no delay and no for loop I got 129 blank spaces before my string of data; this also didn't keep the string mashup from occuring (I checked). Any help would be greatly appreciated.
    #include <string.h>
    #include <ctype.h>
    String values = ""; // what is returned from the function
    int ledPin = 13; // LED test pin
    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() {
    
    pinMode(ledPin, OUTPUT); // Initialize LED pin
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    Serial.begin(4800);
    for (int i=0;i<300;i++){ // Initialize a buffer for received data
    linea[i]=' ';
    } 
    }
    void loop() {
    values = GPS();
    
    Serial.print(values);
    Serial.println("");
    delay(2000);
    
    }
    String GPS(){
    String placeholder;
    placeholder = ""; 
    
    for(int k=0;k<130;k++){
    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[i]==comandoGPR[i-1]){
    bien++;
    }
    }
    if(bien==6){ // If yes, continue and process the data
    for (int i=0;i<300;i++){
    if (linea[i]==','){ // check for the position of the "," separator
    indices[cont]=i;
    cont++;
    }
    if (linea[i]=='*'){ // ... 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): ");
    placeholder += "Time in UTC (HhMmSs): ";
    break;
    case 1 :
    // Serial.print("Status (A=OK,V=KO): ");
    placeholder += "Status (A=OK, V=KO): ";
    break;
    case 2 :
    // Serial.print("Latitude: ");
    placeholder += "Latitude: ";
    break;
    case 3 :
    // Serial.print("Direction (N/S): ");
    placeholder += "Direction (N/S): ";
    break;
    case 4 :
    // Serial.print("Longitude: ");
    placeholder += "Longitude: ";
    break;
    case 5 :
    // Serial.print("Direction (E/W): ");
    placeholder += "Direction (E/W): ";
    break;
    case 6 :
    // Serial.print("Velocity in knots: ");
    placeholder += "Velocity in knots: ";
    break;
    case 7 :
    // Serial.print("Heading in degrees: ");
    placeholder += "Heading in degrees: ";
    break;
    case 8 :
    // Serial.print("Date UTC (DdMmAa): ");
    placeholder += "Date UTC (DdMmAa): ";
    break;
    case 9 :
    // Serial.print("Magnetic degrees: ");
    placeholder += "Magnetic degrees: ";
    break;
    case 10 :
    // Serial.print("(E/W): ");
    placeholder += "(E/W): ";
    break;
    case 11 :
    // Serial.print("Mode: ");
    placeholder += "Mode: ";
    break;
    case 12 :
    // Serial.print("Checksum: ");
    placeholder += "Checksum: ";
    break;
    }
    for (int j=indices[i];j<(indices[i+1]-1);j++){
    // Serial.print(linea[j+1]); 
    placeholder += linea[j+1];
    }
    // Serial.println("");
    }
    // Serial.println("---------------");
    }
    conta=0; // Reset the buffer
    for (int i=0;i<300;i++){ // 
    linea[i]=' '; 
    } 
    }
    }
    }
    byteGPS = -1;
    return placeholder;
    }
    
    
  • SRNSRN Posts: 15
    edited 2013-07-24 12:27
Sign In or Register to comment.