Day 2



This above is my basic set up to test the GPS module on a battery itself. After taking this outside, it was clear that the GPS was actually working and writing to the EEPROM. From here, I will be working on attaching the actual pixels in order to have them lighting up. Right now, this is my current code with which i will have to add in bits and pieces for specific locations and for the pixels to light up specific colors at specific distances.

#include <Adafruit_GPS.h>

#include <EEPROM.h>

int addr = 0;
const int inPin = 6;     // the number of the toggle pin to enable logging
const int ledPin =  7;      // the number of the LED pin (displays if logging is on)
int buttonState = 0; // variable for reading the toggle status

// what's the name of the hardware serial port?
#define GPSSerial Serial1
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false

uint32_t timer = millis();


void setup() {
  // put your setup code here, to run once:
  // initialize the pushbutton pin as an input:
  pinMode(inPin, INPUT);
  pinMode(ledPin, OUTPUT);
  //while (!Serial);  // uncomment to have the sketch wait until Serial is ready
  
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
     
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz
     
  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  
  // Ask for firmware version
  GPSSerial.println(PMTK_Q_RELEASE);

  // clear the eeprom of stale data on inital start
  clearEeprom();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(inPin);
  // button state high when logging is on
  if (buttonState == HIGH) {
    // turn on LED to display logging is on
    digitalWrite(ledPin, HIGH);
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
  
    // this if is a place to debug if set true at top of file
    if (GPSECHO)

    if (c) Serial.print(c);
    // if a sentence is received, we can check the checksum, parse it...
    if (GPS.newNMEAreceived()) {
      // a tricky thing here is if we print the NMEA sentence, or data
      // we end up not listening and catching other sentences!
      // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
      Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
      if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
        return; // we can fail to parse a sentence in which case we should just wait for another
      }
      // if millis() or timer wraps around, we'll just reset it
      if (timer > millis()) timer = millis();
     
      // approximately every 2 seconds or so, print out (and write to eeprom) the current location
      if (millis() - timer > 2000) {
        timer = millis(); // reset the timer

        // make sure the GPS has a fix on location before continuing
        if (GPS.fix) {
          Serial.print("Location: ");
          Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
          Serial.print(", ");
          Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);

          // Save location to eeprom in the format
          // lat,lon;
          // ie. 4300.0302N,7868.9946W;
          // writen as ints (ASCII value)
          //23 spaces taken per addr saved
          //1024 space total
          // about 44 addresses able to be logged

          writeToEeprom((String)GPS.latitude,9);
          delay(1);

          writeToEeprom((String)GPS.lat,1);
          EEPROM.write(addr, (int)",");
          addr = addr + 1;
          delay(1);

          writeToEeprom((String)GPS.longitude,9);
          delay(1);

          writeToEeprom((String)GPS.lon,1);
          delay(1);
        
          // write endline char in our case ';'
          EEPROM.write(addr, (int)";");
          addr = addr + 1;
          delay(1);
      
        }
      }
    }
    
    else {
      digitalWrite(ledPin, LOW);
    }
}

// function to clear memory
int clearEeprom(){
  for(int i = 0; i< 1024; i++){
    EEPROM.write(i,"");
  }
  return 0;
}

// writes first count number of chars byte by byte
int writeToEeprom(String data, int count){
  for(int i = 0; i < count; i++){
        EEPROM.write(addr, (int)data[i]);

        addr = addr + 1;
        if (addr >= EEPROM.length()) {
          addr = 0; // resets addr counter
          break;
        }
        
      }
      return 0;
  
}

Here is the working GPS module.





Comments