Guide sketch for 2 DC motor , L9110s, and an ultra sonic sensor

Hello,

I am new to programming and I started this 2 WD avoidance bot project and I look up a few code online and every code seems to have error, the right wheel is not rotating the same direction of the left wheel, if there is any code I can look up to or I can base on would be great, any tip or suggestion would greatly be appreciated.

Thank you!

The code that I messed around with is shown below.

#include <NewPing.h>        //for the Ultrasonic sensor function library.

//L298N motor control pins
const int LeftMotorForward = 11;
const int LeftMotorBackward = 12;
const int RightMotorForward = 5;
const int RightMotorBackward = 6;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  

  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
  
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
  
  if (distance <= 200){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);

    
    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

  void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, HIGH);
  
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(RightMotorBackward, HIGH); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  

 
}
 
void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  

}

Hi, @daymian_001
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

We need to see your code and tell us what it doesn't do.
Also we need a schematic, a hand drawn diagram is fine, just label components and pin names along with including the power supply.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

1 Like

Post the code that you are having trouble with and we can, probably, help you to get it to work. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Post a schematic of the project wiring. Include all components, their part numbers and/or values and power supplies. Post data sheets for the motors, motor drivers and sensors. Hand drawn, photographed and posted is preferred over fritz diagrams.

Photos of the project that show the wiring and component layout can also be valuable.

The more information that you provide, the more and faster help you can get.

I edited the post for the code, for what it doesn't do the bot currently feels like it is just doing just a pattern of sequence but it does dodge some obstacle. Here is a diagram basically I connected the Vcc and Gnd to the Vcc and Gnd of the L9110s since I am using a powerbank to power everything up .

I edited the post for the code, Here is the diagram basically I connected the Vcc ang Gnd to the Vcc ang Gnd of the L9110s since I am using a power bank to power everything up


.

Hi,
You need to write some basic test code for the motor and for the ultrasonic.
A code JUST for the motor controller, get the wheels moving fwd then bwd.
A code JUST to communicate with the ultrasonic, to make sure it works.

For the moment forget about your other code, lets prove that the hardware works.

How are you powering the motors?

Thanks.. Tom.... :grinning: :+1: :coffee: :australia:

How is the power bank connected to the Uno?

By using the included USB cable I think this is USB 2.0.

I already tested the Wheels and they are working fine and just now I looked up a test code for the ultrasonic and the reading seems ok.

I am powering the motors thru the Arduino.

Hi,
Fine, that is good.

Now tell us;

  1. What you want the robot to do when not detecting an obstacle?
  2. What distance you would regard as an obstacle detected?
  3. What the motor response should be to the detection?
  4. What the motors will do when the obstacle is no longer detected?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I don't think the idea is good.
If your only problem is that the wheels are turning in different directions, you can try to exchange the connection of the two wires of one motor or make the appropriate change in the program. It depends on what you prefer.

Hi,

If you are commanding both to got FWD, I assume one motor has been turned around to be on the other side.
So both FWD will mean one goes forward, the other back.

In your motor test code do you command them both to go FWD at the same time?
If so, do they BOTH go in the robots FWD direction?

If it does, just swap the wires to the motor going in the wrong direction.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Yeah It just for temporary, I'll change it once the thing is doing its intended job.

Yaa I tried that already and it is just doing the same thing and I been changing the code yet still the same result.

Yes they both go in the FWD direction.

I tried that as well and it still doing it.

If you just swap one of the motors wires, it should change direction, DO NOT MODIFY THE CODE.

Tom... :grinning: :+1: :coffee: :australia:

now it goes in both direction but after one whole spin the other wheel goes into the opposite direction and after doing one whole it goes back to the correction rotation and its doing it over and over so basically it is not going forward it is just doing circles, seems like the ultrasonic sensor is not doing its job should I put a serial print?

Hi,
Can you please post your motor test code?
I hope that is what you are using and not your full blown ultrasonic code.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:
PS, Can you also answer the questions in post #10, we need to know what you want the motors to do.

Here is the motor test code

const int motorA1 = 6;
const int motorA2 = 7;
const int motorB1 = 8;
const int motorB2 = 9;

void setup() {
  // put your setup code here, to run once:
  pinMode( motorA1, OUTPUT);
  pinMode( motorA2, OUTPUT);

  pinMode( motorB1, OUTPUT);
  pinMode( motorB2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite( motorA1, HIGH);
  digitalWrite( motorA2, LOW);

  digitalWrite( motorB1, HIGH);
  digitalWrite( motorB2, LOW);

  delay(5000);
  //stop
  digitalWrite( motorA1, LOW);
  digitalWrite( motorA2, LOW);

  digitalWrite( motorB1, LOW);
  digitalWrite( motorB2, LOW);

  delay(3000);

  digitalWrite( motorA1, LOW);
  digitalWrite( motorA2, HIGH);

  digitalWrite( motorB1, LOW);
  digitalWrite( motorB2, HIGH);

  delay(5000);
  //stop
  digitalWrite( motorA1, LOW);
  digitalWrite( motorA2, LOW);

  digitalWrite( motorB1, LOW);
  digitalWrite( motorB2, LOW);

  delay(3000);

}

Hi,
And did the motors do all that the code wanted them to?
How are you powering your project including motors?

Tom... :grinning: :+1: :coffee: :australia:

1.I just want it to go forward until it detects something.
2.Probably around 10cm.
3.Go backward and turn right or left then goes forward again.
4.Continues to go forward.