Advertisement
amirulhakim25

GPS based ELF magnetic measuring system

Nov 26th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <SPI.h>
  3. #include <SD.h>
  4. #include <TinyGPS.h>
  5.  
  6. TinyGPS gps;
  7. // The jumpers on the shield set to work with
  8. // these pins, and can be changed if needed.
  9. // create gps object
  10. SoftwareSerial ss(2, 3);
  11.  
  12. // File for logging data
  13. File rawdata;
  14.  
  15. const int buttonPin = 2;           // the number of the pushbutton pin
  16. const int ledPin = 8;              // the number of the LED pin
  17.  
  18. int buttonState = 0;              // variable for reading the pushbutton status
  19.  
  20. /**************************************************************
  21. Function: setup
  22. Purpose: set up Arduino
  23. Args: none
  24. Returns: nothing
  25. Notes: This function is required by the Arduino
  26. ***************************************************************/
  27. void setup()
  28. {
  29. pinMode(buttonPin, INPUT);
  30. // initialize the push button pin as an input
  31. pinMode(buttonPin, OUTPUT);
  32. // initialize the LED as an output
  33.  
  34. // Serial monitor
  35. Serial.begin(9600);
  36. // baud rate for GPS - 38400 is prefered, but 4800 can also be used
  37. ss.begin(38400);
  38.  
  39. // Comments from TinyGPS simple_test example
  40. Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  41. Serial.println("by Mikal Hart");
  42. Serial.println();
  43.  
  44. // Initialize card
  45. Serial.print("Initializing SD card...");
  46.  
  47. pinMode(10, OUTPUT);
  48. if (!SD.begin(10)) {
  49. Serial.println("...initialization failed!");
  50. }
  51. else
  52. Serial.println("...initialization done.");
  53.  
  54. }
  55.  
  56. /**************************************************************
  57. Function: loop
  58. Purpose: loop funtion for Arduino
  59. Args: none
  60. Returns: nothing
  61. Notes: This function is required by the Arduino, and the
  62. Arduino will loop through this function indefinately.
  63.  
  64. ***************************************************************/
  65. void loop(){
  66.  
  67. bool newData = false;
  68. unsigned long chars;
  69. unsigned short sentences, failed;
  70.  
  71. // Open data file
  72. File rawdata = SD.open("rawdata.txt", FILE_WRITE);
  73.  
  74. // For one second we parse GPS data and report some key values
  75.  
  76. buttonState = digitalRead(buttonPin);         // read the state of the pushbutton value
  77.  
  78. for (unsigned long start = millis(); millis() - start < 1000;)
  79. {
  80. if(buttonState == HIGH){
  81. // turn LED on
  82. delay(500);
  83. digitalWrite(ledPin, HIGH);
  84. while (ss.available())
  85. {
  86. char c = ss.read();   //GPS data flowing
  87. // Send raw data to the terminal window
  88. Serial.write(c);
  89.  
  90. // Send raw data to the SD card
  91. if(rawdata)
  92. rawdata.write(c);
  93.  
  94. if (gps.encode(c))          // if there is a new valid sentence
  95. newData = true;
  96. }
  97. }
  98. // Send manipulate data to the terminal window.
  99. /*
  100. if (newData)
  101. {
  102. float flat, flon;
  103. unsigned long age;
  104. gps.f_get_position(&flat, &flon, &age);
  105. Serial.print("LAT=");
  106. Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
  107. Serial.print(" LON=");
  108. Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
  109. Serial.print(" SAT=");
  110. Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
  111. Serial.print(" PREC=");
  112. Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
  113. }
  114. */
  115. // Send a carriage return and close the file
  116. Serial.println("");
  117. rawdata.write("\r");
  118. rawdata.close();
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement