Guide for combining l298n code

I have a project to make a boat with waypoint gps navigation, I have combined the two codes (waypoint and compass heading). but when I enter the code l298n for the motor driver, for some reason the motor doesn't respond. is there something wrong in writing my code?

the picture on the serial monitor is like this, but the motor still doesn't respond. is there a miss in writing the code?

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <QMC5883LCompass.h>

#define RX_PIN 3
#define TX_PIN 2
#define LED_PIN 7
#define GPS_BAUD 9600
#define WAYPOINT_THRESHHOLD 5
#define LEFT_MOTOR  8          // Enable/speed motors Right        
#define RIGHT_MOTOR 9          // Enable/speed motors Left         
#define IN_1  4          // L298N in1 motors Rightx          
#define IN_2  5          // L298N in2 motors Right           
#define IN_3  6           // L298N in3 motors Left           
#define IN_4  7           // L298N in4 motors Left           


TinyGPSPlus gps;
QMC5883LCompass compass;

SoftwareSerial ss(RX_PIN, TX_PIN);


int calcLeftMotor(double relBearing) {
  if (relBearing > 0)
    return 255;
  else
    return 1.425 * relBearing + 255;
}

int calcRightMotor(double relBearing) {
  if (relBearing <= 0)
    return 255;
  else
    return -1.425 * relBearing + 255;
}

//KALAU NGASIH RAW DATA MAGNETIK (if give raw magnetic data)
double calcHeading (int x, int y) {
  return -atan2(x, y) * 180 / 3.141592654;
}

void setup() {
  Serial.begin(9600);
  ss.begin(GPS_BAUD);
  pinMode(LED_PIN, OUTPUT); //LED
  pinMode(LEFT_MOTOR, OUTPUT);
  pinMode(RIGHT_MOTOR, OUTPUT);
  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT);
  pinMode(IN_3, OUTPUT);
  pinMode(IN_4, OUTPUT);


  Serial.println("Waypoint Tracker");

  //init compass
  compass.init();
}

void loop() {
  double targetLat = -7.013606;
  double targetLon = 107.638358;

  while (ss.available() > 0)
    gps.encode(ss.read());

  double dist = 99999999;
  if (gps.location.isValid()) {
    double dist = distance(targetLat, targetLon, gps.location.lat(), gps.location.lng());
    Serial.print("distance to target: ");
    Serial.println(dist);


    if (dist < WAYPOINT_THRESHHOLD)
      goForward();
    else
      stop();

    //    if (dist < WAYPOINT_THRESHHOLD)
    //      digitalWrite(LED_PIN, HIGH);
    //
    //    if (dist > WAYPOINT_THRESHHOLD)
    //      digitalWrite(LED_PIN, LOW);


    double bearing = 0; //target angle
    compass.read(); // reads from compass

    double heading = calcHeading(compass.getX(), compass.getY());
    double relBearing = bearing - heading;

    int leftSpeed = calcLeftMotor(relBearing);
    int rightSpeed = calcRightMotor(relBearing);

    Serial.print(" Heading: "); Serial.println(heading);
    Serial.print("Left: "); Serial.print(leftSpeed); Serial.print(" Right: "); Serial.println(rightSpeed);
    Serial.println(" ");
    delay (1000);

    analogWrite(LEFT_MOTOR, leftSpeed);
    analogWrite(RIGHT_MOTOR, rightSpeed);
  }
  //  else {
  //    Serial.println ("GPS NOT FOUND");
  //    delay (1000);
  //  }


}

double distance(double lat1, double lon1, double lat2, double lon2)
{
  // Conversion factor from degrees to radians (pi/180)
  const double toRadian = 0.01745329251;

  // First coordinate (Radians)
  double lat1_r = lat1 * toRadian;
  double lon1_r = lon1 * toRadian;

  // Second coordinate (Radians)
  double lat2_r = lat2 * toRadian;
  double lon2_r = lon2 * toRadian;

  // Delta coordinates
  double deltaLat_r = (lat2 - lat1) * toRadian;
  double deltaLon_r = (lon2 - lon1) * toRadian;

  // Distance
  double a = sin(deltaLat_r / 2) * sin(deltaLat_r / 2) + cos(lat1_r) * cos(lat2_r) * sin(deltaLon_r / 2) * sin(deltaLon_r / 2);
  double c = 2 * atan2(sqrt(a), sqrt(1 - a));
  double distance = 6371 * c * 1000;

  return distance;

}

void goForward() {
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
}

void goBackward() {
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, HIGH);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
}

void stop() {
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
}

void goRight() {
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
}

void goLeft() {
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
}

#define RX_PIN 3
#define TX_PIN 2
#define LED_PIN 7
#define GPS_BAUD 9600
#define WAYPOINT_THRESHHOLD 5
#define LEFT_MOTOR 8 // Enable/speed motors Right
#define RIGHT_MOTOR 9 // Enable/speed motors Left
#define IN_1 4 // L298N in1 motors Rightx
#define IN_2 5 // L298N in2 motors Right
#define IN_3 6 // L298N in3 motors Left
#define IN_4 7 // L298N in4 motors Left

Pin 7 is used twice.

2 different variables both called dist.
I've not properly checked if you have done the same with other variables but experience of answering questions on here suggests that if someone does that once they have probably done it several times.

Oh sure, thank you.. i forgot to change that pin

still no response sir, "double dist = 9999" is code to declare variable "dist".
And another "double dist" at the bottom (before the l298n function) is the code for the waypoint system mentioned above (at dist=99999)

I don't think you understand.

When you put:

double dist = 9999

You create a variable of type double with the name dist and give it the value of 9999

Shortly after that you have:

double dist = distance(targetLat, targetLon, gps.location.lat(), gps.location.lng());
   

This creates a new variable of type double, also with the name dist and you give it whatever value the function distance returns. So now you have 2 variables, both called dist, each with different values and different scope.

I do not know if you actually want one variable called dist or if you really want 2 different variables. If you want 2 different variable then, to avoid confusion, they should have different names.

I suspect you are cutting and chopping bits of code without any understanding of what it does. If that's the case then please go back to basics and study the examples in the IDE and the tutorials on this web site until you gain enough knowledge to understand the code you are looking at.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.