void setup()
{
//Serial.begin(115200);
nss.begin(9600);
// Configuration de l'afficheur 16 caractères en 2 lignes
lcd.begin(16, 2);
// creation des caracteres speciaux
lcd.createChar(8, NO);
lcd.createChar(1, N);
lcd.createChar(2, NE);
lcd.createChar(3, E);
lcd.createChar(4, SUDEST);
lcd.createChar(5, S);
lcd.createChar(6, SO);
lcd.createChar(7, O);//*/
lcd.clear();
lcd.print("Init");
delay(100);
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (SD.begin(10))
{
memset(kmprev,0,sizeof(kmprev));
// on lit le nombre de km parcouru jusqu'ici
File dataFile = SD.open("datalog.txt");
// if the file is available, write to it:
if (dataFile)
{
while (dataFile.available())
{
byteIn = dataFile.read();
if (bufPos < sizeof(kmprev))
kmprev[bufPos++] = byteIn;
}
dataFile.close();
kmcount = strtod(kmprev, NULL);
delay(100);
}
// if the file isn't open, pop up an error:
}
else
{
lcd.setCursor(0, 1);
lcd.print("SD");
delay(500);
}//*/
newData = false;
lcd.clear();
lcd.print("No GPS");
// For one second we parse GPS data and report some key values
while (newData==false)
{
while (nss.available())
{
b = nss.read();
//Serial.write(b); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(b))
{
// Did a new valid sentence come in?
newData = true;
gps.f_get_position(&flat, &flon, &age);
lastLat=flat;
lastLon=flon;
}
}
}
lcd.clear();
delay(10);
}
void loop()
{
/*-----------------------------------------------------------------------------------
============================GPS======================================================
-----------------------------------------------------------------------------------*/
newData = false;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (nss.available())
{
b = nss.read();
//Serial.write(b); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(b)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData == true)
{
gps.f_get_position(&flat, &flon, &age);
intVitesse=(gps.f_speed_kmph())*1.10;
if (kmState>=5)
{
kmcount=kmcount+(TinyGPS::distance_between(flat, flon, lastLat, lastLon) / 1000);
kmState=0;
lastLat=flat;
lastLon=flon;
intkm = kmcount;
enregistrer(intkm);
}
/*-----------------------------------------------------------------------------------
============================GPS=END==================================================
-----------------------------------------------------------------------------------*/
//bigFont(intVitesse); // affichage de la vitesse en gros, fonctionne seulement si inferieur a 999!
//lcd.print("Km/h");
String point = TinyGPS::cardinal(gps.f_course());
lcd.setCursor(0, 0);
delay(10);
if (point.equalsIgnoreCase("NO"))
{
lcd.write(2);
}
else if (point.equalsIgnoreCase("NE"))
{
lcd.write(8);
}
else if (point.equalsIgnoreCase("E"))
{
lcd.write(7);
}
else if (point.equalsIgnoreCase("SE"))
{
lcd.write(6);
}
else if (point.equalsIgnoreCase("S"))
{
lcd.write(5);
}
else if (point.equalsIgnoreCase("SO"))
{
lcd.write(4);
}
else if (point.equalsIgnoreCase("O"))
{
lcd.write(3);
}
else
{
lcd.write(1);
}
delay(10);
// on gere l'affichage des options heure et temperature
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
lcdState ++;
previousMillis = currentMillis;
}
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
switch (lcdState)
{
case 1:
val = gps.satellites();
unite = (" Sat");
break;
case 2:
unite = ("'A ");
val = (( (float) ((((float)(analogRead(tmpPin)))*5000.0)/1023.0) - 500.0) /10.0);
break;
default:
kmState++;
unite = ("'M ");
val=thermocouple.readCelsius();
lcdState = 0;
}
//sprintf(ligneA, "%03dkm/h %02d:%02d", intVitesse , hour+2, minute);
//sprintf(ligneB, "%dkm %d%s", intkm, val, unite);
String ligneA=" ";
String ligneB="";
ligneA += showDigits(intVitesse, 3);
ligneA += "km/h ";
ligneA += showDigits(hour+2, 2);
ligneA += ":";
ligneA += showDigits(minute, 2);
ligneB += showDigits(intkm, 5);
ligneB += "km ";
ligneB += showDigits(val, 2);
ligneB += unite;
lcd.print(ligneA);
Serial.println(ligneA);
delay(10);
lcd.setCursor(0, 1);
delay(10);
lcd.print(ligneB);
Serial.println(ligneB);
delay(10);
lcd.home();
}
delay(40);
}
String showDigits(int digits, int n)
{
String string;
// utility function for digital clock display: prints leading 0
if(digits < 1)
{
for (int r=0; r<n; r++)
{
string += '0';
}
}
else if(digits < 10)
{
for (int r=0; r < (n-1); r++)
{
string += '0';
}
string += digits;
}
else if(digits < 100)
{
for (int r=0; r < (n-2); r++)
{
string += '0';
}
string += digits;
}
else if(digits < 1000)
{
for (int r=0; r < (n-3); r++)
{
string += '0';
}
string += digits;
}
else if(digits < 10000)
{
for (int r=0; r < (n-4); r++)
{
string += '0';
}
string += digits;
}
else if(digits < 100000)
{
for (int r=0; r < (n-5); r++)
{
string += '0';
}
string += digits;
}
else
{
string += digits;
}
return string;
}