Code cleanup ++

This commit is contained in:
id101010
2015-12-03 02:39:01 +01:00
parent d0b38011d2
commit 10d7a5ee8e

View File

@@ -1,12 +1,13 @@
/* /*
* Reverse Geocache * Reverse Geocache
* *
* A geocache device which unlocks in a specific area. * A geocache device which unlocks in a specific area.
* *
* Parts used: * Parts used:
* ----------- * -----------
* - Arduino UNO R3 * - Arduino UNO R3
* - Ultimate GPS logger shield * - Ultimate GPS logger shield
* - 16x2 LCD Shield
* - 9V Battery * - 9V Battery
*/ */
#include <math.h> #include <math.h>
@@ -23,11 +24,12 @@ const float deg2rad = 0.01745329251994;
const float rEarth = 6371000.0; // Earths radius in meters const float rEarth = 6371000.0; // Earths radius in meters
float range = 9999; // Distance between src and pos float range = 9999; // Distance between src and pos
String destination = "N46 59.776, E007 27.771"; String destination = "N46 59.776, E007 27.771";
//String destination = "N46 55.090, E007 26.442"; // Gurten
String position = ""; // read from GPS String position = ""; // read from GPS
String tmp = ""; String tmp = "";
SoftwareSerial mySerial(3,2); // Software Serial on 3 (rx) and 2 (tx) SoftwareSerial mySerial(3,2); // Software Serial on 3 (rx) and 2 (tx)
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Init 16x2 LCD LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Init 16x2 LCD
Adafruit_GPS GPS(&mySerial); // Use software serial for gps com Adafruit_GPS GPS(&mySerial); // Use software serial for gps com
float lat1 = 0; float lat1 = 0;
@@ -38,16 +40,19 @@ float lon2 = 0;
// Prototypes // Prototypes
void useInterrupt(boolean); void useInterrupt(boolean);
void print_message(String Line1, String Line2); void print_message(String Line1, String Line2);
void useInterrupt(boolean v);
/*******************************************/ void print_message(String Line1, String Line2);
//String destination = "N46 55.090, W7 26.442"; // Gurten: N 46 55.090 E 7 26.442 float haversine (float lat1, float lon1, float lat2, float lon2);
/*******************************************/ float string2lon(String position);
float string2lat(String position);
String gps2string(void);
String int2fw(int x, int n);
void setup() void setup()
{ {
lcd.begin(16, 2); lcd.begin(16, 2);
lcd.clear(); lcd.clear();
//Serial.begin(9600); //Serial.begin(9600);
//Serial.println("[DEBUGGING INFORMATIONS]:"); //Serial.println("[DEBUGGING INFORMATIONS]:");
@@ -63,13 +68,13 @@ void loop()
{ {
if (GPS.newNMEAreceived()) { // Read nmea string if available if (GPS.newNMEAreceived()) { // Read nmea string if available
if (!GPS.parse(GPS.lastNMEA())) { // Parse string if possible if (!GPS.parse(GPS.lastNMEA())) { // Parse string if possible
return; return;
} }
} }
if (GPS.fix) { // If there was a fix if (GPS.fix) { // If there was a fix
position = gps2string(); // get position string position = gps2string(); // get position string
lat1 = string2lat(position); lat1 = string2lat(position);
lat2 = string2lat(destination); lat2 = string2lat(destination);
lon1 = string2lon(position); lon1 = string2lon(position);
@@ -79,7 +84,7 @@ void loop()
tmp = String(range); tmp = String(range);
tmp += " [m]"; tmp += " [m]";
print_message("I wand up.", tmp); print_message("Me wants up!", tmp);
} else { } else {
print_message("Hello Mischa!", "Take me outside!"); // No fix available print_message("Hello Mischa!", "Take me outside!"); // No fix available
@@ -119,7 +124,7 @@ SIGNAL(TIMER0_COMPA_vect)
{ {
char c = GPS.read(); // Read from serial input char c = GPS.read(); // Read from serial input
if (GPSECHO){ // If GPSECHO is enabled if (GPSECHO) { // If GPSECHO is enabled
if (c) { if (c) {
UDR0 = c; // Send nmea char by char UDR0 = c; // Send nmea char by char
} }
@@ -134,7 +139,7 @@ SIGNAL(TIMER0_COMPA_vect)
**************************************************/ **************************************************/
void useInterrupt(boolean v) void useInterrupt(boolean v)
{ {
if (v) { if (v) {
OCR0A = 0xAF; // Set up timer0 OCR0A = 0xAF; // Set up timer0
TIMSK0 |= _BV(OCIE0A); // Enable timer0 TIMSK0 |= _BV(OCIE0A); // Enable timer0
} else { } else {
@@ -183,13 +188,13 @@ String gps2string(void)
mm = (int) latitude % 100; mm = (int) latitude % 100;
mmm = (int) round(1000 * (latitude - floor(latitude))); mmm = (int) round(1000 * (latitude - floor(latitude)));
gps2lat = lat + int2fw(dd, 2) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3); gps2lat = lat + int2fw(dd, 2) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3);
// lon string // lon string
dd = (int) longitude/100; dd = (int) longitude/100;
mm = (int) longitude % 100; mm = (int) longitude % 100;
mmm = (int) round(1000 * (longitude - floor(longitude))); mmm = (int) round(1000 * (longitude - floor(longitude)));
gps2lon = lon + int2fw(dd, 3) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3); gps2lon = lon + int2fw(dd, 3) + " " + int2fw(mm, 2) + "." + int2fw(mmm, 3);
// Assemble // Assemble
return (gps2lat + ", " + gps2lon); return (gps2lat + ", " + gps2lon);
} }
@@ -205,13 +210,13 @@ float string2lat(String position)
{ {
// returns radians: e.g. String position = "N38 58.892, W076 29.177"; // returns radians: e.g. String position = "N38 58.892, W076 29.177";
float lat = ((position.charAt(1) - '0') * 10.0) + \ float lat = ((position.charAt(1) - '0') * 10.0) + \
((position.charAt(2) - '0') * 1.0) + \ ((position.charAt(2) - '0') * 1.0) + \
((position.charAt(4) - '0') / 6.0) + \ ((position.charAt(4) - '0') / 6.0) + \
((position.charAt(5) - '0') / 60.0) + \ ((position.charAt(5) - '0') / 60.0) + \
((position.charAt(7) - '0') / 600.0) + \ ((position.charAt(7) - '0') / 600.0) + \
((position.charAt(8) - '0') / 6000.0) + \ ((position.charAt(8) - '0') / 6000.0) + \
((position.charAt(9) - '0') / 60000.0); ((position.charAt(9) - '0') / 60000.0);
lat *= deg2rad; lat *= deg2rad;
if (position.charAt(0) == 'S') { if (position.charAt(0) == 'S') {
@@ -228,18 +233,18 @@ float string2lat(String position)
* Descr: returns the float representation of * Descr: returns the float representation of
* the current longitude. * the current longitude.
**************************************************/ **************************************************/
float string2lon (String position) float string2lon(String position)
{ {
// returns radians: e.g. String position = "N38 58.892, W076 29.177"; // returns radians: e.g. String position = "N38 58.892, W076 29.177";
float lon = ((position.charAt(13) - '0') * 100.0) + \ float lon = ((position.charAt(13) - '0') * 100.0) + \
((position.charAt(14) - '0') * 10.0) + \ ((position.charAt(14) - '0') * 10.0) + \
((position.charAt(15) - '0') * 1.0) + \ ((position.charAt(15) - '0') * 1.0) + \
((position.charAt(17) - '0') / 6.0) + \ ((position.charAt(17) - '0') / 6.0) + \
((position.charAt(18) - '0') / 60.0) + \ ((position.charAt(18) - '0') / 60.0) + \
((position.charAt(20) - '0') / 600.0) + \ ((position.charAt(20) - '0') / 600.0) + \
((position.charAt(21) - '0') / 6000.0) + \ ((position.charAt(21) - '0') / 6000.0) + \
((position.charAt(22) - '0') / 60000.0); ((position.charAt(22) - '0') / 60000.0);
lon *= deg2rad; lon *= deg2rad;
if (position.charAt(12) == 'W') { if (position.charAt(12) == 'W') {
@@ -256,14 +261,14 @@ float string2lon (String position)
* Descr: Calculates the distance between * Descr: Calculates the distance between
* two points given by (lat1/2 lon1/2) * two points given by (lat1/2 lon1/2)
**************************************************/ **************************************************/
float haversine (float lat1, float lon1, float lat2, float lon2) float haversine(float lat1, float lon1, float lat2, float lon2)
{ {
// returns the great-circle distance between two points // returns the great-circle distance between two points
float h = sq((sin((lat1 - lat2) / 2.0))) + \ float h = sq((sin((lat1 - lat2) / 2.0))) + \
(cos(lat1) * cos(lat2) * \ (cos(lat1) * cos(lat2) * \
sq((sin((lon1 - lon2) / 2.0)))); sq((sin((lon1 - lon2) / 2.0))));
float d = 2.0 * rEarth * asin (sqrt(h)); float d = 2.0 * rEarth * asin (sqrt(h));
return d; return d;
} }