Sorry...
/* CONNEXIONS :
GPS
RX : 3
TX : 2
SD CARD
MOSI : 11
MISO : 12
CLK : 13
CS : 10
Red Led : 5
Green Led : 6
*/
#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include <SD.h>
#define Rled 5
#define Gled 6
#define LEDON 125
#define LEDOFF 0
#define SDChipSelect 10
TinyGPS gps;
NewSoftSerial nss(2, 3);
boolean actualiseGPS();
unsigned long LogLastUpdate = 0;
unsigned long DELAI_LOGGER = 5000;
boolean Track = true;
void setup(){
byte month, day, hour, minute, second, hundredths;
int year;
unsigned long age;
boolean output = HIGH;
Serial.begin(9600);
pinMode(Rled,OUTPUT);
pinMode(Gled,OUTPUT);
pinMode(SDChipSelect, OUTPUT); //SS obligatoire sur le pin 10
analogWrite(Rled, LEDON);
analogWrite(Gled, LEDOFF);
//initialisation de la carte SD
if(!SD.begin(SDChipSelect)){
while(1){
analogWrite(Rled, LEDON);
delay(250);
analogWrite(Rled, LEDOFF);
delay(250);
}
}
//on vérifie que l'entête du fichier correspond à ce que l'on veut
File dataFile = SD.open("data.txt");
if (dataFile){
if (dataFile.size() < 66 ){
dataFile.close();
SD.remove("data.txt");
File dataFile = SD.open("data.txt", FILE_WRITE);
dataFile.println("type,new_track,time,latitude,longitude,alt,speed,name");
dataFile.close();
}
}
//initialisation du GPS
nss.begin(9600);
Serial.print("Attente GPS...");
while(!actualiseGPS()){
if(output) analogWrite(Gled, LEDON);
else analogWrite(Gled, LEDOFF);
output = !output;
delay(100);
}
Serial.println("OK");
}
void loop(){
float vitesse;
//on allume la led verte
analogWrite(Rled, LEDOFF);
analogWrite(Gled, LEDON);
//on actualise le GPS
while(!actualiseGPS()){ //tant qu'on a pas de données
analogWrite(Rled, LEDON); //on laisse la led rouge allumée
}
analogWrite(Rled, LEDOFF); //on éteint la led rouge en sortant
vitesse = gps.f_speed_kmph(); //on lit la vitesse
Serial.print("Vitesse actuelle : ");
Serial.println(vitesse);
if (vitesse < 2 || vitesse > 500){
DELAI_LOGGER = 300000; // toutes les 5 minutes
}else if(vitesse > 75){ // > 80km/h
DELAI_LOGGER = 1000; // toutes les secondes
}else if(vitesse > 45){ // > 45km/h
DELAI_LOGGER = 2000; // toutes les 2 secondes
}else if(vitesse > 25){ // > 25km/h
DELAI_LOGGER = 3000; // toutes les 3 secondes
}else{
DELAI_LOGGER = 5000; // toutes les 5 secondes
}
//forçage pour test de DELAI_LOGGER
//DELAI_LOGGER = 2000;
if((millis() - LogLastUpdate) > DELAI_LOGGER){
GPS2SD(gps);
LogLastUpdate = millis();
}
}
void GPS2SD(TinyGPS &gps){
// type,new_track,time,latitude,longitude,alt,speed,course,name
float flat, flon, falt, fspeed;
byte month, day, hour, minute, second, hundredths;
int year;
unsigned long age;
File dataFile = SD.open("data.txt", FILE_WRITE); //on ouvre le fichier
if (dataFile){
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
gps.f_get_position(&flat, &flon, &age);
falt = gps.f_altitude();
fspeed = gps.f_speed_kmph();
dataFile.print('T');
dataFile.print(',');
if(Track){
dataFile.print('1');
Track = false;
}else dataFile.print('0');
dataFile.print(',');
if(static_cast<int>(hour + 2) < 10) dataFile.print('0');
dataFile.print(static_cast<int>(hour + 2));
dataFile.print(':');
if(static_cast<int>(minute) < 10) dataFile.print('0');
dataFile.print(static_cast<int>(minute));
dataFile.print(':');
if(static_cast<int>(second) < 10) dataFile.print('0');
dataFile.print(static_cast<int>(second));
dataFile.print(',');
printFloatSD(flat,5,dataFile);
dataFile.print(',');
printFloatSD(flon,5,dataFile);
dataFile.print(',');
printFloatSD(falt,0,dataFile);
dataFile.print(',');
printFloatSD(fspeed,0,dataFile);
dataFile.print(',');
if(static_cast<int>(day) < 10) dataFile.print('0');
dataFile.print(static_cast<int>(day));
dataFile.print('/');
if(static_cast<int>(month) < 10) dataFile.print('0');
dataFile.print(static_cast<int>(month));
dataFile.print('/');
dataFile.print(year);
dataFile.println();
dataFile.close();
}
}
void printFloatSD(double number, int digits, File &dataFile)
{
// Handle negative numbers
if (number < 0.0)
{
dataFile.print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
dataFile.print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
dataFile.print(".");
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
dataFile.print(toPrint);
remainder -= toPrint;
}
}
void printFloatSerial(double number, int digits)
{
// Handle negative numbers
if (number < 0.0)
{
Serial.print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
Serial.print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
Serial.print(".");
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
Serial.print(toPrint);
remainder -= toPrint;
}
}
boolean actualiseGPS(){
while (nss.available()){
if (gps.encode(nss.read())) return true;
}
return false;
}