Notice to YSPilots/YSFLIGHT

Notice to YSPilots/YSFLIGHT
Legacy Pack available under the YSFLIGHT category.
Any individual requests for a model must be made to my email address, see bottom of the page..
Enjoy!
Skippy

Sunday, 5 January 2014

Project Freja: Final Code

So, a nice little code dump here. This is the final code dump of Project Freja. This logged the moisture, altitude, accuracy (in m), position and date to the SD card. Hope you can make something of it!

//Newest as of 02/11/2013
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(0, 1, 5, 4, 3, 2);
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <math.h>
SoftwareSerial mySerial(8, 7);
const int sensorPin = A0;
const float baselineMoisture = 0.0;
Adafruit_GPS GPS(&mySerial);
boolean usingInterrupt = false;
void useInterrupt(boolean);
#define chipSelect 10 //SD card define
File logfile;
int switchState = 0;

//Function
// convert NMEA coordinate to decimal degrees
float decimalDegrees(float nmeaCoord) {
  uint16_t wholeDegrees = 0.01*nmeaCoord;
  return wholeDegrees + (nmeaCoord - 100.0*wholeDegrees)/60.0;
}
//EndFunction

//Function
//To convert HDOP into metre range
float Accu(float x1) {
  float result;
  result = sqrt(sq(x1) * 44.89 + 1);
  return result;
}
//EndFunction

//Function Read Hex Value Return decimal

uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}
//Function end
void setup() 
{
   pinMode(9, INPUT); //The Switch input
   lcd.begin(16,2);
//Serial.begin(115200);
       // connect to the GPS at the desired rate
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 or 5 Hz update rate
  GPS.sendCommand(PGCMD_NOANTENNA);
    useInterrupt(true);

  delay(1000);
  mySerial.println(PMTK_Q_RELEASE);
}


SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
   pinMode(10, OUTPUT); //SD card select
   if (!SD.begin(10)) {
  //  Serial.println("initialization failed!");
    return;
  }
}

uint32_t timer = millis();
 
void loop()                     // run over and over again
{
  int sensorVal = analogRead(sensorPin);
  switchState = digitalRead(9);
#  // in case you are not using the interrupt above, you'll
  // need to 'hand query' the GPS, not suggested :(
  if (! usingInterrupt) {
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
 
  }
 
  // 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();
 
if ((GPS.fix)&& (switchState == HIGH)) {
   logfile = SD.open("GPSlog.txt", FILE_WRITE);
//Serial.print("Fix & High");
   logfile.print(decimalDegrees(GPS.latitude), 5); logfile.print(",-");
   logfile.print(decimalDegrees(GPS.longitude), 5); logfile.print(",");
   logfile.print(sensorVal);
   logfile.print(",");
   logfile.print(Accu(GPS.HDOP));
   logfile.print(",");
   logfile.println(GPS.altitude);
   logfile.close();
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Logged to Card");
   delay(1000);
}
 
  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) {
    timer = millis(); // reset the timer
   
//  Serial.print(" quality: "); Serial.println((int)GPS.fixquality);

    if (GPS.fix) {
 
lcd.clear();
lcd.setCursor(0,0);
lcd.print(decimalDegrees(GPS.latitude), 5);
      lcd.print(",+-");
      lcd.print(Accu(GPS.HDOP), 1);
      lcd.print("m");
      lcd.setCursor(0,1);
      lcd.print(decimalDegrees(GPS.longitude), 5);
      lcd.print(",");

      lcd.print(sensorVal); lcd.print("%");
      lcd.print(GPS.speed, 1);
      
      }
      if (!GPS.fix) {
        lcd.clear();
       
        lcd.setCursor(0,0);
        lcd.print("No Fix");
      }
       }
}

No comments: