arduino-tech

Arduino Program Files

Below is a code listing of the debug enabled version of the Arduino code for the logger. This version parses the GPS every 15 seconds, checks for a heading change of 30 degrees or more and then writes a CSV formatted report to the GPS.LOG file. The program also writes a report every 30 minutes regardless of movement.

There are lots of comments throughout the code that help explain the functionality of the program. Please feel free to make any changes you want to make the program more well suited to your purpose.

Program Explanation

The first section of the program code are compiler directives that tell the compiler to include three libraries.

  • SoftwareSerial.h - A library that allows you to use serial communication over other digital pins of the Arduino.
  • TinyGPS.h - Allows basic communication with the GPS and makes it easier to retrieve data from the GPS.
  • SD.h - Enables the Arduino to communicate with the SD card shield.

The next section declares the global constants and variables that are used by the program.

  • POW_PIN - Specifies the pin that provides the power for the SD card shield. In this case pin 8
  • CS - The chip select pin. This is the hardware SS pin. Usually pin 10 on the Uno.
  • LED - Pin 9. This is connected to the green LED which flashes when the Arduino is writing to the SD card.
  • maximum_report_interval = 1800000 - This is the maximum interval (default is 30 minutes) after which the data logger will output a report. This happens regardless if the logger is moving or stationary. Note this value is the number of milliseconds in 30 minutes.
  • normal_report_interval = 15000 - Specifies how often to parse the GPS data. Since the data logger was intended to be used in a motor vehicle it was assumed that the vehicle couldn't travel too far in 15 seconds to make much of a difference in the reported position. Also specified in milliseconds.
  • max_course_deviation = 30 - Parameter that controls the minimum heading change that the GPS must encounter before logging a report. 30 degrees seemed like a number that balances changes in course with turns that a vehicle normally makes.

Remember, all these values are just starting points. Feel free to make changes to them and see what happens to your maps. If you lower maximum_report_interval you will receive more data reports when your logger is stationary. If you lower normal_report_interval your heading will be checked more often (and in version 2 of the program, your distance traveled). This will result in more data points and a smoother path but is probably not necessary unless the vehicle travels faster than normal highway speeds.

The next section declares variables to be used by the program. There isn't really anything here unusual so I won't provide any explanations.

Functions

Coming from a background in Pascal programming I am a very structured type of programmer and believe in functions as a way to re-use code. Hence a lot of my programs may have more functions than the average Arduino program but that is just my programming style.

  • write_header function - This function creates the GPS.LOG file and writes out the CSV header. This is only run once at the beginning of the program and only if the GPS.LOG file does not already exist on the SD card. If the file does exist then the program adds data to the file. It doesn't overwrite it.
  • setup_card function - This function initializes the communication with the SD card. If there is an error the program aborts since we are using the SD card to store the data.
  • setup function - As you know, all Arduino programs must have a setup function. This function is run once at program startup. This is a simple function which sets up the pin modes of the various output pins for the Uno. It also initalizes some variables and turns off the green LED. It initializes the connection to the GPS (which is at 9600 baud). Finally, it calls the setup_card function to initialize the communications with the SD card shield.
  • write_data function - Writes out the data to the GPS.LOG file. It turns on the green LED to let the user know that it is writing to the SD card. In this function we are passing it the type_of_report and depending on the type of report we write out a message to the serial console and flash the LED. For report types of "I" we flash the LED 3 times and for the other types of reports we flash it only once. The data is then written to the log file. The log file is closed and the LED is turned off.
  • loop function - All Arduino programs have a loop function. This is the function that is called after the setup function is executed and it continues to run while the program is running. In the loop I check the time to determine if 30 minutes have passed since the last time through the loop. If so, a report type of "I" is recorded to the log file. The loop then checks to see if 15 seconds have passed. If so, the program checks for valid GPS data. If there is valid data coming from the GPS the program grabs that data by calling the gpsdump function. If valid data has been parsed from the GPS it then checks to see if a course deviation of more than 30 degrees has been made. If so a report type of "R" is logged. In version 2 the loop also checks to see if the distance traveled is more than 3/4 of a mile (a report type of "D").
  • gpsdump function - This function came directly from the sample program files provided by the TinyGPS library. Basically, it grabs data from the GPS and prints it to the screen (version 1 only). It also stores the data in global variables to be used by the other functions.
  • feedgps function - Checks to see if there is any data available from the GPS. If so it returns true to the calling routine. If not, it returns false. This is used by the loop function above.

Version 2