Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
We are hosting Hackster Cafe: Open Hardware Summit. Watch the stream live on Friday!We are hosting Hackster Cafe: Open Hardware Summit. Stream on Friday!
RoboCircuits
Published © MIT

App Controlled Robot

On your fingertips

AdvancedFull instructions provided5 hours56
App Controlled Robot

Things used in this project

Story

Read more

Schematics

Circuit

Code

App Controlled Car code

Arduino
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>


// WiFi Credentials
const char* ssid = "Robot";
const char* password = "87654321";

// Motor Control Pins
#define ENA 25  // Enable/speed motors Right
#define IN_1 2  // L298N in1 motors Right
#define IN_2 15  // L298N in2 motors Right
#define IN_3 16  // L298N in3 motors Left
#define IN_4 17  // L298N in4 motors Left
#define ENB 4   // Enable/speed motors Left
#define SERVO_PIN1 19
#define SERVO_PIN2 21
#define IN_5 34
#define IN_6 35
  
#define Light 23  // Light control pin

// Motor speed and control
String command;
int speedCar = 150;  // Default speed (0 to 255)
int speed_low = 60;

// LEDC channels for PWM
#define PWM_FREQ 1000
#define PWM_RES 8
#define PWM_CHANNEL_A 0
#define PWM_CHANNEL_B 1

WebServer server(80);
Servo myServo1;  // Single Servo Object
Servo myServo2;  // Single Servo Object
void setup() {
  Serial.begin(115200);

  // Set pin modes
  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT);
  pinMode(IN_3, OUTPUT);
  pinMode(IN_4, OUTPUT);
  pinMode(IN_5, OUTPUT);
  pinMode(IN_6, OUTPUT);
  pinMode(Light, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(ENA, OUTPUT);
  // Configure PWM for ESP32

  // Start WiFi Access Point
  WiFi.softAP(ssid, password);
  Serial.print("AP IP address: ");
  Serial.println(WiFi.softAPIP());

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

  // analogWrite(ENA,255);
  // analogWrite(ENB,255);

  // Configure PWM for ESP32
  myServo1.attach(SERVO_PIN1);
  myServo2.attach(SERVO_PIN2);

  // Set initial servo position
  myServo1.write(90);
  myServo2.write(90);
}

void loop() {
  server.handleClient();

  command = server.arg("State");
  if (command == "F") goForward();
  else if (command == "B") goBack();
  else if (command == "L") goLeft();
  else if (command == "R") goRight();
  else if (command == "I") go_up();
  else if (command == "G") go_down();
 
  else if (command == "W") digitalWrite(Light, HIGH);
  else if (command == "S") stopRobot();
  else if (command == "w") digitalWrite(Light, LOW);
  else if (command.toInt() >= 0 && command.toInt() <= 10) {
    speedCar = 0 + (command.toInt() * 25);
    Serial.print("speedCar");
    Serial.println(speedCar);
  } 
  else if (command.toInt() >= 11 && command.toInt() <= 180)
    servoWrite(command.toInt());
}

void HTTP_handleRoot() {
  if (server.hasArg("State")) {
    Serial.println(server.arg("State"));
  }
  server.send(200, "text/html", "");
}



void servoWrite(int pos) {
  myServo2.write(180-pos);
  myServo1.write(pos);
}

// Movement functions
void goForward() {
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
  digitalWrite(IN_5, HIGH);
  digitalWrite(IN_6, LOW);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, speedCar);
  // ledcWrite(PWM_CHANNEL_B, speedCar);
}

void goBack() {
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, HIGH);
  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
    digitalWrite(IN_5, LOW);
  digitalWrite(IN_6, HIGH);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, speedCar);
  // ledcWrite(PWM_CHANNEL_B, speedCar);
}

void goRight() {
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
    digitalWrite(IN_5, HIGH);
  digitalWrite(IN_6, LOW);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, speedCar);
  // ledcWrite(PWM_CHANNEL_B, speedCar);
}

void goLeft() {
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
    digitalWrite(IN_5, HIGH);
  digitalWrite(IN_6, LOW);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, speedCar);
  // ledcWrite(PWM_CHANNEL_B, speedCar);
}

void go_up() {
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
    digitalWrite(IN_5, HIGH);
  digitalWrite(IN_6, LOW);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, speedCar - speed_low);
  // ledcWrite(PWM_CHANNEL_B, speedCar);
}

void go_down() {
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
   digitalWrite(IN_5, HIGH);
  digitalWrite(IN_6, LOW);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, speedCar);
  // ledcWrite(PWM_CHANNEL_B, speedCar - speed_low);
}





void stopRobot() {
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
   digitalWrite(IN_5, LOW);
  digitalWrite(IN_6, LOW);
  analogWrite(ENA, speedCar);
  analogWrite(ENB, speedCar);
  // ledcWrite(PWM_CHANNEL_A, 0);
  // ledcWrite(PWM_CHANNEL_B, 0);
}

Credits

RoboCircuits
39 projects • 222 followers
YouTuber, Explorer, Creator, Programmer, Arduino Lover and Engineer
Contact

Comments

Please log in or sign up to comment.