Hello all,
i am new to this stuff, but trying to control my servo motor whith the Servo motor SG90, Ultrasonic sensor HC-SR04 and GPS module NEO-6M-001. What i am trying to achieve is having the servo only rotate to 180 when gps coordinates are correct and dis >= 15. Currently gps, distance and time are showing on serial monitor. Servo will rotate to 180 when dis >15 and move servo back to 20 if (dis > 15). if the code is written as seen bellow but is ignoring the coordinates from gps.
{
if (gps.location.rawLat() .deg == int_lat && gps.location.rawLng() .deg == int_lng && dis >= 15)
Serial.print("Reached desired location");
Serial.print("Package delivered to: Latitude=");
Serial.print(gps.location.lat(), 6);
Serial.print(" , Longitude=");
Serial.print(gps.location.lng(), 6);
Serial.print(" at time:");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.print(gps.time.second());
Serial.println(" UTC");
servoMain.write(180); // Turn Servo Right to 180 degrees
if (dis > 15)
servoMain.write(20);
I have been following a tutorial online and below is the full code as well as a link to the tutorial i found. Any help would be extreamly appriciated and please go easy on me as i am very new to this and am trying to accomplish this for my little brothers drone.
/* DIY Drone Instructable that Delivers a Payload
*/
#include <TinyGPS++.h>
#include<Servo.h>
//define TX and RX pins for GPS
static const int RXPin = 0, TXPin = 1; //defining RX and TX pins of Arduino
static const uint32_t GPSBaud = 9600; //defining GPS baud rate
TinyGPSPlus gps;
//destination to drop payload
int des_lat = 33;
int des_lng = 73;
//declaring pins for ultrasonic sensor
const int trigPin = 8; //declare pin number for trig pin
const int echoPin = 9; //declare pin number for echo pin
long duration;
int dis;
Servo servoMain; // defining Servo
void setup() {
Serial.begin(9600); //baud rate
Serial.begin(GPSBaud); //GPS receiving values at Serial0
Serial1.begin(9600); //Ultrasonic receiving values at Serial1
pinMode(trigPin, OUTPUT); // declaring trigPin as output
pinMode(echoPin, INPUT); // declaring echoPin as input
servoMain.attach(14); // attach servo's PWM to Arduino's digital pin 14
servoMain.write (20); //initializing servo
}
void loop() {
while (Serial.available() > 0) {
gps.encode(Serial.read());
if (gps.location.isUpdated()) {
// Displaying latitude (degrees)
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
// Longitude in degrees (double)
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
// displaying the date
Serial.print("Day (DD/MM/YYYY) = ");
Serial.print(gps.date.day());
Serial.print("/");
Serial.print(gps.date.month());
Serial.print("/");
Serial.println(gps.date.year());
// Displaying the time
Serial.print("Time (HH:MM:SS) = ");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.print(gps.time.second());
Serial.println(" UTC");
digitalWrite(trigPin, LOW); //intializing trigPin
delayMicroseconds(2);
// setting trigPin high
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// reading the echoPin
duration = pulseIn(echoPin, HIGH);
// calculating distance
dis = duration * 0.034 / 2;
// displaying distance on Serial Monitor
Serial.print("Ground Clearance: ");
Serial.print(dis);
Serial.println("cm");
//dropping off payload at desired destination
if (gps.location.rawLat() .deg == int_lat && gps.location.rawLng() .deg == int_lng && dis >= 15)
{
Serial.println("Reached desired location");
Serial.print("Package delivered to: Latitude=");
Serial.print(gps.location.lat(), 6);
Serial.print(" , Longitude=");
Serial.print(gps.location.lng(), 6);
Serial.print(" at time:");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.print(gps.time.second());
Serial.println(" UTC");
servoMain.write(20); // Turn Servo Right to 180 degrees
}
else if (dis<=15)
{
servoMain.write(180); // Turn Servo Right to 180 degrees
}
Serial.println("----------------------------------------------------------------------------------------");
delay(500);
}
}
}