Go Down

Topic: Programming problem!! (Read 73 times) previous topic - next topic

rdx_op

Jun 30, 2020, 12:53 pm Last Edit: Jun 30, 2020, 12:54 pm by rdx_op
i have made an obstacle avoiding robot with arduino nano......but whenever i give power to arduino nano..the 4 dc motors are spinning in backwards...note that i used a HC-SRO4 sonar sensor...here is my code..plz help me where the code is wrong..
Code: [Select]
#include<NewPing.h>
const int Enable1 = 13  ;
const int Enable2 = 9;
const int EchoPin = 8;
const int TriggerPin = 6;

const int LeftMotorICpin1 = 16;
const int LeftMotorICpin2 = 17;
const int RightMotorICpin1 = 7;
const int RightMotorICpin2 = 3;
NewPing sonar(6, 8, 200);
//**********************VOID SETUP***************************//
void setup()
{
  Serial.begin(9600);//Serial communication bandwith rate : 9600...if your computers bandwith is diffrent
  //            |_________
  //                      |
  //you can edit it from here**
  pinMode(LeftMotorICpin1, OUTPUT);  // Digital pin 10 set as output Pin
  pinMode(LeftMotorICpin2, OUTPUT);  // Digital pin 11 set as output Pin
  pinMode(RightMotorICpin1, OUTPUT);  // Digital pin 12 set as output Pin
  pinMode(RightMotorICpin2, OUTPUT);  // Digital pin 13 set as output Pin
  pinMode(Enable1, OUTPUT);  // Digital pin 13 set as output Pin
  pinMode(Enable2, OUTPUT);  // Digital pin 9 set as output Pin
  
  
  
}
//************************VOID lOOP*****************************//
void loop()
{
  int Distance = sonar.ping_cm();
  Serial.println(Distance);

  if (Distance >= 40) // Forward
  {
    digitalWrite(LeftMotorICpin1, HIGH);
    digitalWrite(LeftMotorICpin2, LOW);
    digitalWrite(RightMotorICpin1, HIGH);
    digitalWrite(RightMotorICpin2, LOW);
    analogWrite(Enable1, 255);
    analogWrite(Enable2, 255);
    delay(1);
  }
  if ( Distance < 20) // Backward
  {
    digitalWrite(LeftMotorICpin1, LOW);
    digitalWrite(LeftMotorICpin2, HIGH);
    digitalWrite(RightMotorICpin1, LOW);
    digitalWrite(RightMotorICpin2, HIGH);
    analogWrite(Enable1, 255);
    analogWrite(Enable2, 255);
    delay(1);

  }
  if (Distance < 40 and Distance >= 20) //Right
  {
    digitalWrite(LeftMotorICpin1, HIGH);
    digitalWrite(LeftMotorICpin2, LOW);
    digitalWrite(RightMotorICpin1, LOW);
    digitalWrite(RightMotorICpin2, HIGH);
    analogWrite(Enable1, 255);
    analogWrite(Enable2, 200);
    delay(1);
  }
 
  
}





pcbbc

The code is doing what it is told.  There's nothing "wrong" with it.

Either...
Move forward (distance to sensor >= 40)
Move backwards (distance to sensor < 20)
Turn Right (distance to sensor between 40 and 20)

Presumably (assuming the motors are wired correctly) the sensor is returning < 20.

What do you want to happen instead?
What sensor values should cause that thing to happen?
What sensor values are you actually getting?

rdx_op

nah.....i connected the motors correctly....by the way how ho u get karmas....and title??

pcbbc

#3
Jun 30, 2020, 02:13 pm Last Edit: Jun 30, 2020, 02:16 pm by pcbbc
nah.....i connected the motors correctly....
So your sensor is detecting less than 20.

by the way how ho u get karmas....and title??
One way would be by answering questions asked of you...

What do you want to happen instead?
What sensor values should cause that thing to happen?
What sensor values are you actually getting?

Go Up