Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This #include statement was automatically added by the Particle IDE.
- #include "gp20u7_particle/gp20u7_particle.h"
- #include "math.h"
- // Pin numbers for vibrating discs
- int vib1 = D7;
- int vib2 = D6;
- int vib3 = D5;
- GP20U7 gps = GP20U7(Serial1);
- // Set up a Geolocation variable to track your current location
- Geolocation currentLocation;
- // Initialize variable to store coordinates
- String pos;
- // Variables used to calculate the distance between two sets of coordinates
- int comma; // Index of the comma in the received string
- String slat; // Latitude of client as a string
- String slng;
- float flat; // Latitude of the client as a float
- float flng;
- float flatr; // Latitude of client in radians
- float flngr;
- double gpslat; // Latitude of the GPS
- double gpslng;
- float gpslatr; // Latitude of GPS in radians
- float gpslngr;
- float latSin; // Store value to be used in calculation
- float lonSin;
- float distKM; // Distance values
- float distMI;
- float distFT;
- void setup() {
- // Start the GPS module
- gps.begin();
- // Setup virtual com port for output
- Serial.begin(9600);
- // Set pins to output for the vibrating discs
- pinMode(vib1, OUTPUT);
- pinMode(vib2, OUTPUT);
- pinMode(vib3, OUTPUT);
- // Set vibration to low, so the vibrating discs don't vibrate at startup
- digitalWrite(vib1, LOW);
- digitalWrite(vib2, LOW);
- digitalWrite(vib3, LOW);
- // Set variable pos that web page can access to store the GPS position
- Particle.variable("pos", pos);
- // Set up buzz function for web page to access
- Particle.function("buzz", buzz);
- // Set up dist function for web page to access
- Particle.function("dist", dist);
- }
- void loop() {
- // If the GPS is transmitting...
- if(gps.read()){
- currentLocation = gps.getGeolocation();
- gpslat = (currentLocation.latitude); // Store the latitude
- gpslng = (currentLocation.longitude); // Store the longitude
- String lati = String(gpslat); // Convert latitude and longitude to strings
- String lngi = String(gpslng);
- // Combine coordinates into one string that can be accessed by the web page
- pos = lati + "," + lngi;
- // Print to serial to check format
- Serial.print("GPS Position: ");
- Serial.println(pos);
- }
- }
- int dist(String coords) {
- // Calculates the distance between two sets of coordinates
- int comma = coords.indexOf(","); // Finds the index of the comma separating the latitude and longitude in the string
- slat = coords.substring(0,comma); // Latitude as a string
- slng = coords.substring((comma + 1),(coords.length())); // Longitude as a string
- flat = slat.toFloat(); // Convert string to float, in degrees
- flng = slng.toFloat();
- flatr = toRadians(flat); // Convert client coordinates to radians
- flngr = toRadians(flng);
- gpslatr = toRadians(gpslat); // Convert GPS coordinates to radians
- gpslngr = toRadians(gpslng);
- // Begin distance calculations using the Haversine formula
- latSin = sin((flatr - gpslatr)/2);
- lonSin = sin((flngr - gpslngr)/2);
- distKM = 2 * asin(sqrt((latSin*latSin) + cos(flatr) * cos(gpslatr) * (lonSin * lonSin)));
- distKM = distKM * 6371; // Multiply by the average radius of earth
- distMI = distKM * 0.621371192; // Convert from kilometers to miles
- distFT = distMI * 5280; // Convert from miles to feet
- // Print information to the serial monitor to check calculations
- Serial.print("String received: ");
- Serial.println(coords);
- Serial.println(coords.length());
- Serial.print("GPS LAT: ");
- Serial.println(gpslat);
- Serial.print("GPS LON: ");
- Serial.println(gpslng);
- Serial.print("Current LAT: ");
- Serial.println(flat);
- Serial.print("Current LON: ");
- Serial.println(flng);
- Serial.print("DISTANCE IN KM: ");
- Serial.println(distKM);
- Serial.print("DISTANCE IN MI: ");
- Serial.println(distMI);
- Serial.print("DISTANCE IN FT: ");
- Serial.println(distFT);
- Serial.println();
- // Check the distance between the coordinates
- if(distFT>50) { // Sets the range to 50 feet
- buzz("buzz"); // Vibrate the motors if the GPS and the client aren't within the range
- return 1; // Return 1 if the GPS and client are not within the set range
- } else {
- return 0; // Return 0 if the GPS and client are within the set range
- }
- }
- int buzz(String string) {
- // Vibrates the motors when the command 'buzz' is recieved
- if (string == "buzz") {
- digitalWrite(vib1,HIGH); // Turn on the motors
- digitalWrite(vib2,HIGH);
- digitalWrite(vib3,HIGH);
- delay(1000); // Wait one second
- digitalWrite(vib1,LOW); // Turn off the motors
- digitalWrite(vib2,LOW);
- digitalWrite(vib3,LOW);
- Serial.println("buzz"); // Print 'buzz' to make sure the command went through
- return 1;
- }
- else { // If a different string was received...
- Serial.println("NOT BUZZ"); // Can add new strings to perform different functions
- return 0;
- }
- }
- float toRadians(float deg) {
- // Convert degrees to radians
- return (3.141592653589793238462643383279502884197169399375105820974944592307816406286/180 ) * deg; // There is no preset value for pi
- }
Add Comment
Please, Sign In to add comment