Changing Speed and Remote Controlling on Elegoo Smart Robot Car v3 plus

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)

I'm guessing you didn't program that sketch. If you had you would probably know that the speed of the car is controlled by the variable carSpeed (I'd think you might have guessed that). Change that and you change the speed.

Likewise the distance that it reacts to is the variable distanceLimit. The reaction time is 0 but the motors take some time to actually come to a stop.

I'll leave you to sort out the IR control. I'd have trouble getting it working by tomorrow with no information about the controller or the IR receiver. Fortunately it's your deadline not mine so I'll just wish you good luck.

Steve

No I did not program that sketch as you have said. However, I did look through the sketch before and I tried increasing the speed as you suggested, but that did not seem to work.

For the controller, I am trying other ways to include it in the current sketch.

Thank you for the info about the distant limit though and hopefully I can reach the deadline.

You're not going to get it going very much faster...the maximum is 255 and you're already on 200. If you want to check that it's the right variable change it down to say 50 and see if it goes slower. Going faster may need better batteries.

Steve

Ok, I will try that. Once again thanks for helping out.

Hi,
I also experimented a bit with the speed of the car. I reduced it to around 60 when starting the movement. This did not move the car.

Then I introduced a short period with a higher speed 150, just for 100 milliseconds and decreased speed to 60 then. The car moves very slow then. Though, the momentum of the motor is so weak, that even a very light push at the front of the car during the movement with 60 stops the car. It is not able to regain speed again.

I gues the reason is that the motors are just for playing around and no serious velocity variation.
Cheers,
mn6