Obstacle Avoiding Robot

Hello
I am making an obstacle avoiding robot 2wd using Arduino Uno, its sensor shield, motor drive l298n and servo motor.
so,I have problem writing code so could help out for writing code i have given the wiring diagram below sopls write a code for obstacle avoidance in the range of 10 cm and it moves its servo motor for turning goes backward and turn left/right then moves forward.**

thank you,
IT'S URGENT

Just a word of advice saying IT’S URGENT in your first post won’t get any faster help, because it suggests you’re missing a deadline, and haven’t prepared your work.

There are several sticky posts that can help you create a meaningful thread, maybe start there.

Posting your existing attempt at the code within CODE tags is a good starting point.
Good luck, I hope it comes together for you.

ok so what should i do

help me edit this code

#include <Servo.h>

Servo servo;

const int trigPin = 13;

const int echoPin = 12;

const int servoPin = 11;

const int enAPin = 6;

const int in1Pin = 7;

const int in2Pin = 5;

const int in3Pin = 4;

const int in4Pin = 2;

const int enBPin = 3;

enum Motor { LEFT, RIGHT };

void go( enum Motor m, int speed)

{

digitalWrite (m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );

digitalWrite (m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );

analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );

}

void testMotors ()

{

static int speed[8] = { 128, 255, 128, 0 ,

−128, −255, −128, 0};

go(RIGHT, 0);

for (unsigned char i = 0 ; i < 8 ; i++)

go(LEFT, speed[i ]), delay (200);

for (unsigned char i = 0 ; i < 8 ; i++)

go(RIGHT, speed[i ]), delay (200);

}

unsigned int readDistance ()

{

digitalWrite ( trigPin , HIGH );

delayMicroseconds (10);

digitalWrite ( trigPin , LOW );

unsigned long period = pulseIn ( echoPin, HIGH );

return period * 343 / 2000;

}

#define NUM ANGLES 7

unsigned char sensorAngle[NUM ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };

unsigned int distance [NUM ANGLES];

void readNextDistance ()

{

static unsigned char angleIndex = 0;

static signed char step = 1;

distance [angleIndex ] = readDistance ();

angleIndex += step ;

if (angleIndex == NUM ANGLES − 1) step = −1;

else if (angleIndex == 0) step = 1;

servo . write ( sensorAngle[angleIndex ] );

}

void setup () {

pinMode(trigPin , OUTPUT);

pinMode(echoPin, INPUT);

digitalWrite ( trigPin , LOW);

pinMode(enAPin, OUTPUT);

pinMode(in1Pin, OUTPUT);

pinMode(in2Pin, OUTPUT);

pinMode(in3Pin, OUTPUT);

pinMode(in4Pin, OUTPUT);

pinMode(enBPin, OUTPUT);

servo . attach ( servoPin );

servo . write (90);

go(LEFT, 0);

go(RIGHT, 0);

testMotors ();

servo . write ( sensorAngle[0] );

delay (200);

for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)

readNextDistance (), delay (200);

}

void loop () {

readNextDistance ();

for (unsigned char i = 0 ; i < NUM ANGLES ; i++)

if ( distance [ i ] < 300)

tooClose = 1;

if (tooClose) {

go(LEFT, −180);

go(RIGHT, −80);

}

else {

go(LEFT, 255);

go(RIGHT, 255);

}

delay (50);

}

Read the posting suggestions..l

The code is a good start…
Does it compile ?
what doesn’t it do that you’re expecting?

Is there any other background you can provide ?

error :

C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:33:54: error: stray '\342' in program
 analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );
                                                      ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:33:55: error: stray '\210' in program
 analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );
                                                       ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:33:56: error: stray '\222' in program
 analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );
                                                        ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:1: error: stray '\342' in program
 −128, −255, −128, 0};
 ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:2: error: stray '\210' in program
 −128, −255, −128, 0};
  ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:3: error: stray '\222' in program
 −128, −255, −128, 0};
   ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:9: error: stray '\342' in program
 −128, −255, −128, 0};
         ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:10: error: stray '\210' in program
 −128, −255, −128, 0};
          ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:11: error: stray '\222' in program
 −128, −255, −128, 0};
           ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:17: error: stray '\342' in program
 −128, −255, −128, 0};
                 ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:18: error: stray '\210' in program
 −128, −255, −128, 0};
                  ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:43:19: error: stray '\222' in program
 −128, −255, −128, 0};
                   ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:30: error: stray '\342' in program
 if (angleIndex == NUM ANGLES − 1) step = −1;
                              ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:31: error: stray '\210' in program
 if (angleIndex == NUM ANGLES − 1) step = −1;
                               ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:32: error: stray '\222' in program
 if (angleIndex == NUM ANGLES − 1) step = −1;
                                ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:44: error: stray '\342' in program
 if (angleIndex == NUM ANGLES − 1) step = −1;
                                            ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:45: error: stray '\210' in program
 if (angleIndex == NUM ANGLES − 1) step = −1;
                                             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:46: error: stray '\222' in program
 if (angleIndex == NUM ANGLES − 1) step = −1;
                                              ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:151:10: error: stray '\342' in program
 go(LEFT, −180);
          ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:151:11: error: stray '\210' in program
 go(LEFT, −180);
           ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:151:12: error: stray '\222' in program
 go(LEFT, −180);
            ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:153:11: error: stray '\342' in program
 go(RIGHT, −80);
           ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:153:12: error: stray '\210' in program
 go(RIGHT, −80);
            ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:153:13: error: stray '\222' in program
 go(RIGHT, −80);
             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:13: error: 'ANGLES' was not declared in this scope
 #define NUM ANGLES 7
             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:75:27: note: in expansion of macro 'NUM'
 unsigned char sensorAngle[NUM ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };
                           ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:20: error: expected ']' before numeric constant
 #define NUM ANGLES 7
                    ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:75:27: note: in expansion of macro 'NUM'
 unsigned char sensorAngle[NUM ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };
                           ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:13: error: 'ANGLES' was not declared in this scope
 #define NUM ANGLES 7
             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:77:24: note: in expansion of macro 'NUM'
 unsigned int distance [NUM ANGLES];
                        ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:20: error: expected ']' before numeric constant
 #define NUM ANGLES 7
                    ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:77:24: note: in expansion of macro 'NUM'
 unsigned int distance [NUM ANGLES];
                        ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino: In function 'void readNextDistance()':
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:87:1: error: 'distance' was not declared in this scope
 distance [angleIndex ] = readDistance ();
 ^~~~~~~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:87:1: note: suggested alternative: 'readDistance'
 distance [angleIndex ] = readDistance ();
 ^~~~~~~~
 readDistance
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:13: error: 'ANGLES' was not declared in this scope
 #define NUM ANGLES 7
             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:19: note: in expansion of macro 'NUM'
 if (angleIndex == NUM ANGLES − 1) step = −1;
                   ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:20: error: expected ')' before numeric constant
 #define NUM ANGLES 7
                    ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:91:19: note: in expansion of macro 'NUM'
 if (angleIndex == NUM ANGLES − 1) step = −1;
                   ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:95:17: error: 'sensorAngle' was not declared in this scope
 servo . write ( sensorAngle[angleIndex ] );
                 ^~~~~~~~~~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino: In function 'void setup()':
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:129:17: error: 'sensorAngle' was not declared in this scope
 servo . write ( sensorAngle[0] );
                 ^~~~~~~~~~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:13: error: 'ANGLES' was not declared in this scope
 #define NUM ANGLES 7
             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:133:32: note: in expansion of macro 'NUM'
 for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)
                                ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:20: error: expected ';' before numeric constant
 #define NUM ANGLES 7
                    ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:133:32: note: in expansion of macro 'NUM'
 for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)
                                ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:133:36: error: expected ')' before 'ANGLES'
 for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)
                                    ^~~~~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:133:45: error: 'i' was not declared in this scope
 for (unsigned char i = 0 ; i < NUM ANGLES ; i ++)
                                             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino: In function 'void loop()':
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:13: error: 'ANGLES' was not declared in this scope
 #define NUM ANGLES 7
             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:143:32: note: in expansion of macro 'NUM'
 for (unsigned char i = 0 ; i < NUM ANGLES ; i++)
                                ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:73:20: error: expected ';' before numeric constant
 #define NUM ANGLES 7
                    ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:143:32: note: in expansion of macro 'NUM'
 for (unsigned char i = 0 ; i < NUM ANGLES ; i++)
                                ^~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:143:36: error: expected ')' before 'ANGLES'
 for (unsigned char i = 0 ; i < NUM ANGLES ; i++)
                                    ^~~~~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:143:45: error: 'i' was not declared in this scope
 for (unsigned char i = 0 ; i < NUM ANGLES ; i++)
                                             ^
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:149:5: error: 'tooClose' was not declared in this scope
 if (tooClose) {
     ^~~~~~~~
C:\Users\priya\AppData\Local\Temp\.arduinoIDE-unsaved202505-15864-1g4uzil.j6khf\sketch_jan5d\sketch_jan5d.ino:149:5: note: suggested alternative: 'fclose'
 if (tooClose) {
     ^~~~~~~~
     fclose

exit status 1

Compilation error: stray '\342' in program

Correct them and carry on.

but i am not c++ expert i only know a bit

Your other topic on the same subject, posted a few minutes ago, has been deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

You do not need to be an expert. Work on one small part, then move to the next. What small part do you want help? You will be doing the coding, I will just be answering questions.

i have done the coding but i cant understand why i am getting so much error

Looks like bad cutting and pasting that caused some spurious non-printing characters.

That and the confusing space in the middle of this keyword:

ok i willl fix it thank you !

thank you, new to forum that's why

Here is how to read the error log...

All errors tell the file and line where each error occurred. The following error...

... shows this...

sketch_jan5d.ino:33:54 stray '\342' 

Look in sketch_jan5d.ino on line 33... and count about 54 characters to find this...

  analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −sp eed : speed );

The "dash" you are using is the wrong "dash/minus" sign.

I suspect all "dash" characters that you used are incorrect. There are 8 of them. Replace them with a "minus" character.

ok so i have fix then it will work

Some of the errors are "dash" versus "minus" but due to the copy/paste, some of the "dash" are extraneous (do not belong).

Try just removing all the "dash" (do not change them to "minus")

ok i have to remove dash but not add -

There are more copy/paste problems. Where did you find the original code?

website