How to get accurate time???

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

any ideas on why its not getting values???

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: How to get accurate time???

Post by tatanka »

Well, I am not sure. I have an Ultimate GPS attached to an Arduino Pro Mini. I have it connected as such:

GPS Arduino
Vin 5V
GND GND
RX D2
TX D3

I have the parsing sketch going, I am inside so no actual GPS data but I am getting a Serial response. See attached pic. I have my serial monitor set to 115200 baud.

I think maybe one of the adafruit guys might need to step in.

T
Attachments
GPS Test.jpg
GPS Test.jpg (53.26 KiB) Viewed 356 times

mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

i get something the serial read out but i get

Time: 0 : 0 : 00.000
Date: 0/0/2000
Fix: 0 quality 0

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: How to get accurate time???

Post by tatanka »

Well, time to get down and dirty then, follow the instructions here:

https://learn.adafruit.com/adafruit-ult ... ter-wiring

The biggest difference is AFTER loading the 'blank' sketch, change the wiring to pins 0 and 1 per the instructions. Send some pictures of your setup and serial monitor if you can.

Tim

mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

I'm not using the breakout board. .. I have the shield....

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: How to get accurate time???

Post by tatanka »


User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: How to get accurate time???

Post by tatanka »

Okay, reading through the info for the shield, they use pins 7 and 8 for the serial connection. So change the code:

Code: Select all

SoftwareSerial mySerial(3, 2);

to this:

SoftwareSerial mySerial(8, 7);
Try that in the parsing sketch first and see if it works then.

Tim

mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

After reading that...... Parsing and echo work... however, i still cant figure out what is keeping my code from collecting the time...

Code: Select all

#include <SPI.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <string.h>
#include <ccspi.h>

SoftwareSerial mySerial(8,7);
Adafruit_GPS GPS(&mySerial);

#define GPSECHO true

int interval = 1000;
float S1=0;
float S2=0;
float S3=0;
float S4=0;
unsigned long previousMillis=0;
unsigned int reading=0;
File logfile;
void setup(){
  Serial.begin(115200);
  GPS.begin(9600);
  delay(1000);
  Serial.println("Hello!!! Let's begin science");
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  GPS.sendCommand(PGCMD_ANTENNA);
  delay(2000);
  mySerial.println(PMTK_Q_RELEASE);
  char c=GPS.read();
  #ifdef UDRO
  if (GPSECHO)
  if (c) UDRO=c;
  #endif
  Serial.println(c);
  int n=0;
  /*while(n==0){
    if(GPS.hour==0){
      Serial.println("Waiting on satalite");
      c=GPS.read();
      Serial.println(GPS.hour);
      Serial.println(GPS.hour,DEC);
      delay(5000);
    }else{
      n=1;
    }
  }*/
  //Serial.print("Finding the SD Card....");
  pinMode(10,OUTPUT);
  delay(1000);
  //if (!SD.begin(10)) {
    //Serial.println("Couldn't find it..... Sorry.");
    //return;
  //}
  //Serial.println("FOUNTD IT!!!!  Lets start recording.");
    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Reading");
    Serial.print(",");
    Serial.print("Time");
    Serial.print(",");
    Serial.print("A0");
    Serial.print(",");
    Serial.print("A1");
    Serial.print(",");
    Serial.print("A2");
    Serial.print(",");
    Serial.println("A3");    
  /*File dataFile=SD.open("timetes1.txt",FILE_WRITE);
  if (dataFile){
    dataFile.print("Reading");
    dataFile.print(",");
    dataFile.print("Time");
    dataFile.print(",");
    dataFile.print("A0");
    dataFile.print(",");
    dataFile.print("A1");
    dataFile.print(",");
    dataFile.print("A2");
    dataFile.print(",");
    dataFile.print("A3");
    dataFile.print(",");
    dataFile.println(starttime);    
    dataFile.close();
  }*/
}
void loop() {
  unsigned long currentMillis = millis();
  if ((unsigned long)(currentMillis-previousMillis)>=interval) {
    S1=analogRead(0)*2.4437927;
    S2=analogRead(1);
    S3=analogRead(2);
    S4=analogRead(3);
    reading=reading+1;
    Serial.print(reading);
    Serial.print(",");
    Serial.print(currentMillis);
    Serial.print(",");
    Serial.print(S1);
    Serial.print(",");
    Serial.print(S2);
    Serial.print(",");
    Serial.print(S3);
    Serial.print(",");
    Serial.println(S4);  
    /*File dataFile=SD.open("timetes1.txt",FILE_WRITE);
    if(dataFile){
      dataFile.print(reading);
      dataFile.print(",");
      dataFile.print(currentMillis);
      dataFile.print(",");
      dataFile.print(S1);
      dataFile.print(",");
      dataFile.print(S2);
      dataFile.print(",");
      dataFile.print(S3);
      dataFile.print(",");
      dataFile.println(S4);
      dataFile.close();
    }*/
    previousMillis=currentMillis;
  }
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to get accurate time???

Post by adafruit_support_rick »

You have the code which reads the GPS located in the setup() function, where it will only be executed once.

Code: Select all

  char c=GPS.read();
  #ifdef UDRO
  if (GPSECHO)
  if (c) UDRO=c;
  #endif
  Serial.println(c);
Once you get to loop(), you will never read the GPS again.

Beyond that, you aren't calling GPS.parse(), so you will never extract the timestamp from the GPS data.

If I were you, I would start over with a fresh copy of the parsing example sketch. Add your sensor readings to that, without disturbing the GPS code.

mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

I only want to call the gps to get the data and time of when the log is created....

well i have figured somethings out and have what i want working.... however, my time jumps every once and a while.... does anything jump out that screams dont do that, that slows everything down???

Code: Select all


#include <Adafruit_GPS.h>  //Call Libraries
#include <SoftwareSerial.h>
#include<SPI.h>
#include<SD.h>

SoftwareSerial mySerial(8, 7);  //Set pins on GPS unit
Adafruit_GPS GPS(&mySerial);

File logfile;
uint32_t timer = millis();
int n=0;
void setup()  
{
  Serial.begin(115200);
  Serial.println("Hello. Let' start the test.");
  GPS.begin(9600);
  pinMode(10,OUTPUT);
  if(!SD.begin(10)){
    Serial.println("SD card not found... Ending program.");
    return;
  }
  
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   
  GPS.sendCommand(PGCMD_ANTENNA);
  delay(1000);
  mySerial.println(PMTK_Q_RELEASE);
  while(n==0){
    char c = GPS.read();
    if (GPS.newNMEAreceived()) {  
      if (!GPS.parse(GPS.lastNMEA()))
        n=0;
    }
    if (millis() - timer > 2000) { 
      timer = millis(); // reset the timer
      if(GPS.hour!=0 && GPS.day!=0){
        Serial.println("Let us get started...");
        File dataFile=SD.open("Log02.txt",FILE_WRITE);
        if(dataFile){
          dataFile.print("\nTime: ");
          dataFile.print(GPS.hour, DEC); dataFile.print(':');
          dataFile.print(GPS.minute, DEC); dataFile.print(':');
          dataFile.print(GPS.seconds, DEC); dataFile.print('.');
          dataFile.println(GPS.milliseconds);
          dataFile.print("Date: ");
          dataFile.print(GPS.day, DEC); dataFile.print('/');
          dataFile.print(GPS.month, DEC); dataFile.print("/20");
          dataFile.println(GPS.year, DEC);
          dataFile.println("Reading,Time,Sensor1,Sensor2,Sensor3,Sensor4");
          dataFile.close();
          Serial.println("Begin");
          n=5;
        }
      }
    }
  }
}
int S1=0;
int S2=0;
int S3=0;
int S4=0;
unsigned int reading=0;
void loop(){
  if(n==5){
   if(millis()-timer>=50){
     timer=millis();
     S1=analogRead(0);
     S2=analogRead(1);
     S3=analogRead(2);
     S4=analogRead(3);
     reading=reading+1;
     File dataFile=SD.open("Log02.txt",FILE_WRITE);
     if(dataFile){
     dataFile.print(reading);
     dataFile.print(",");
     dataFile.print(timer);
     dataFile.print(",");
     dataFile.print(S1);
     dataFile.print(",");
     dataFile.print(S2);
     dataFile.print(",");
     dataFile.print(S3);
     dataFile.print(",");
     dataFile.println(S4);
     dataFile.close();}
    }
  }
} 

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to get accurate time???

Post by adafruit_support_rick »

Only thing I can suggest is to open the data file once, in setup, instead of every time through loop():

Code: Select all

#include <Adafruit_GPS.h>  //Call Libraries
#include <SoftwareSerial.h>
#include<SPI.h>
#include<SD.h>

SoftwareSerial mySerial(8, 7);  //Set pins on GPS unit
Adafruit_GPS GPS(&mySerial);

File dataFile;
uint32_t timer = millis();
int n=0;
void setup()  
{
  Serial.begin(115200);
  Serial.println("Hello. Let' start the test.");
  GPS.begin(9600);
  pinMode(10,OUTPUT);
  if(!SD.begin(10)){
    Serial.println("SD card not found... Ending program.");
    return;
  }

  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   
  GPS.sendCommand(PGCMD_ANTENNA);
  delay(1000);
  mySerial.println(PMTK_Q_RELEASE);
  while(n==0){
    char c = GPS.read();
    if (GPS.newNMEAreceived()) {  
      if (!GPS.parse(GPS.lastNMEA()))
        n=0;
    }
    if (millis() - timer > 2000) { 
      timer = millis(); // reset the timer
      if(GPS.hour!=0 && GPS.day!=0){
        Serial.println("Let us get started...");
        File dataFile=SD.open("Log02.txt",FILE_WRITE);
        if(dataFile){
          dataFile.print("\nTime: ");
          dataFile.print(GPS.hour, DEC); 
          dataFile.print(':');
          dataFile.print(GPS.minute, DEC); 
          dataFile.print(':');
          dataFile.print(GPS.seconds, DEC); 
          dataFile.print('.');
          dataFile.println(GPS.milliseconds);
          dataFile.print("Date: ");
          dataFile.print(GPS.day, DEC); 
          dataFile.print('/');
          dataFile.print(GPS.month, DEC); 
          dataFile.print("/20");
          dataFile.println(GPS.year, DEC);
          dataFile.println("Reading,Time,Sensor1,Sensor2,Sensor3,Sensor4");
          dataFile.close();
          Serial.println("Begin");
          dataFile=SD.open("Log02.txt",FILE_WRITE);
          if(dataFile)
            n=5;
        }
      }
    }
  }
}
int S1=0;
int S2=0;
int S3=0;
int S4=0;
unsigned int reading=0;
void loop(){
  if(n==5){
    if(millis()-timer>=50){
      timer=millis();
      S1=analogRead(0);
      S2=analogRead(1);
      S3=analogRead(2);
      S4=analogRead(3);
      reading=reading+1;
      dataFile.print(reading);
      dataFile.print(",");
      dataFile.print(timer);
      dataFile.print(",");
      dataFile.print(S1);
      dataFile.print(",");
      dataFile.print(S2);
      dataFile.print(",");
      dataFile.print(S3);
      dataFile.print(",");
      dataFile.println(S4);
      dataFile.flush();
    }
  }
} 

mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

What happens when power is shut off? ??

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How to get accurate time???

Post by adafruit_support_rick »

I put a dataFile.flush() at the end of your loop. That should ensure that all the current data is written to the file at end of the loop.

mschramm
 
Posts: 27
Joined: Fri Feb 28, 2014 12:34 pm

Re: How to get accurate time???

Post by mschramm »

so what is the difference form dataFile.close() and dataFile.flush()? Is it just something that will be more efficient when writing so many times a second?

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: How to get accurate time???

Post by adafruit_support_mike »

A flush and a close are two different events.

Device drivers don't always write data to the physical media when you send them a write() command. It's more efficient (and often better for the device) to write data in blocks of a specific size. The device driver manages that by keeping a buffer of data it's been told to write to the file, but only writing when the buffer size reaches a certain limit.

A flush tells the driver "no, really.. write the data to the physical medium. I want your buffer to be empty before anything else happens." A close disposes of the data structures used to represent the file in the program's address space. Closing a file might force a flush, but that's not guaranteed. There are cases where it's better to stop immediately and abandon any data that's still hanging around in the buffer.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”