I am currently working on programming the Elegoo Smart Robot Car v3 plus. I have a sketch that allows the car to use line tracking and as it approaches an obstacle it slows down to stop. The sketch so far works well, however I am trying to make some edits to it but am unable to. I am trying to figure out how to increase the car's speed to its maximum as I believe it can go faster than its current pace as well as be able to control the car using the remote (IR Remote Control) it came with if need be. More specifically I want to be able to press the number 1 button to start the sketch program that allows the car to line track and slow down as it approaches an obstacle and for the "ok" button to stop the car. I also want to be able to know the cars reaction time in detecting an obstacle before it slows down and what distance this is from, as well as how to change said distance.
May I please get some help trying to add these features to my current program. I have a deadline for the upcoming Sunday (February 16) if anyone can assist before then.
Thank you!!
#include <Servo.h>
Servo myservo;
//Line Tracking IO define
#define LT_R !digitalRead(10)
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)
// Sonar define
#define Echo A4
#define Trig A5
// Servo define
#define SERVO_PIN 3
// Motor controller define
#define ENB 5
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define ENA 6
#define carSpeed 200
int sonarDistance = 0;
const int distanceLimit = 25;
void forward(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void back(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void left(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void right(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void stop(){
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
}
//Ultrasonic distance measurement function
int getDistance() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
return (int)pulseIn(Echo, HIGH) / 58;
}
void setup(){
myservo.attach(SERVO_PIN);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
stop();
myservo.write(90);
}
void loop() {
sonarDistance = getDistance();
if(sonarDistance <= distanceLimit && !(sonarDistance < 0)) {
// An object is blocking our path
stop();
} else {
if(LT_M){
forward();
}
else if(LT_R) {
right();
while(LT_R);
}
else if(LT_L) {
left();
while(LT_L);
}
}
}
Sketch.ino (1.89 KB)