The robot has a distance sensor and if there is an obstacle it will change the path.
This is the code:
////////////////////////////////////////////////////////
// Arduino Obstacle Avoiding Robot v2.0 //
// By Aarav Garg - 2021 //
////////////////////////////////////////////////////////
//including the libraries
#include <NewPing.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"
//defining pins and variables
#define TRIG_PIN D3
#define ECHO_PIN D4
#define MAX_DISTANCE 200
#define turn_amount 500
//defining motors,servo,sensor
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
//defining global variables
boolean goesForward=false;
int distance = 100;
#define I2CAdd_1 0x40
#define I2CAdd_2 0x41
int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
int pos8=260;
int pos9=140;
const float delay1 = 0.5;
int delay2 = 500;
/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);
void setup() {
// Initialise both modules
HCPCA9685_1.Init(SERVO_MODE);
HCPCA9685_2.Init(SERVO_MODE);
// Wake both devices up
HCPCA9685_1.Sleep(false);
HCPCA9685_2.Sleep(false);
unsigned int Pos;
/* standing */
HCPCA9685_1.Servo(2, pos4);
HCPCA9685_1.Servo(5, pos4);
HCPCA9685_1.Servo(8, pos4);
HCPCA9685_1.Servo(11, pos4);
HCPCA9685_1.Servo(14, pos4);
HCPCA9685_2.Servo(1, pos4);
HCPCA9685_1.Servo(1, pos4);
HCPCA9685_1.Servo(4, pos4);
HCPCA9685_1.Servo(7, pos4);
HCPCA9685_1.Servo(10, pos4);
HCPCA9685_1.Servo(13, pos4);
HCPCA9685_2.Servo(0, pos4);
HCPCA9685_1.Servo(0, pos2);
HCPCA9685_1.Servo(3, pos3);
HCPCA9685_1.Servo(6, pos2);
HCPCA9685_1.Servo(9, pos2);
HCPCA9685_1.Servo(12, pos3);
HCPCA9685_1.Servo(15, pos2);
delay(delay2);
for (int Pos = pos4; Pos < pos5; Pos++)
{
HCPCA9685_1.Servo(1, Pos);
HCPCA9685_1.Servo(4, Pos);
HCPCA9685_1.Servo(7, Pos);
HCPCA9685_1.Servo(10, Pos);
HCPCA9685_1.Servo(13, Pos);
HCPCA9685_2.Servo(0, Pos);
delay(delay1);
}
delay(delay2);
for (int Pos = pos4; Pos < pos6; Pos++)
{
HCPCA9685_1.Servo(2, Pos);
HCPCA9685_1.Servo(5, Pos);
HCPCA9685_1.Servo(8, Pos);
HCPCA9685_1.Servo(11, Pos);
HCPCA9685_1.Servo(14, Pos);
HCPCA9685_2.Servo(1, Pos);
delay(1);
}
/////
Serial.begin(9600);
HCPCA9685_2.Servo(2, 220);
// myservo.write(90);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
// moveForward();
// goesForward=false;
// moveForward();
int distanceR = 0;
int distanceL = 0;
delay(40);
Serial.println(distance);
if(distance<=15)
{
Serial.println("Object Detected");
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
Serial.print("Distance Right = ");
Serial.println(distanceR);
delay(1000);
distanceL = lookLeft();
Serial.print("Distance Left = ");
Serial.println(distanceL);
delay(1000);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
}
else
{
turnLeft();
moveStop();
}
}
else
{
moveForward();
}
//reseting the variable after the operations
distance = readPing();
}
int lookRight()
{
HCPCA9685_2.Servo(2, 130);
// myservo.write(0);
delay(500);
int distance = readPing();
delay(100);
HCPCA9685_2.Servo(2, 220);
// myservo.write(90);
return distance;
}
int lookLeft()
{
HCPCA9685_2.Servo(2, 310);
// myservo.write(180);
delay(500);
int distance = readPing();
delay(100);
HCPCA9685_2.Servo(2, 220);
// myservo.write(90);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}
void moveStop() {
}
/* Move Forward */
void moveForward() {
for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
HCPCA9685_1.Servo(1, Pos);
delay(delay1);
}
for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
HCPCA9685_1.Servo(1, Pos);
delay(delay1);
}
}
/* moveBackward */
void move_backward() {
for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
HCPCA9685_1.Servo(4, Pos);
delay(delay1);
}
for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
HCPCA9685_1.Servo(4, Pos);
delay(delay1);
}
}
/* turn Right */
void turnRight() {
for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
HCPCA9685_1.Servo(7, Pos);
delay(delay1);
}
for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
HCPCA9685_1.Servo(7, Pos);
delay(delay1);
}
}
/* turn Left */
void turnLeft() {
/////
for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
HCPCA9685_1.Servo(10, Pos);
delay(delay1);
}
for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
HCPCA9685_1.Servo(10, Pos);
delay(delay1);
}
}
/* Stop Move */
void moveStop() {
}
Sorry I don't know how to collect the moves in to a list. I don't know how to send the data in blocks to arduino for path drawing. I defined four directions for the robot in my code and I can control the robot with buttons but how can I control the robot when I draw a path? Can you edit the blocks or code?