# Arduino robot kit – Autonomous Motion

At this point, we can put the motion and the range sensing capabilities of the robot to good use. For autonomous motion (no remote control), the robot needs to use its range sensor to detect obstacles and avoid them by turning to another direction. The next sketch does that by moving the robot forward while reading the distance ahead. Once an obstacle is detected then the robot stops and scans the space ahead in the 180 degree range and then turn towards the direction of the maximum distance. If there is no suitable path ahead the robot reverses direction.

The most challenging task in this project is to turn the robot precisely towards the direction of the clear path. Since the kit does not have rotation sensor, I used delay() to ensure there is enough time to turn the robot. To measure the rotation speed of the robot, I used a sketch to turn the robot continuously while using a stopwatch to determine the time it takes for the robot to turn 360 degrees. To get accurate results, let the robot turns several times on different surfaces (e.g. carpet and wood floors) on a fully charged batteries.

```void loop() {
motor.onFwd(motor.Motor_LR, 100);
delay(100);
int distm = ultrasonic.Ranging(CM);
// if there is an obstacle less than 20cm ahead, stop
// and find alternate path
if(distm &lt 50) {
motor.stop(motor.Motor_LR);
rangeSweep(mid-90, mid+90, dist);
myservo.write(mid);
int angle = getAngle(dist);

// if there is a clear path to right or left
// then move towards it, othrwise, reverse direction
if(angle &gt (mid+10)) {
motor.turnRight(100);
delay(s * (angle-mid)); // turn
} else if(angle &lt (mid-10)) {
motor.turnLeft(100);
delay(s * (mid-angle)); // turn
} else {
motor.turnRight(100);
delay(s * 180); //
}
}
}```

## 4 thoughts on “Arduino robot kit – Autonomous Motion”

1. Andrew

Thanks for pins its very help. Im just coonect IR Remote and make it work.
But i have other question…. This Sketch dont compile :(( i have bad library or something else

In file included from Robo_WalkFree.ino:7:
x:\Program Files\arduino\libraries\Ultrasonic/Ultrasonic.h:11:22: error: WProgram.h: No such file or directory

Found where is problem:
Change in library some code in header to fix it for all versions

#if defined(ARDUINO) && ARDUINO >= 100
#include “Arduino.h”
#else
#include “WProgram.h”
#endif

in Ultrasonic.h and Ultrasonic.cpp

2. Andrew

Can you make table for sensor shield with ports?

like:

EN = Arduino Pin 18
RS = Arduino Pin 17
RW = Arduino Pin 16
D7 = Arduino Pin 7
D6 = Arduino Pin 6
D5 = Arduino Pin 5
D4 = Arduino Pin 4
D3 = Arduino Pin 11
D2 = Arduino Pin 10
D1 = Arduino Pin 9
D0 = Arduino Pin 8
CSEL1 = Arduino Pin 14