I have a doubt with my code


const int TrigPin = 11;
const int EchoPin = 10;
const int mot1 = 5;
const int mot2 = 4;
const int mot3 = 3;
const int mot4 = 2;
long period, interval;

void setup()
{
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
pinMode (mot1,OUTPUT);
pinMode (mot2,OUTPUT);
pinMode (mot3,OUTPUT);
pinMode (mot4,OUTPUT);
}

void loop()
{    
digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(TrigPin,HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);  
period = pulseIn(EchoPin,HIGH);
interval = period/58.2;
if(interval<20)
{
digitalWrite(mot1,LOW);
digitalWrite(mot2,HIGH);
digitalWrite(mot3,HIGH);
digitalWrite(mot4,LOW);
}
else
{
digitalWrite(mot1,HIGH);
digitalWrite(mot2,LOW);
digitalWrite(mot3,HIGH);
digitalWrite(mot4,LOW);
}  
delay(100);
}

Is this is a code for Obstecle avoiding robot

Please clarify - what is your problem?
It's impolite to make people look for something they don't know

Welcome to the forum

What doubts do you have ?
What happens when you run the code ?
What should happen when you run the code ?

How are the motors powered ?

Actually my friend coded my obstecle avoiding robot but when I uploaded it , it doesn't work so I have a confusion . IS the above code is a correct CODE FOR obstacle avoiding robot?

There is no way to answer that question without more context. What is your definition (or requirements) for an obstacle avoiding robot? For example it could be:

  1. Robot moves in straight line and stops when obstacle is encountered.
  2. Robot moves in straight line and turns left when obstacle is encountered.
  3. Robot moves in straight line and turns right when obstacle is encountered.

From what distance should it detect the object?

Also, "doesn't work" needs to be defined as well. Is it not moving? Is it not detecting the object? Is it detecting the object and not responding?

You have not told us what Arduino the robot is using, what motors, or what sensor(s). We need a wiring diagram of your actual wiring.

@ToddL1962 , you beat me to it! I'm assuming some sort of ultrasonic sensor with a variable like EchoPin. Probably a single forward facing ultrasonic device.

@david301 can you detail your hardware. As already mentioned, what Arduino are you using? Which motor controller are you using? How have you connected all these hardware components together? What power source are you using?

Remember, we're not in the room next to you so we can't see your setup. You have to describe it to us.

Have you tested the various components of your system individually?

As previously asked

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