10 x een conditie testen, data wegschrijven op SDkaart

Arduino specifieke Software
Berichten: 11
Geregistreerd: 24 Mei 2016, 11:59

10 x een conditie testen, data wegschrijven op SDkaart

Berichtdoor Looneyt » 23 Jan 2020, 14:33

Hallo allemaal,

Met een Pro Mini 8Mhz 3V3, GPS module en een SDkaartje heb ik een programma geschreven die checked of een bepaalde conditie aanwezig is ( valid fix en HDOP < 2000), als dat zo is, schrijf dan GPS data naar de SDKaart. Dit werkt naar behoren. als de conditie Onwaar is, knippert er een LED en als de conditie weer Waar wordt, gaat er keurig data naar de SD kaart.

Nu is mijn plan om per positie waar de GPS module wordt gebruikt, slechts 10 locaties te loggen, ik ben niet geïnteresseerd wat er in de tussentijd gebeurd. Sterker nog, er is geen GPS ontvangst op die momenten.

Hoe maak ik een dergelijke lus waarbij steeds de conditie wordt getest? Ik weet dat de While loop dit doet maar als de conditie Onwaar is, gaat ie uit zijn loop en volgt de Onwaar statement. Do While is in mijn geval denk ik niet handig, de conditie wordt pas aan het einde van de lus getest, de waarden zijn dan al op de SDkaart geschreven. Een If loop wordt uitgevoerd zodra de conditie Waar is. Het zal een combinatie van If, For en wellicht While worden maar ik zit vast. wellicht dat een state machine een oplossing biedt? Ik weet daar nog te weinig van maar hou graag de opties open.

Weet iemand mij een duw te geven in de juiste richting?

Wat ik wil is dit:

Code: Alles selecteren
doe dit 10 keer maar alleen als een fix beschikbaar is EN HDOP < 2000

Schrijf dan een regel weg naar SDkaart

anders

Laat rode led knipperen


Edit: Mij schiet net het gebruik van een counter te binnen, wellicht biedt dat soelaas, ik ga hier in duiken.

Mijn volledige code:

Code: Alles selecteren
/*
  Project: GPS buoy for position logging from underwater.
  Function: This sketch listens to a GPS module and when a fix meets certain criteria, 10 lines of position information are stored on the SDcard.
  The operator can see the status of the logging using an RGB LED (or seperate LEDs).

  Wiring GPS module:
  GPS module -> Arduino Pro Mini 8MHz 3V3
  GND           GND
  RX pin        not used, GPS can only TX
  TX pin        Digital pin 8
  VCC pin       3V3

  Wiring SD card module:
  SD card module -> Arduino Pro Mini 8MHz 3V3
  VCC           3.3V
  CS            Digital pin 10
  D1/MOSI       Digital pin 11
  SCK/CLK       Digital pin 13
  D0/MISO       Digital pin 12
  GND           GND

 Description:  This program logs a certain amount of GPS fields in a tabular fashion on a SDcard.
 You must also enable HDOP in GPSfix_cfg.h.
     Most NeoGPS examples display *all* configured GPS fields in a CSV format
     (e.g., NMEA.ino).
  Prerequisites:
     1) NMEA.ino works with your device (correct TX/RX pins and baud rate)
     2) GPS_FIX_HDOP is defined in GPSfix_cfg.h
  'Serial' is for debug output to the Serial Monitor window.
  License:
    Copyright (C) 2014-2017, SlashDevin
    This file is part of NeoGPS
    NeoGPS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    NeoGPS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with NeoGPS.  If not, see <http://www.gnu.org/licenses/>.
*/
//************************************************************
#include <SPI.h>//include library code to communicate with SPI devices
#include <SD.h>//include library code for SD card
#include <NMEAGPS.h> //the GPS library
#include <GPSport.h> //The used port configuration set for NMEAGPS

//**********************************************************************
NMEAGPS     gps;
const int chipSelect = 10; //SDcard is set to pin 10.
// Check configuration
#ifndef GPS_FIX_HDOP
#error You must uncomment GPS_FIX_HDOP in GPSfix_cfg.h!
#endif
//**********************************************************************
//Button configuration

const int buttonPin = 6;     // the number of the pushbutton pin

//LED configuration
const int blueled = 3;
const int redled = 5;
const int greenled = 4;
int ledState = LOW;
//int redState = LOW;
//int greenState = LOW;
unsigned long previousMillis = 0;  //stores the last time the LED was updated
const long interval = 100; //Interval at which to blink LED

void setup()
{
  pinMode(buttonPin, INPUT); //The button can be used, currently optional
  pinMode(blueled, OUTPUT); //Blue part of the RGB Led
  pinMode(redled, OUTPUT); //Red part of the RGB Led
  pinMode(greenled, OUTPUT); //Green part of the RGB Led
  DEBUG_PORT.begin(9600);
  gpsPort.begin(9600);
  Serial.print("Initializing SD card...");//setup for the SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present!");
    return;
  }
  Serial.println("Card initialized.");
  Serial.println("File opened. Start logging GPS data to SD card:");
  File dataFile = SD.open("gpsdata.csv", FILE_WRITE); //opens file
  if (dataFile) { //if the file opened ok, writes to it
    dataFile.println("Date, Time , Lat , Long , Sats , HDOP , Speed");//prints the headers for our data
  }
  dataFile.close();//file closed
  DEBUG_PORT.println
  (
    F( "Sats HDOP Latitude  Longitude  Date       Time    Speed  Heading  \n"
       "          (deg)     (deg)                                         \n" )
  );

  repeat( '-', 133 );
  digitalWrite(greenled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
  digitalWrite(redled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
  digitalWrite(blueled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
}
void loop()
{
  if (gps.available( gpsPort )) {
    gps_fix fix = gps.read();

    if (fix.valid.hdop && (fix.hdop < 2000)) {
    bool  validDT         = fix.valid.date & fix.valid.time;
    digitalWrite(redled, HIGH); //used to reset the Red LED to off when the Loop function is valid again
    digitalWrite(greenled, LOW); //Set green led to on in order to show the buoy is currently in a valid Fix state and can write to the SDcard.
    print(             fix.satellites       , fix.valid.satellites, 3             ); //debugging only
    print(             fix.hdop / 1000.0      , fix.valid.hdop      , 6, 2          ); //debugging only
    print(             fix.latitude ()      , fix.valid.location  , 10, 6         ); //debugging only
    print(             fix.longitude()      , fix.valid.location  , 11, 6         ); //debugging only
    print(             fix.dateTime         , validDT             , 20            ); //debugging only
//   print(             fix.altitude ()      , fix.valid.altitude  , 7, 2          ); //debugging only
    print(             fix.speed_kph()      , fix.valid.speed     , 7, 2          ); //debugging only
    print(             fix.heading  ()      , fix.valid.heading   , 7, 2          ); //debugging only
//    print( compassDir( fix.heading  () )    , fix.valid.heading   , 4             ); //debugging only
//    print( gps.statistics.chars , true, 10 ); //not used
//    print( gps.statistics.ok    , true,  6 ); //not used
//    print( gps.statistics.errors, true,  6 ); //not used

    DEBUG_PORT.println();
    //***************************************************************************
    // The routine for writing data to SD card:
   //for (int i = 0; i <= 255; i++) { //When do I need to call this? In order to write just 10 lines to the SDcard?
    File dataFile = SD.open("gpsdata.csv", FILE_WRITE);


    if (dataFile) {// if the file is available, write to it:

      if (fix.dateTime.date < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.date);
      dataFile.print( '/' );
      if (fix.dateTime.month < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.month);
      dataFile.print( '/' );
      dataFile.print(fix.dateTime.year);
      dataFile.print(',');

      if (fix.dateTime.hours < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.hours);
      dataFile.print( ':' );
      if (fix.dateTime.minutes < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.minutes);
      dataFile.print( ':' );
      if (fix.dateTime.seconds < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.seconds);
      dataFile.print(',');
      dataFile.print(fix.latitude (), 10);
      dataFile.print(",");
      dataFile.print(fix.longitude(), 11);
      dataFile.print(",");
      dataFile.print(fix.satellites, 3);
      dataFile.print(",");
      dataFile.print(fix.hdop / 1000.0, 6);
//      dataFile.print(",");
//      dataFile.print(fix.altitude (), 7);
      dataFile.print(",");
      dataFile.print(fix.speed_kph(), 7);
      dataFile.print("\n");
      dataFile.close();
    }
   
    //}

    //***************************************************************

  }
  else {
    noFix();
  }
  }
 
}

//-----------------
//  Print utilities

static void repeat( char c, int8_t len )
{
  for (int8_t i = 0; i < len; i++)
    DEBUG_PORT.write( c );
}

static void printInvalid( int8_t len )
{
  DEBUG_PORT.write( ' ' );
  repeat( '*', abs(len) - 1 );
}

static void print( float val, bool valid, int8_t len, int8_t prec )
{
  if (!valid) {
    printInvalid( len );
  } else {
    char s[16];
    dtostrf( val, len, prec, s );
    DEBUG_PORT.print( s );
  }
}

static void print( int32_t val, bool valid, int8_t len )
{
  if (!valid) {
    printInvalid( len );
  } else {
    char s[16];
    ltoa( val, s, 10 );
    repeat( ' ', len - strlen(s) );
    DEBUG_PORT.print( s );
  }
}

static void print( const __FlashStringHelper *str, bool valid, int8_t len )
{
  if (!valid) {
    printInvalid( len );
  } else {
    int slen = strlen_P( (const char *) str );
    repeat( ' ', len - slen );
    DEBUG_PORT.print( str );
  }
}

static void print( const NeoGPS::time_t & dt, bool valid, int8_t len )
{
  if (!valid) {
    printInvalid( len );
  } else {
    DEBUG_PORT.write( ' ' );
    Serial << dt; // this "streaming" operator outputs date and time
  }
}

//------------------------------------------------------------
//  This snippet is from NMEAaverage.  It keeps all the
//    compass direction strings in FLASH memory, saving RAM.

const char nCD  [] PROGMEM = "N";
const char nneCD[] PROGMEM = "NNE";
const char neCD [] PROGMEM = "NE";
const char eneCD[] PROGMEM = "ENE";
const char eCD  [] PROGMEM = "E";
const char eseCD[] PROGMEM = "ESE";
const char seCD [] PROGMEM = "SE";
const char sseCD[] PROGMEM = "SSE";
const char sCD  [] PROGMEM = "S";
const char sswCD[] PROGMEM = "SSW";
const char swCD [] PROGMEM = "SW";
const char wswCD[] PROGMEM = "WSW";
const char wCD  [] PROGMEM = "W";
const char wnwCD[] PROGMEM = "WNW";
const char nwCD [] PROGMEM = "NW";
const char nnwCD[] PROGMEM = "NNW";

const char * const dirStrings[] PROGMEM =
{ nCD, nneCD, neCD, eneCD, eCD, eseCD, seCD, sseCD,
  sCD, sswCD, swCD, wswCD, wCD, wnwCD, nwCD, nnwCD
};

const __FlashStringHelper *compassDir( uint16_t bearing ) // degrees CW from N
{
  const int16_t directions    = sizeof(dirStrings) / sizeof(dirStrings[0]);
  const int16_t degreesPerDir = 360 / directions;
  int8_t  dir           = (bearing + degreesPerDir / 2) / degreesPerDir;

  while (dir < 0)
    dir += directions;
  while (dir >= directions)
    dir -= directions;

  return (const __FlashStringHelper *) pgm_read_ptr( &dirStrings[ dir ] );

} // compassDir

void noFix() //blink the red LED to show there is no fix
{
   unsigned long currentMillis = millis();
  digitalWrite(greenled, HIGH);
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(redled, ledState);
  }
}

Advertisement

Berichten: 11
Geregistreerd: 24 Mei 2016, 11:59

Re: 10 x een conditie testen, data wegschrijven op SDkaart

Berichtdoor Looneyt » 23 Jan 2020, 14:51

Zoals in mijn edit staat, net schoot me de counter binnen. Ik heb mijn pseudocode aangepast naar:

Code: Alles selecteren
Als een fix beschikbaar is EN HDOP < 2000 (Is dit ONWAAR, laat rode led knipperen.)

Schrijf dan een regel weg naar SDkaart


Tel 1 bij de counter op

als de counter 10 is, stap dan uit de functie en doe knopfunctie

knopfunctie
Laat blauwe led knipperen zolang knop = Laag

Als knop = Hoog -> void loop() uitvoeren.


Ik zal dit moeten verwerken en kijken wat het doet.

Berichten: 11
Geregistreerd: 24 Mei 2016, 11:59

Re: 10 x een conditie testen, data wegschrijven op SDkaart

Berichtdoor Looneyt » 24 Jan 2020, 15:15

/met een counter krijg ik het niet voor elkaar, helaas;

Nu wordt er niets gelogd en counter verschijnt niet op de serial monitor..

Code: Alles selecteren

#include <SPI.h>//include library code to communicate with SPI devices
#include <SD.h>//include library code for SD card
#include <NMEAGPS.h> //the GPS library
#include <GPSport.h> //The used port configuration set for NMEAGPS

//**********************************************************************
NMEAGPS     gps;
const int chipSelect = 10; //SDcard is set to pin 10.
// Check configuration
#ifndef GPS_FIX_HDOP
#error You must uncomment GPS_FIX_HDOP in GPSfix_cfg.h!
#endif
//**********************************************************************
//Button configuration

const int buttonPin = 6;     // the number of the pushbutton pin

//LED configuration
const int blueled = 3;
const int redled = 5;
const int greenled = 4;
int ledState = LOW;
//int redState = LOW;
//int greenState = LOW;
unsigned long previousMillis = 0;  //stores the last time the LED was updated
const long interval = 100; //Interval at which to blink LED
int buttonState = 0;
int counter = 0;
void setup()
{
  pinMode(buttonPin, INPUT); //The button can be used, currently optional
  pinMode(blueled, OUTPUT); //Blue part of the RGB Led
  pinMode(redled, OUTPUT); //Red part of the RGB Led
  pinMode(greenled, OUTPUT); //Green part of the RGB Led
  DEBUG_PORT.begin(9600);
  gpsPort.begin(9600);
  Serial.print("Initializing SD card...");//setup for the SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present!");
    return;
  }
  Serial.println("Card initialized.");
  Serial.println("File opened. Start logging GPS data to SD card:");
  File dataFile = SD.open("gpsdata.csv", FILE_WRITE); //opens file
  if (dataFile) { //if the file opened ok, writes to it
    dataFile.println("Date, Time , Lat , Long , Sats , HDOP , Speed");//prints the headers for our data
  }
  dataFile.close();//file closed
  DEBUG_PORT.println
  (
    F( "Sats HDOP Latitude  Longitude  Date       Time    Speed  Heading  \n"
       "          (deg)     (deg)                                         \n" )
  );

  repeat( '-', 133 );
  digitalWrite(greenled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
  digitalWrite(redled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
  digitalWrite(blueled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
}
void loop()
{
 
        logPos();
    if (counter >= 10){
       
      digitalWrite(redled, LOW); //used to reset the Red LED to off when the Loop function is valid again
    digitalWrite(greenled, HIGH);
      //  else {
          noFix();
       
   //for (int i = 0; i <= 255; i++) { //When do I need to call this? In order to write just 10 lines to the SDcard?
    }
 }

void logPos()
  {
   if (gps.available( gpsPort )) {
    gps_fix fix = gps.read();

    if (fix.valid.hdop && (fix.hdop < 2000)) {
        bool  validDT         = fix.valid.date & fix.valid.time;
   
    File dataFile = SD.open("gpsdata.csv", FILE_WRITE);

    digitalWrite(greenled, LOW); //Set green led to on in order to show the buoy is currently in a valid Fix state and can write to the SDcard.
   
    if (dataFile) {     // if the file is available, write to it:

      if (fix.dateTime.date < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.date);
      dataFile.print( '/' );
      if (fix.dateTime.month < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.month);
      dataFile.print( '/' );
      dataFile.print(fix.dateTime.year);
      dataFile.print(',');

      if (fix.dateTime.hours < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.hours);
      dataFile.print( ':' );
      if (fix.dateTime.minutes < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.minutes);
      dataFile.print( ':' );
      if (fix.dateTime.seconds < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.seconds);
      dataFile.print(',');
      dataFile.print(fix.latitude (), 10);
      dataFile.print(",");
      dataFile.print(fix.longitude(), 11);
      dataFile.print(",");
      dataFile.print(fix.satellites, 3);
      dataFile.print(",");
      dataFile.print(fix.hdop / 1000.0, 6);
//      dataFile.print(",");
//      dataFile.print(fix.altitude (), 7);
      dataFile.print(",");
      dataFile.print(fix.speed_kph(), 7);
      dataFile.print("\n");
      dataFile.close();
    Serial.println(counter, DEC);
    counter++;
    //if (counter >= 10){
    //  noFix();
    //}
    }
    }
}
 
//return counter;
}
    //***************************************************************

 
  //else {
  //  noFix();
  //}
//  }
 
//}

//-----------------
//  Print utilities

static void repeat( char c, int8_t len )
{
  for (int8_t i = 0; i < len; i++)
    DEBUG_PORT.write( c );
}

static void printInvalid( int8_t len )
{
  DEBUG_PORT.write( ' ' );
  repeat( '*', abs(len) - 1 );
}

static void print( float val, bool valid, int8_t len, int8_t prec )
{
  if (!valid) {
    printInvalid( len );
  } else {
    char s[16];
    dtostrf( val, len, prec, s );
    DEBUG_PORT.print( s );
  }
}

static void print( int32_t val, bool valid, int8_t len )
{
  if (!valid) {
    printInvalid( len );
  } else {
    char s[16];
    ltoa( val, s, 10 );
    repeat( ' ', len - strlen(s) );
    DEBUG_PORT.print( s );
  }
}

static void print( const __FlashStringHelper *str, bool valid, int8_t len )
{
  if (!valid) {
    printInvalid( len );
  } else {
    int slen = strlen_P( (const char *) str );
    repeat( ' ', len - slen );
    DEBUG_PORT.print( str );
  }
}

static void print( const NeoGPS::time_t & dt, bool valid, int8_t len )
{
  if (!valid) {
    printInvalid( len );
  } else {
    DEBUG_PORT.write( ' ' );
    Serial << dt; // this "streaming" operator outputs date and time
  }
}

//------------------------------------------------------------
//  This snippet is from NMEAaverage.  It keeps all the
//    compass direction strings in FLASH memory, saving RAM.

const char nCD  [] PROGMEM = "N";
const char nneCD[] PROGMEM = "NNE";
const char neCD [] PROGMEM = "NE";
const char eneCD[] PROGMEM = "ENE";
const char eCD  [] PROGMEM = "E";
const char eseCD[] PROGMEM = "ESE";
const char seCD [] PROGMEM = "SE";
const char sseCD[] PROGMEM = "SSE";
const char sCD  [] PROGMEM = "S";
const char sswCD[] PROGMEM = "SSW";
const char swCD [] PROGMEM = "SW";
const char wswCD[] PROGMEM = "WSW";
const char wCD  [] PROGMEM = "W";
const char wnwCD[] PROGMEM = "WNW";
const char nwCD [] PROGMEM = "NW";
const char nnwCD[] PROGMEM = "NNW";

const char * const dirStrings[] PROGMEM =
{ nCD, nneCD, neCD, eneCD, eCD, eseCD, seCD, sseCD,
  sCD, sswCD, swCD, wswCD, wCD, wnwCD, nwCD, nnwCD
};

const __FlashStringHelper *compassDir( uint16_t bearing ) // degrees CW from N
{
  const int16_t directions    = sizeof(dirStrings) / sizeof(dirStrings[0]);
  const int16_t degreesPerDir = 360 / directions;
  int8_t  dir           = (bearing + degreesPerDir / 2) / degreesPerDir;

  while (dir < 0)
    dir += directions;
  while (dir >= directions)
    dir -= directions;

  return (const __FlashStringHelper *) pgm_read_ptr( &dirStrings[ dir ] );

} // compassDir

void noFix() //blink the red LED to show there is no fix
{
  unsigned long currentMillis = millis();
  digitalWrite(greenled, HIGH);
  buttonState = digitalRead(buttonPin);
      while (buttonPin == LOW){
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
  }
    // set the LED with the ledState of the variable:
    digitalWrite(redled, ledState);
  counter = 0;
  //return counter, DEC;
}
}

Gebruikers-avatar
Berichten: 7
Geregistreerd: 02 Sep 2016, 20:17

Re: 10 x een conditie testen, data wegschrijven op SDkaart

Berichtdoor Adam69 » 26 Jan 2020, 00:11

Ik heb de eerste code gebruikt, die je hebt geplaatst.
Hieronder een tweetal aanpassingen, waardoor er een teller van 10x bij komt

Ga naar regel
Code: Alles selecteren
const long interval = 100; //Interval at which to blink LED


En plaats daar een regel onder, zoals ik hier heb gedaan
Code: Alles selecteren
const long interval = 100; //Interval at which to blink LED
int aantal= 0;



Zoek dan ook naar deze regel
Code: Alles selecteren
    if (fix.valid.hdop && (fix.hdop < 2000)) {


en verander die regel naar dit hieronder, met de extra extra regel voor 'aantal' erbij
Code: Alles selecteren
    if ( (fix.valid.hdop) && (fix.hdop< 2000) && (aantal< 11) ) {
      aantal= aantal+ 1;


Ik hoop dat je hiermee verder komt.

Berichten: 11
Geregistreerd: 24 Mei 2016, 11:59

Re: 10 x een conditie testen, data wegschrijven op SDkaart

Berichtdoor Looneyt » 26 Jan 2020, 15:29

Bedankt Adam69! Mijn uiteindelijke oplossing heeft veel van jou voorstel, het programma werkt nu naar behoren.

Ik heb inderdaad een counter toegevoegd, tevens heb ik nog wat andere zaken moeten veranderen (zoals het wegschrijven van headers per log cyclus).
Mijn uiteindelijke code, deels, is nu zo geworden;

Code: Alles selecteren
#include <SPI.h>//include library code to communicate with SPI devices
#include <SD.h>//include library code for SD card
#include <NMEAGPS.h> //the GPS library
#include <GPSport.h> //The used port configuration set for NMEAGPS

//**********************************************************************
NMEAGPS     gps;
const int chipSelect = 10; //SDcard is set to pin 10.
// Check configuration
#ifndef GPS_FIX_HDOP
#error You must uncomment GPS_FIX_HDOP in GPSfix_cfg.h!
#endif
//**********************************************************************
//Button configuration

const int buttonPin = 6;     // the number of the pushbutton pin

//LED configuration
const int blueled = 3;
const int redled = 5;
const int greenled = 4;
int ledState = LOW;
//int redState = LOW;
//int greenState = LOW;

unsigned long previousMillis = 0;  //stores the last time the LED was updated
const long interval = 200; //Interval at which to blink LED
int buttonState = 0;
int counter = 0;
boolean printheaders = false; //needed for a one time run in loop

void setup()
{
  pinMode(buttonPin, INPUT); //The button is used to start another logging cycle
  pinMode(blueled, OUTPUT); //Blue part of the RGB Led
  pinMode(redled, OUTPUT); //Red part of the RGB Led
  pinMode(greenled, OUTPUT); //Green part of the RGB Led
  //DEBUG_PORT.begin(9600);
  gpsPort.begin(9600);
  //Serial.print("Initializing SD card...");//setup for the SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present!");
    return;
  }
  //Serial.println("Card initialized.");
  //Serial.println("File opened. Start logging GPS data to SD card:");
 
  //DEBUG_PORT.println
  //(
  //  F( "Sats HDOP Latitude  Longitude  Date       Time    Speed  Heading  \n"
  //     "          (deg)     (deg)                                         \n" )
  //);

  //repeat( '-', 133 );
  digitalWrite(greenled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
  digitalWrite(redled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
  digitalWrite(blueled, HIGH); //As I'm currently using common Anode RGB Led, this is set to High. When using common Cathode, use LOW instead.
}
void loop()
{
    digitalWrite(blueled, HIGH);
   
            if ((counter == 0) && (printheaders == false))  { //making sure this runs only once
      printheaders = true;
      File dataFile = SD.open("gpsdata.csv", FILE_WRITE);
        if (dataFile) {     // if the file is available, write to it:
        dataFile.println("Date, Time , Lat , Long , Sats , HDOP , Speed");//prints the headers for our data
        }
        dataFile.close();//file closed 
    }
    if (gps.available( gpsPort ) && (counter <= 9)) {
    gps_fix fix = gps.read();
                                   
    if (fix.valid.hdop && (fix.hdop < 2000)){ 
        bool  validDT         = fix.valid.date & fix.valid.time;
               
               // print(             fix.satellites       , fix.valid.satellites, 3             );
               // print(             fix.hdop / 1000.0      , fix.valid.hdop      , 6, 2          );
               // print(             fix.latitude ()      , fix.valid.location  , 10, 6         );
               // print(             fix.longitude()      , fix.valid.location  , 11, 6         );
               // DEBUG_PORT.println();
   
                   
    File dataFile = SD.open("gpsdata.csv", FILE_WRITE);
    digitalWrite(redled, HIGH);
    digitalWrite(greenled, LOW); //data is being written to the card
    if (dataFile) {     // if the file is available, write to it:
     
       
      if (fix.dateTime.date < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.date);
      dataFile.print( '/' );
      if (fix.dateTime.month < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.month);
      dataFile.print( '/' );
      dataFile.print(fix.dateTime.year);
      dataFile.print(',');

      if (fix.dateTime.hours < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.hours);
      dataFile.print( ':' );
      if (fix.dateTime.minutes < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.minutes);
      dataFile.print( ':' );
      if (fix.dateTime.seconds < 10)
        dataFile.print( '0' );
      dataFile.print(fix.dateTime.seconds);
      dataFile.print(',');
      dataFile.print(fix.latitude (), 10);
      dataFile.print(",");
      dataFile.print(fix.longitude(), 11);
      dataFile.print(",");
      dataFile.print(fix.satellites, 3);
      dataFile.print(",");
      dataFile.print(fix.hdop / 1000.0, 6);
      dataFile.print(",");
      dataFile.print(fix.heading (), 7);
      dataFile.print(",");
      dataFile.print(fix.speed_kph(), 7);
      dataFile.print("\n");
      dataFile.close();
    //Serial.println(counter, DEC);
    counter++;
    }
   }
   else {
      noFix();
    }
    }
    else {
  digitalWrite(greenled, HIGH);    //Lines written, no more write actions.
  if (counter >= 10){ //once 10 lines are written, called poslog, positive log to stop writing lines and wait for operator input
    poslog();
  }
    }



void noFix() //blink the red LED to show there is no fix
{
  unsigned long currentMillis = millis();
  //digitalWrite(greenled, HIGH);
    if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == HIGH) {
      ledState = LOW;
    } else {
      ledState = HIGH;
    }
  }
    // set the LED with the ledState of the variable:
    digitalWrite(redled, ledState);
}

void poslog()
  {
   unsigned long currentMillis = millis();
   digitalWrite(blueled, HIGH);
        while (digitalRead(buttonPin) == HIGH) { //Wait for the operator to press butan
          counter = 0;
          printheaders = false;
          break;
        }
        if (currentMillis - previousMillis >= interval) { //else, just blink the led
        // save the last time you blinked the LED
        previousMillis = currentMillis;

        // if the LED is off turn it on and vice-versa:
        if (ledState == HIGH) {
        ledState = LOW;
    }
    else {
      ledState = HIGH;
    }
  }
    // set the LED with the ledState of the variable:
    digitalWrite(blueled, ledState);
    }
 

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 5 gasten