Skip to content

Instantly share code, notes, and snippets.

@ljmf00
Last active March 23, 2018 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmf00/203a8274bb443275dcbe23aa32236ce6 to your computer and use it in GitHub Desktop.
Save ljmf00/203a8274bb443275dcbe23aa32236ce6 to your computer and use it in GitHub Desktop.
// ____ ____ __ ___ _ _ __ __ _
// ( _ \( _ \ / \ / __)( \/ )/ \ ( ( \
// ) __/ ) /( O )( (__ ) /( O )/ /
// (__) (__\_) \__/ \___)(__/ \__/ \_)__)
//
// Copyright (c) 2018 Luís Ferreira
//
// MIT License
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/** @file src.ino
* Initial and core file for arduino
*/
#define PROCYON_DEBUG 1
#define PROCYON_ENABLE_SD 0
#define PROCYON_ENABLE_BMP085 0
#define PROCYON_ENABLE_GPS_NEO6M 1
#define PROCYON_LOCAL_PRESSURE 100600
//#include <Wire.h>
typedef unsigned long int ulong_t;
/// Millisecond precision timer
class Timer {
public:
/// Constructor
Timer() : _t(millis()) {}
inline ulong_t elapsedMillis() { return millis() - _t; }
void reset() { _t = millis(); }
private:
ulong_t _t;
};
#if PROCYON_ENABLE_BMP085
#include <Adafruit_BMP085.h>
/// Global variables
Adafruit_BMP085 bmp;
#endif
#if PROCYON_ENABLE_GPS_NEO6M
#include <NMEAGPS.h>
#include <GPSport.h>
NMEAGPS gps;
gps_fix fix;
#define gpsPort Serial
#else
#define DEBUG_PORT Serial
#endif
#if PROCYON_ENABLE_SD
#include <SD.h>
File logfile;
Timer file_asave;
String filepath;
/// File autosaver callback
void fileAutoSave() {
logfile.close();
logfile = SD.open(filepath, FILE_WRITE);
file_asave.reset();
}
#endif
/** Setup function before main loop
* @see loop()
*/
void setup() {
//Start serial if debug mode on
#if PROCYON_DEBUG
DEBUG_PORT.begin(9600);
#if PROCYON_ENABLE_BMP085
if (!bmp.begin()) {
DEBUG_PORT.println("Could not find a valid BMP085 sensor, check wiring!");
while(true);
}
#endif
#else
#if PROCYON_ENABLE_BMP085
bmp.begin();
#endif
#endif
#if PROCYON_ENABLE_GPS_NEO6M
while (!Serial) {}
gpsPort.begin(9600);
#if PROCYON_DEBUG
DEBUG_PORT.println("\nGPS started");
#endif
#endif
#if PROCYON_ENABLE_SD
// Start timer
file_asave = Timer();
// Start SDCard
SD.begin();
// Get log filename
uint16_t i = 0;
while (SD.exists("log_" + i))
i++;
filepath = "log_" + i;
logfile = SD.open(filepath, FILE_WRITE);
file_asave.reset();
#endif
}
/** Main loop
* @see setup()
*/
void loop() {
String lastRead = String(millis());
#if PROCYON_ENABLE_BMP085
lastRead += "\t" + bmp.readTemperature() \
+ "\t" + bmp.readPressure() \
+ "\t" + bmp.readAltitude(PROCYON_LOCAL_PRESSURE);
#endif
#if PROCYON_ENABLE_GPS_NEO6M
while (gps.available( gpsPort ))
{
fix = gps.read();
if (fix.valid.location)
lastRead += "\t" + String(fix.latitude(), 6) + "\t" + String(fix.longitude(), 6);
if (fix.valid.altitude)
lastRead += "\t" + String(fix.altitude());
#if PROCYON_DEBUG
DEBUG_PORT.println(lastRead);
#endif
}
#endif
#if PROCYON_ENABLE_SD
logfile.println(lastRead);
//File autosaver
if (file_asave.elapsedMillis() > 2500)
fileAutoSave();
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment