Arduino Integration Code

C++ code to run your RC Vehicle with the BT Arduino Car Controller App

Setup Instructions

  1. Open the Arduino IDE.
  2. Ensure you have the correct board manager installed (e.g., Seeed XIAO ESP32-S3 or other ESP-based modules that support WiFi functionality).
  3. Copy the code provided below and paste it into a new, blank sketch.
  4. Connect your motor driver (such as the L298N) and motors to the corresponding pins defined at the top of the sketch. Feel free to modify the pin numbers to match your specific wiring.
  5. Upload the code to your micro-controller board.
  6. On your phone, connect to the new Wifi Car access point that your board will broadcast.
  7. Open the App, connect over WiFi mode, and start driving!
car_controller.ino
// --- L298N Motor Driver Pins (Seeed XIAO ESP32-S3) ---
#define ENA 43
#define MOTOR_IN1 44
#define MOTOR_IN2 7
#define ENB 8
#define MOTOR_IN3 9
#define MOTOR_IN4 1

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>

int speedCar = 200;
int speed_Coeff = 3;

const char* ssid = "Wifi Car";
WebServer server(80);

void goAhead() {
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);
  analogWrite(ENA, speedCar);
  digitalWrite(MOTOR_IN3, HIGH);
  digitalWrite(MOTOR_IN4, LOW);
  analogWrite(ENB, speedCar);
}
void goBack() {
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, HIGH);
  analogWrite(ENA, speedCar);
  digitalWrite(MOTOR_IN3, LOW);
  digitalWrite(MOTOR_IN4, HIGH);
  analogWrite(ENB, speedCar);
}
void goRight() {
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, HIGH);
  analogWrite(ENA, speedCar + 50);
  digitalWrite(MOTOR_IN3, HIGH);
  digitalWrite(MOTOR_IN4, LOW);
  analogWrite(ENB, speedCar);
}
void goLeft() {
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);
  analogWrite(ENA, speedCar + 50);
  digitalWrite(MOTOR_IN3, LOW);
  digitalWrite(MOTOR_IN4, HIGH);
  analogWrite(ENB, speedCar);
}
void goAheadRight() {
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, HIGH);
  analogWrite(ENA, speedCar / speed_Coeff);
  digitalWrite(MOTOR_IN3, LOW);
  digitalWrite(MOTOR_IN4, HIGH);
  analogWrite(ENB, speedCar);
}
void goAheadLeft() {
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, HIGH);
  analogWrite(ENA, speedCar);
  digitalWrite(MOTOR_IN3, LOW);
  digitalWrite(MOTOR_IN4, HIGH);
  analogWrite(ENB, speedCar / speed_Coeff);
}
void goBackRight() {
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);
  analogWrite(ENA, speedCar / speed_Coeff);
  digitalWrite(MOTOR_IN3, HIGH);
  digitalWrite(MOTOR_IN4, LOW);
  analogWrite(ENB, speedCar);
}
void goBackLeft() {
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);
  analogWrite(ENA, speedCar);
  digitalWrite(MOTOR_IN3, HIGH);
  digitalWrite(MOTOR_IN4, LOW);
  analogWrite(ENB, speedCar / speed_Coeff);
}
void stopRobot() {
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, LOW);
  analogWrite(ENA, 0);
  digitalWrite(MOTOR_IN3, LOW);
  digitalWrite(MOTOR_IN4, LOW);
  analogWrite(ENB, 0);
}

// ✅ Command is handled IMMEDIATELY when HTTP request arrives
void HTTP_handleRoot(void) {
  if (server.hasArg("State")) {
    String command = server.arg("State");
    Serial.println(command);

    if (command == "F") goAhead();
    else if (command == "B") goBack();
    else if (command == "L") goLeft();
    else if (command == "R") goRight();
    else if (command == "I") goAheadRight();
    else if (command == "G") goAheadLeft();
    else if (command == "J") goBackRight();
    else if (command == "H") goBackLeft();
    else if (command == "S") stopRobot();
    else if (command == "0") speedCar = 100;
    else if (command == "1") speedCar = 117;
    else if (command == "2") speedCar = 134;
    else if (command == "3") speedCar = 151;
    else if (command == "4") speedCar = 168;
    else if (command == "5") speedCar = 185;
    else if (command == "6") speedCar = 204;
    else if (command == "7") speedCar = 220;
    else if (command == "8") speedCar = 225;
    else if (command == "9") speedCar = 300;
  }
  server.send(200, "text/html", "");
}

void setup() {
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(MOTOR_IN1, OUTPUT);
  pinMode(MOTOR_IN2, OUTPUT);
  pinMode(MOTOR_IN3, OUTPUT);
  pinMode(MOTOR_IN4, OUTPUT);

  Serial.begin(115200);

  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  server.on("/", HTTP_handleRoot);
  server.onNotFound(HTTP_handleRoot);
  server.begin();
}

void loop() {
  server.handleClient();  // Only job: listen for incoming requests
}