RC Car: Function Error

I am trying to build an RC car, but I am still coding its instructions for forward and reverse. Everything seems to work when I call "forward" and "reverse" separately. However, when I run this code, the loop function does not go over everything. Instead, it gets stuck on "forward" and never goes into "stop" or "reverse". What went wrong? (I am using PlatformIO)

Components:

ESP32
L298N Motor Driver Module

#include <Arduino.h>

const int motorAPin1 = 19;
const int motorAPin2 = 18;
const int motorAEnbl = 27;
const int motorBPin1 = 26;
const int motorBPin2 = 25;
const int motorBEnbl = 5;

void forward();
void reverse();
void stop();

void setup() {
  Serial.begin(9600);
  pinMode(motorAPin1, OUTPUT);
  pinMode(motorAPin2, OUTPUT);
  pinMode(motorAEnbl, OUTPUT);
  pinMode(motorBPin1, OUTPUT);
  pinMode(motorBPin2, OUTPUT);
  pinMode(motorBEnbl, OUTPUT);
}

void loop() {
  forward();
  delay(15000);
  stop();
  delay(15000);
  reverse();
  delay(15000);
  stop();
  delay(15000);
}

void forward() {
  digitalWrite(motorAPin1, HIGH);
  digitalWrite(motorAPin2, LOW);
  digitalWrite(motorBPin1, HIGH);
  digitalWrite(motorBPin2, LOW);

  int speed = 100;
  bool reachedMaxSpeed = false;

  while (true) {
    analogWrite(motorAEnbl, speed);
    analogWrite(motorBEnbl, speed);
    Serial.printf("\nDIRECTION: FORWARD | SPEED: %d", speed);

    if (!reachedMaxSpeed) {
      speed++;
      delay(100);
      if (speed > 255) {
        speed = 255;
        reachedMaxSpeed = true;
      }
    }
  }
}

void reverse() {
  digitalWrite(motorAPin1, LOW);
  digitalWrite(motorAPin2, HIGH);
  digitalWrite(motorBPin1, LOW);
  digitalWrite(motorBPin2, HIGH);

  int speed = 100;
  bool reachedMaxSpeed = false;

  while (true) {
    analogWrite(motorAEnbl, speed);
    analogWrite(motorBEnbl, speed);
    Serial.printf("\nDIRECTION: REVERSE | SPEED: %d", speed);

    if (!reachedMaxSpeed) {
      speed++;
      delay(100);
      if (speed > 255) {
        speed = 255;
        reachedMaxSpeed = true;
      }
    }
  }
}

void stop() {
  Serial.printf("\nDIRECTION: STOP | SPEED: 0");
  digitalWrite(motorAPin1, LOW);
  digitalWrite(motorAPin2, LOW);
  digitalWrite(motorBPin1, LOW);
  digitalWrite(motorBPin2, LOW);
  analogWrite(motorAEnbl, 0);
  analogWrite(motorBEnbl, 0);
}

Hi, @khosmoz
Welcome to the forum.

How do you get out of the while... loops if they are true all the time.

 while (true) {
    analogWrite(motorAEnbl, speed);
    analogWrite(motorBEnbl, speed);
    Serial.printf("\nDIRECTION: REVERSE | SPEED: %d", speed);

    if (!reachedMaxSpeed) {
      speed++;
      delay(100);
      if (speed > 255) {
        speed = 255;
        reachedMaxSpeed = true;
      }
    }
 }

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

My bad, I thought that code will stop since I called another function. Thanks for the reply @TomGeorge.

Been tinkering with the code but I cant find a solution. Do you have any suggestions? Thanks.

you need something to tell your car when to stop going forwards/reverse, basically to break; out of your infinite while loop
maybe a pushbutton? or a timer using millis() ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.