0% found this document useful (0 votes)
71 views6 pages

Arduino GPS Vehicle Speed Monitor

This document contains code for detecting vehicle speed using a GPS module. It includes libraries for LCD display, software serial communication, and GPS parsing. The setup function initializes the LCD and serial ports. The main loop function gets time and location data from the GPS, calculates speed in km/h, displays it on the LCD, and prints it over serial. It delays between readings to allow the GPS time to acquire satellites and get an accurate fix. Several static functions format and print GPS data components like time, date, and strings to the serial port.

Uploaded by

anon_645372799
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views6 pages

Arduino GPS Vehicle Speed Monitor

This document contains code for detecting vehicle speed using a GPS module. It includes libraries for LCD display, software serial communication, and GPS parsing. The setup function initializes the LCD and serial ports. The main loop function gets time and location data from the GPS, calculates speed in km/h, displays it on the LCD, and prints it over serial. It delays between readings to allow the GPS time to acquire satellites and get an accurate fix. Several static functions format and print GPS data components like time, date, and strings to the serial port.

Uploaded by

anon_645372799
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include<LiquidCrystal.

h> // lcd(7, 6, 5, 4, 3, 2);

#include <SoftwareSerial.h>

#include <TinyGPS++.h>

#include <SPI.h>

#include <Wire.h>

char timeinmin[32];

String speedinkm;

int f=0;

int valid=0;

static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;

void setup()

[Link](16,2); // Start lcd n semua pin

[Link](9600);

[Link](GPSBaud);

[Link](speedinkm);

void loop()

Time([Link]); // start function gps

valid=[Link](); // detect location guna gps sebab equation speed =distance/time

f = round( [Link]());
speedinkm = String(f);

[Link](speedinkm);

[Link]();

smartDelay(500);

Display_data();

[Link]("Detecting Vehicle Speed"); // initial display sblm detect speed

[Link](0,1);

[Link]()

static void Time(TinyGPSTime &t)

sprintf(timeinmin, "%02d:%02d", [Link](), [Link]());

// timeinmin[0]='1';

//timeinmin[1]='9';

[Link](timeinmin);

//[Link](" ");

// [Link](timeinmin[1]);

int temp1 = (int)timeinmin[0];

temp1=temp1-48;

temp1=temp1*10;

temp1=temp1+((int)timeinmin[1]-48);
temp1=temp1+5;

if(temp1>=24){temp1=temp1-24;}

char a[2];

itoa(temp1, a, 10);

timeinmin[0]=a[0];

timeinmin[1]=a[1];

static void smartDelay(unsigned long ms)

unsigned long start = millis();

do

while ([Link]())

[Link]([Link]());

} while (millis() - start < ms);

static void printFloat1(float val, bool valid, int len, int prec)

if (!valid)

while (len-- > 1)

[Link]('*');
[Link](' ');

else

[Link](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)

[Link](' ');

smartDelay(0);

static void printInt(unsigned long val, bool valid, int len)

char sz[32] = "*****************";

if (valid)

sprintf(sz, "%ld", val);

sz[len] = 0;

for (int i=strlen(sz); i<len; ++i)

sz[i] = ' ';

if (len > 0)

sz[len-1] = ' ';

[Link](sz);
smartDelay(0);

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)

if (![Link]())

[Link](F("********** "));

else

char sz[32];

sprintf(sz, "%02d/%02d/%02d ", [Link](), [Link](), [Link]());

[Link](sz);

if (![Link]())

[Link](F("******** "));

else

char sz[32];

sprintf(sz, "%02d:%02d:%02d ", [Link](), [Link](), [Link]());

[Link](sz);
}

printInt([Link](), [Link](), 5);

smartDelay(0);

static void printStr(const char *str, int len)

int slen = strlen(str);

for (int i=0; i<len; ++i)

[Link](i<slen ? str[i] : ' ');

smartDelay(0); // detect speed smpai sini je

Common questions

Powered by AI

The formatting functions like `printDateTime` and `printStr` help standardize and organize output data for display or logging. `printDateTime` formats the GPS date and time into a human-readable format, handling potential invalid data scenarios, while `printStr` ensures strings are consistently displayed by filling the remaining length with spaces. These functions contribute to the clarity and usability of the output by uniformly formatting data.

The `sprintf` function is used for formatting strings with specific patterns and placing numbers into strings. This function allows complex formatting operations like zero-padding, specific width alignment, and embedding variable data into formatted strings, offering a powerful and flexible way to produce formatted output for display or logging. It is especially useful for preparing combined date-time strings and other structured text outputs in this code.

The `lcd.print` function displays text on the LCD at the current cursor position. By using `lcd.setCursor`, you can specify the exact position (column and row) where text begins to print. This combination enables precise control over where text is displayed on the LCD screen, allowing for dynamic updates and clear formatting, such as displaying different information on separate lines.

The `smartDelay` function processes incoming GPS data during the delay by checking if there is any available data from the Serial communication and encoding it using `gps.encode(Serial1.read())`. This ensures that even while other operations might be paused within a delay interval, the GPS data is continuously being processed and updated.

The speed is calculated using the GPS's speed data, where the equation speed = distance/time is referenced. In the code, the speed in kilometers per hour is retrieved from `gps.speed.kmph()`, rounded using `round()`, and then stored as an integer in the variable `f`. The speed is then converted to a string in variable `speedinkm`.

In the `Time` function, time is formatted into hours and minutes from GPS data and stored as a string `timeinmin`. An adjustment of 5 hours is added to account for a time zone or offset. If this adjustment results in a value greater than or equal to 24, it adjusts by subtracting 24 to maintain a 24-hour format. This ensures the time remains valid and formatted correctly after the offset is applied.

The `printFloat1` function formats a floating-point number to a specified precision and length. If the data is invalid (as indicated by the `valid` flag being false), it prints asterisks in place of the number to signify unavailable or incorrect data. If the data is valid, the function calculates the required spacing and prints the number with the specified precision, ensuring consistent formatting of output.

Invalid GPS data is handled by checking the validity flags before processing or displaying the data. For example, the `printFloat1` and `printDateTime` functions check the validity of data (using `valid` flags) and selectively print placeholder characters (e.g., asterisks) if the data is invalid. This approach prevents incorrect information from being displayed or logged, ensuring data integrity and reliability.

`SoftwareSerial` allows serial communication on other digital pins of an Arduino, but it has significant limitations such as consuming CPU cycles due to bit-banging, limited data rate capability (less than hardware serial ports), and potential conflicts if used with other intensive operations simultaneously. Additionally, it can handle only one incoming and one outgoing stream at a time, which could limit data throughput effectiveness in GPS applications.

The `TinyGPSPlus` library is used to interface with GPS modules to parse and process GPS data. It provides the necessary functions to retrieve data such as location, speed, and time from GPS signals. In this code, it is specifically used to detect location and calculate speed.

You might also like