Created
July 29, 2020 22:41
-
-
Save duanebester/3e028d9078dc89f665b836e3f2eab586 to your computer and use it in GitHub Desktop.
Arduino Tracker Datalogger Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SparkFun_Ublox_Arduino_Library.h> | |
#include <Arduino_LSM9DS1.h> | |
#include <Arduino_APDS9960.h> | |
#include <SPI.h> | |
#include <SD.h> | |
// | |
// ---- For Arduino Nano 33 BLE Sense board ---- | |
// Connected to MKR Mem Shield & MKR GPS Shield | |
// ~ Note: GPS Module connected by UART | |
// ------------- By @duanebester --------------- | |
// SD Vars | |
File dataFile; | |
int chipSelect = 4; | |
// GPS Vars | |
SFE_UBLOX_GPS myGPS; | |
long latitude = 0; | |
long longitude = 0; | |
long speed = 0; | |
byte satellites = 0; | |
int timeout = 50; | |
long lastGPSTime = 0; | |
// IMU Vars | |
float ax, ay, az; | |
float gx, gy, gz; | |
// Misc | |
bool DEBUG = true; | |
int counter = 0; | |
#define LEDR (22u) | |
#define LEDG (23u) | |
#define LEDB (24u) | |
void setup() | |
{ | |
// Setup User Terminal | |
Serial.begin(115200); // UART to PC/Mac | |
while(!Serial); | |
Serial1.begin(9600); // UART to GPS | |
while(!Serial1); | |
// Initialize digital pin LED_BUILTIN as an output. | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(LEDR, OUTPUT); | |
pinMode(LEDB, OUTPUT); | |
pinMode(LEDG, OUTPUT); | |
digitalWrite(LEDR, HIGH); // LOW triggered LED.... | |
digitalWrite(LEDG, HIGH); | |
digitalWrite(LEDB, HIGH); | |
// Setup GPS | |
if (!myGPS.begin(Serial1)) { | |
Serial.println(F("GPS not detected!")); | |
while (1); | |
} | |
Serial.println("GPS Started!"); | |
// Setup SD Card | |
if (!SD.begin(chipSelect)) { | |
Serial.println("SD Card failed or not present!"); | |
while (1); | |
} | |
if(SD.exists("DATALOG.CSV")) { | |
SD.remove("DATALOG.CSV"); | |
dataFile = SD.open("DATALOG.CSV", FILE_WRITE); | |
dataFile.close(); | |
} | |
delay(500); // Make sure existing DATALOG.CSV file is gone | |
dataFile = SD.open("DATALOG.CSV", FILE_WRITE); | |
dataFile.println("Count,AccX,AccY,AccZ,GyrX,GyrY,GyrZ,Lat,Long,Speed"); | |
Serial.println("SD Card Initialized!"); | |
// Setup Gesture Sensor | |
if (!APDS.begin()) { | |
Serial.println("Error initializing gesture sensor!"); | |
} | |
APDS.setGestureSensitivity(85); | |
Serial.println("Gesture sensor initialized!"); | |
// Setup IMU | |
if (!IMU.begin()) { | |
Serial.println("Failed to initialize IMU!"); | |
while(1); | |
} | |
Serial.println("IMU initialized!"); | |
} | |
void loop() | |
{ | |
if (millis() - lastGPSTime > 500) { | |
lastGPSTime = millis(); // Update the timer | |
latitude = myGPS.getLatitude(timeout); | |
longitude = myGPS.getLongitude(timeout); | |
speed = myGPS.getGroundSpeed(timeout); | |
satellites = myGPS.getSIV(timeout); | |
if(DEBUG) { | |
Serial.print(F("Lat: ")); | |
Serial.print(latitude); | |
Serial.print(F(" Long: ")); | |
Serial.print(longitude); | |
Serial.print(F(" (degrees * 10^-7)")); | |
Serial.print(F(" Speed: ")); | |
Serial.print(speed); | |
Serial.print(F(" (mm/s)")); | |
Serial.print(F(" satellites: ")); | |
Serial.println(satellites); | |
} | |
} | |
if (IMU.accelerationAvailable()) { | |
IMU.readAcceleration(ax, ay, az); | |
if(DEBUG) { | |
Serial.print(F("Accel x: ")); | |
Serial.print(ax); | |
Serial.print(F(" y: ")); | |
Serial.print(ay); | |
Serial.print(F(" z: ")); | |
Serial.println(az); | |
} | |
} | |
if (IMU.gyroscopeAvailable()) { | |
IMU.readGyroscope(gx, gy, gz); | |
if(DEBUG) { | |
Serial.print(F("Gyro x: ")); | |
Serial.print(gx); | |
Serial.print(F(" y: ")); | |
Serial.print(gy); | |
Serial.print(F(" z: ")); | |
Serial.println(gz); | |
} | |
} | |
if (APDS.gestureAvailable()) { | |
// A gesture was detected, read and print to serial monitor | |
switch (APDS.readGesture()) { | |
case GESTURE_UP: | |
Serial.println("Detected UP gesture"); | |
break; | |
case GESTURE_DOWN: | |
Serial.println("Detected DOWN gesture"); | |
break; | |
case GESTURE_LEFT: | |
Serial.println("Detected LEFT gesture"); | |
break; | |
case GESTURE_RIGHT: | |
Serial.println("Detected RIGHT gesture"); | |
break; | |
default: | |
// ignore | |
break; | |
} | |
} | |
if (dataFile) { | |
counter += 1; | |
String dataString = ""; | |
dataString += String(counter) + ","; | |
dataString += String(ax) + ","; | |
dataString += String(ay) + ","; | |
dataString += String(az) + ","; | |
dataString += String(gx) + ","; | |
dataString += String(gy) + ","; | |
dataString += String(gz) + ","; | |
dataString += String(latitude) + ","; | |
dataString += String(longitude) + ","; | |
dataString += String(speed); | |
dataFile.println(dataString); | |
} | |
delay(150); // Don't pound too hard | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment