Go Down

Topic: Having issues programming gear motors connected to L298N board (Read 135 times) previous topic - next topic

lxcon

Hi i'm doin simple Line following robot, using KY033 IR sensor, with two motor connected to L298N. I'm pretty sure i get all the hardware right and connected and test by simple programs. So I'm assuming this is the programming issue.

Code: [Select]
const int enA = 2;
const int in1 = 5;
const int in2 = 6;

const int enB = 3;
const int in3 = 8;
const int in4 = 9;

const int leftSensor = 8;
const int rightSensor = 9;

bool flag;
//A -> left
//B -> right

void setup(){
 //Serial.begin(9600);
 pinMode(leftSensor, INPUT);
 pinMode(rightSensor, INPUT);
 
 pinMode(enA, OUTPUT);
 pinMode(enB, OUTPUT);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);



 delay(500);
}

void loop(){
 flag = isDetected(leftSensor);

 if(flag < 1){
   moveForward(100, 100, 20);
 }
 if(flag > 0){
   stopMoving();
 }
 delay(2000);
}

bool isDetected(int pin){
   int sensor = pin;
   sensor = analogRead(pin);
 while(sensor <= 100){
   return true;
 }
 return false;
}

void forward(){
 digitalWrite(in1, LOW);
 digitalWrite(in2, HIGH);
 digitalWrite(in3, LOW);
 digitalWrite(in4, HIGH);
}

void backward(){
 digitalWrite(in1, HIGH);
 digitalWrite(in2, LOW);
 digitalWrite(in3, HIGH);
 digitalWrite(in4, LOW);
}

void moveForward(int leftSpeed, int rightSpeed, int delayTime){
 
 analogWrite(enA, leftSpeed);
 analogWrite(enB, rightSpeed);
 forward();
 delay(delayTime);
 
}

void moveBackward(int leftSpeed, int rightSpeed){
 analogWrite(enA, leftSpeed);
 analogWrite(enB, rightSpeed);
 backward();
}

void stopMoving(){
 analogWrite(enA,0);
 analogWrite(enB,0);
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
 digitalWrite(in3, LOW);
 digitalWrite(in4, LOW);
 
}


So in theory, my code should move the car forward when the sensor is on the line. And will be stopped when it is off the line. But my bot aint moving at all. I'm pretty sure i'm missing some programming knowledge.

Edit: moveForward() function work perfectly once i put it alone in the void loop() without any condition. The conditional statement arent the problem, i tested them with Serial.println() they worked.

UKHeliBob

Quote
moveForward() function work perfectly once i put it alone in the void loop() without any condition.
That would seem to indicate that the condition needed to call the function is never true

Have you tried printing the value of flag before testing whether it is less than 1 ?  As flag is declared as a boolean why not test for true/false instead of < 1 and > 0 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jremington

Please use code tags to post code ("</>" button) (edit your post to correct this).

After you have done that, post the working code.

lxcon

That would seem to indicate that the condition needed to call the function is never true

Have you tried printing the value of flag before testing whether it is less than 1 ?  As flag is declared as a boolean why not test for true/false instead of < 1 and > 0 ?
yes I did try both true and false, or < 1 and > 0. Both worked since i tested them with Serial.println(). I think it's something to do with code flow.

lxcon

Please use code tags to post code ("</>" button) (edit your post to correct this).

After you have done that, post the working code.
Hi, Thank you for pointing the code tag out. I'm not sure what do you exactly mean by the working code, the code i provide technically has no error, could be compiled and upload to the board. The issue is i can't get to work moveForward() function inside any conditional statement. It is working if it's alone in the Loop() tho.

i think i'm having code flow logic issue. Or may be i'm missing something i need to know about digitalWrite() method. Nevertheless I genuinely need your answer.

noweare

Seems like you should should have it moving slightly left or right as it moves forward depending on what sensor is low (or high) it seems like once you are off the line you just stop.  I just went thru your code quickly so I could be missing something.

UKHeliBob

Quote
yes I did try both true and false, or < 1 and > 0. Both worked since i tested them with Serial.println(). I think it's something to do with code flow.
CAn you please confirm that the value of flag is printed is 0 or 1 in the Serial monitor when an object is detected and that the value changes when an object is not detected ?

I note that Serial/begin() is commented out in the code you posted and that there are no Serial.print() statements in it.  Please add them to the code you posted and report on what you see.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lxcon

CAn you please confirm that the value of flag is printed is 0 or 1 in the Serial monitor when an object is detected and that the value changes when an object is not detected ?

I note that Serial/begin() is commented out in the code you posted and that there are no Serial.print() statements in it.  Please add them to the code you posted and report on what you see.
Code: [Select]

const int enA = 2;
const int in1 = 5;
const int in2 = 6;

const int enB = 3;
const int in3 = 8;
const int in4 = 9;

const int leftSensor = 8;
const int rightSensor = 9;

bool flag;
//A -> left
//B -> right

void setup(){
  Serial.begin(9600);
  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);
 
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);



  delay(500);
}

void loop(){
 

  if(isDetected(leftSensor) < 1){
    moveForward(100, 100, 20);
    Serial.println("on the line");
  }
  if(isDetected(leftSensor) > 0){
    stopMoving();
    Serial.println("off from line");
  }
 
 

  //Serial.println(isDetected(leftSensor));
  //moveForward(233, 233, 20);
}

bool isDetected(int pin){
  if(analogRead(pin) <= 100){
    return true;
  }
  else{
    return false;
  }
  return false;
}

void forward(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void backward(){
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void moveForward(int leftSpeed, int rightSpeed, int delayTime){
 
  analogWrite(enA, leftSpeed);
  analogWrite(enB, rightSpeed);
  forward();
  delay(delayTime);
 
}

void moveBackward(int leftSpeed, int rightSpeed){
  analogWrite(enA, leftSpeed);
  analogWrite(enB, rightSpeed);
  backward();
}

void stopMoving(){
  analogWrite(enA,0);
  analogWrite(enB,0);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
 
}



condition statement working fine.
It printed "on the line" while it's on the line.
It printed "off from line" while it's not on the line.
still the motor aint moving, moveForward() function seems broken inside the condition.


UKHeliBob

You are using pins 8 and 9 for 2 different things each.

That can't be right.  What exactly is wired to pins 8 and 9 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lxcon

You are using pins 8 and 9 for 2 different things each.

That can't be right.  What exactly is wired to pins 8 and 9 ?
oh it's actually leftsensor and rightsensor, i'm just running a test code with left sensor before i implement it.

UKHeliBob

Code: [Select]

const int in3 = 8;
const int in4 = 9;

const int leftSensor = 8;
const int rightSensor = 9;

void setup()
{

  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);

  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

So, are pins 8 and 9 inputs or outputs and what are they being used for ?

One of us is confused
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lxcon

Code: [Select]

const int in3 = 8;
const int in4 = 9;

const int leftSensor = 8;
const int rightSensor = 9;

void setup()
{

  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);

  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

So, are pins 8 and 9 inputs or outputs and what are they being used for ?

One of us is confused
pin 8 for the left sensor, and 9 for the right sensor.
I'm currently only testing with left sensor. This is not the issue.

Gates

redefine your inputs and outputs.
Code: [Select]
const int in3 = 8;
const int in4 = 9;

const int leftSensor = 8;
const int rightSensor = 9;

either change in3 and in4 to some other pins or change sensor pins.

UKHeliBob

Quote
This is not the issue.
It very much is an issue.  You cannot use pin 8 for 2 different things at the same time and it cannot be both an input and an output at the same time

What is connected to pin 8 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up