Hello forum member
i am newbie to arduino and programming
with the help from forum member i build a nice tool for light aviation (paramotor/paragliding/hang gliding)
GPS that show the Ground speed and the direction in real time
What i am trying to do now is :
To write the GPS variables (speed+direction/ heading) to sd card in a 1 minute loop
something like this :
speed Heading
30 180
32 188
35 190
10 020
8 010
from the data i can see that my lowest speed is 8 kmh and direction 010
the ide is to calibrate the device with 360 degree turn and then run a query to search for the lowest speed and direction
and display the data to lcd screen
Can get help with the code or example how to write the data in array and how to query for the data
Many appreciate for the help
i attached the code and picture of the device
Thanks in advance
Itay
#include <SoftwareSerial.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <LiquidCrystal_I2C.h>
// start data loger
#include <SD.h>
#include <SPI.h>
////// end data loger
static const int RXPin = 4, TXPin = 3;
/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
TinyGPS gps;
SoftwareSerial ss(RXPin, TXPin);
static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
float TakeoffLA ;
float TakeoffLO ;
int CS_PIN = 10;
File GPSDATA;
void Takeofflocation()
{
}
void setup()
{
Serial.begin(115200);
lcd.begin(20,4);
ss.begin(9600);
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
delay(1000);
float flat , flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
gps.f_get_position(&flat ,&flon, &age) ;
// delay(1000);
SD.begin(CS_PIN);
TakeoffLA = flat;
TakeoffLO = flon;
}
void loop()
{
float flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
gps.f_get_position(&flat, &flon, &age);
print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
float GPSSPEED = gps.f_speed_kmph() ;
//TakeoffLA = flat;
//TakeoffLO = flon;
float COURSE = gps.f_course();
float GPSALT = gps.f_altitude() ;
float GPSALT1 = round(GPSALT*10)/1000;
print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Distance = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
GPSDATA= SD.open("GPSData.txt",FILE_WRITE);
if (GPSDATA) {
gps.stats(&chars, &sentences, &failed);
Serial.println();
Serial.println("GPS-GROUND SPEED IS :");
Serial.println(GPSSPEED);
Serial.println("GPS Takeofflocation LAT");
Serial.println(TakeoffLA);
Serial.println("GPS Takeofflocation LON");
Serial.println(TakeoffLO);
Serial.println("GPS CORDINATE LAT NOW");
Serial.println(flat);
Serial.println("GPS CORDINATE LON NOW");
Serial.println(flon);
Serial.println("Distance from takeoff");
// GPSDATA.print("GPSSPEED");
// GPSDATA.println("SPEED");
GPSDATA.print("S");
GPSDATA.print(GPSSPEED);
GPSDATA.print("H");
GPSDATA.print(COURSE);
// GPSDATA.println("------------");
// GPSDATA.println("COURSE");
// GPSDATA.println(COURSE);
GPSDATA.close();
}
if (flat == TinyGPS::GPS_INVALID_F_ANGLE)
{
// lcd.print("N/A");
}
else
{
unsigned long distance = TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000;
if (distance!= 0xFFFFFFFF)
{
lcd.setCursor(10,3);
// lcd.print(distance-13214);
lcd.print(distance);
print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
}
else
{
lcd.print(F("Invalid distance"));
}
}
//float Bearing = (flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
if (flat == TinyGPS::GPS_INVALID_F_ANGLE)
{
lcd.setCursor(13,1);
// lcd.print("N/A");
}
else
{
unsigned long course_to1 = TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO);
if (1!= 2)
lcd.setCursor(9,1);
lcd.print(course_to1);
}
//print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, TakeoffLA, TakeoffLO) / 1000, 0xFFFFFFFF, 9);
Serial.println(" ");
Serial.println("Bearing");
print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, TakeoffLA, TakeoffLO), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
Serial.println(" ");
Serial.println("bearing from variable same lcd");
Serial.println( Bearing);
smartdelay(1000);
lcd.setCursor(0,0);
lcd.print("Speed");
lcd.setCursor(9,0);
lcd.print(GPSSPEED);
lcd.setCursor(17,0);
lcd.print("Kmh");
lcd.setCursor(0,2);
lcd.print("Altitude");
lcd.setCursor(10,2);
lcd.print(GPSALT);
lcd.setCursor(13,2);
lcd.print(" Meter");
lcd.setCursor(0,1);
// lcd.print("Course");
// Serial.println(gps.altitude.meters())
lcd.print("Bearing");
// lcd.setCursor(10,1);
lcd.setCursor(12,1);
lcd.print((char)223);
lcd.setCursor(14,1);
lcd.print("H");
/// H for heading
lcd.setCursor(16,1);
lcd.print(COURSE);
lcd.setCursor(19,1);
lcd.print((char)223);
lcd.setCursor(0,3);
lcd.print("Takeoff");
lcd.setCursor(18,3);
lcd.print("Km");
}
static void smartdelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void print_float(float val, float invalid, int len, int prec)
{
if (val == invalid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(' ');
}
smartdelay(0);
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
char sz[32];
if (val == invalid)
strcpy(sz, "*******");
else
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz = ' ';
- if (len > 0)*
- sz[len-1] = ' ';*
- Serial.print(sz);*
- smartdelay(0);*
}
static void print_date(TinyGPS &gps)
{ - int year;*
- byte month, day, hour, minute, second, hundredths;*
- unsigned long age;*
- gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);*
- if (age == TinyGPS::GPS_INVALID_AGE)*
_ Serial.print("********** ******** ");_ - {*
- char sz[32];*
- sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",*
- month, day, year, hour, minute, second);*
- Serial.print(sz);*
- }*
- print_int(age, TinyGPS::GPS_INVALID_AGE, 5);*
- smartdelay(0);*
}
static void print_str(const char *str, int len)
{ - int slen = strlen(str);*
- for (int i=0; i<len; ++i)*
_ Serial.print(i<slen ? str : ' ');_
* smartdelay(0);*
}
