Obstacle Detecting Robot Car

Hi guys

My little brother is making an obstacle detecting robot car for his class, but unfortunately his teacher isn’t helpful at all. I’ve never made something like this before but doing some research I found some code that may work and used Microsoft Copilot to help. We are using the Arduino Elegoo starter kit.

Could you guys help out and let me know if the code might actually work?


#include <NewPing.h>

#include <Servo.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// -------------------------

// LCD Setup

// -------------------------

LiquidCrystal_I2C lcd(0x27, 16, 2);

// -------------------------

// Motor pins

// -------------------------

const int LeftMotorForward  = 2;

const int LeftMotorBackward = 3;

const int RightMotorForward = 4;

const int RightMotorBackward = 5;

// -------------------------

// Ultrasonic sensor pins

// -------------------------

#define trig_pin A1

#define echo_pin A2

#define maximum_distance 200

// -------------------------

// Globals

// -------------------------

boolean goesForward = false;

int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance);

Servo servo_motor;

// -------------------------

// Setup

// -------------------------

void setup() {

// Motor pins

pinMode(RightMotorForward, OUTPUT);

pinMode(LeftMotorForward, OUTPUT);

pinMode(LeftMotorBackward, OUTPUT);

pinMode(RightMotorBackward, OUTPUT);

// Servo

servo_motor.attach(8);

servo_motor.write(90);   // center

delay(800);

// LCD

lcd.init();

lcd.backlight();

lcd.setCursor(0, 0);

lcd.print("Robot Starting");

delay(1000);

distance = readPing();

}

// -------------------------

// Main Loop

// -------------------------

void loop() {

int distanceRight = 0;

int distanceLeft  = 0;

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Dist: ");

lcd.print(distance);

lcd.print(" cm");

if (distance <= 20) {

lcd.setCursor(0, 1);

lcd.print("Obstacle!");

moveStop();

delay(200);

moveBackward();

lcd.setCursor(0, 1);

lcd.print("Backing Up ");

delay(350);

moveStop();

delay(200);

distanceRight = lookRight();

distanceLeft  = lookLeft();

if (distanceRight >= distanceLeft) {

lcd.setCursor(0, 1);

lcd.print("Turning Right");

turnRight();

} else {

lcd.setCursor(0, 1);

lcd.print("Turning Left ");

turnLeft();

}

moveStop();

}

else {

lcd.setCursor(0, 1);

lcd.print("Forward     ");

moveForward();

}

distance = readPing();

delay(50);

}

// -------------------------

// Look Right

// -------------------------

int lookRight() {

servo_motor.write(40);

delay(350);

int dist = readPing();

servo_motor.write(90);

delay(150);

return dist;

}

// -------------------------

// Look Left

// -------------------------

int lookLeft() {

servo_motor.write(140);

delay(350);

int dist = readPing();

servo_motor.write(90);

delay(150);

return dist;

}

// -------------------------

// Read Ultrasonic Sensor

// -------------------------

int readPing() {

int cm = sonar.ping_cm();

if (cm == 0) cm = 250;  // treat no reading as "very far"

return cm;

}

// -------------------------

// Movement Functions

// -------------------------

void moveStop() {

digitalWrite(RightMotorForward, LOW);

digitalWrite(LeftMotorForward, LOW);

digitalWrite(RightMotorBackward, LOW);

digitalWrite(LeftMotorBackward, LOW);

}

void moveForward() {

if (!goesForward) {

goesForward = true;

digitalWrite(LeftMotorForward, HIGH);

digitalWrite(RightMotorForward, HIGH);

digitalWrite(LeftMotorBackward, LOW);

digitalWrite(RightMotorBackward, LOW);

}

}

void moveBackward() {

goesForward = false;

digitalWrite(LeftMotorBackward, HIGH);

digitalWrite(RightMotorBackward, HIGH);

digitalWrite(LeftMotorForward, LOW);

digitalWrite(RightMotorForward, LOW);

}

void turnRight() {

digitalWrite(LeftMotorForward, HIGH);

digitalWrite(RightMotorBackward, HIGH);

delay(450);

moveStop();

}

void turnLeft() {

digitalWrite(LeftMotorBackward, HIGH);

digitalWrite(RightMotorForward, HIGH);

delay(450);

moveStop();

}

I doubt that.

So, you don't trust a knowledgeable teacher but you trust a robot?

Did you test it? What do you observe? Does it move correctly? Does it report correctly?

I think you have your right and left backwards. But you need to test it.

Will you be getting the grade for the class, or will your brother advance to the next class without understanding this class?

The teacher never taught him this stuff and I’m just trying to help him through the course.

Why did you say the teacher is not helpful, when it is you who is pushing your brother to "do" something the teacher has not yet taught?

Maybe just let your brother learn from the teacher at the pace of the curriculum?

Maybe because the assignment is happening in 10 hours and I only heard about it 12 hours ago?

For someone that knows everything you’re pretty goddamn ignorant

Probably not. This does not change you doing your brother'a homework. Maybe just let the kid learn.

I could test the code, validate it, give you an updated version and your brother would have learned nothing. I assume the idea of this project is a learning experience. We all learn even when we FAIL (Forward Action In Life). I wish him the best of luck and he should post his own questions.