Building MiniRobot Code

Hello everyone,

I’m new here and I have tried to build Mini Robot.
Main Idea:
The robot will go forward with 3 wheels, only 2 will be connected to 2 different DC motors and will check constantly with Ultrasonic sensor that will be connected to Servo motor if there is object in front of me, once it will detect object 15 cm from the sensor, the Servo motor will go 45 degrees to the right, 45 to the left and will check where from it will be easier to pass the object. once it will decide the robot will turn and keep on moving forward.
The code that I wrote will be attached here and I will glad if you will help me to optimize and correct my code.

Thank you.

//All rights reserved to Predato1

#include <Servo.h>
#include <ArduinoRobot.h>
Servo motorR;
Servo motorL;
int const Trig = 10;
int const Echo = 9;
const int servoPin = 2;
const int pingPin = 7;

void setup()
{
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
Robot.begin();

}

void loop()
{
int Rec,V,Dis;
int R=0;
int L=0;
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);
Dis=(Rec/(29.1*2));
Robot.motorsWrite(100,100);
if (Dis <= 15)
{
While (Rec = HIGH && R<=45)
{
myservo.write(R);
R++;
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);
}

myservo.write(0);
delay(15);
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);

While (Rec = HIGH && L>=-45)
{
myservo.write(L);
L=L-1;
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);
}
myservo.write(0);
if(R-L>0)
{
Robot.motorsWrite(-100,100);
delay(50);
}
if(R-L<0)
{
Robot.motorsWrite(100,-100);
delay(50);
}
if(R==L)
{
Robot.motorsWrite(-100,-100);
delay(150);
}

}

}
}

Don't see any servo attach.
Don't see any code tags.

I’m sorry, here is the correct for now :

#include <Servo.h>
#include <ArduinoRobot.h>
Servo motorR;
Servo motorL;
int const Trig = 10;
int const Echo = 9;
const int servoPin = 2;
const int pingPin = 7;

void setup()
{
Serial.begin(9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
motorR.attach(00);
motorL.attach(01);
myservo.attach(11);
Robot.begin();

}

void loop()
{
int Rec,V,Dis;
int R=0;
int L=0;
digitalWrite(Trig,HIGH); //Checking for object
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);
Dis=(Rec/(29.1*2));
Robot.motorWrite(100,100); //Moving forward
if (Dis <= 15) //Detecting object
Robot.motorsWrite(0,0); //Stop
{
While (Rec = HIGH && R<=45) //Testing the area from 0-45 degrees
{
myservo.write(R);
R++;
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);
}

myservo.write(0); //Return to home position
delay(15);
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);

While (Rec = HIGH && L>=-45) //Testing the area from 0-(-45) degrees
{
myservo.write(L);
L=L-1;
digitalWrite(Trig,HIGH);
delay(1);
digitalWrite(Trig,LOW);
Rec=pulseIn(Echo,HIGH);
}
myservo.write(0);
if(R-L>0) //If the object is longer from the right, turn left.
{
Robot.motorsWrite(-100,100);
delay(50);
}
if(R-L<0) //If the object is longer from the left , turn right.
{
Robot.motorsWrite(100,-100);
delay(50);
}
if(R==L) //If equal , turn around
{
Robot.motorsWrite(-100,-100);
delay(150);
}

}

}
}

Still not seeing the code tags
While (Rec = HIGH && L>=-45)  Now can we see the code that actually compiles?

What do you mean by tags?
Sorry for the illiteracy, as I mentioned, I am new here.

There are sticky posts that are at the top of this and every forum here. To help out new forum users like yourself.
Go read up and see if that helps.
readthis.png

That code will not compile. Therefore it has never worked. There is no point in posting code without first at least trying to compile/validate it. You just waste people's time.

Read the error messages you get when you try to validate it and try to fix the problems so that it does compile. Then post the working version. If there are errors you can't understand please post the complete error messages here so that we can help with them.

Steve

...and if you want to compare for equality, use ==, not =

 motorR.attach(00);
   motorL.attach(01);

Not such a great idea, if you're using Serial (which you are).

Don't get into the habit of putting leading zeroes on numeric literals unless you really know what you're doing.