5100

A car based on a PCB designed to control a car with a ESP32 and a L293D motor driver.

ESP32 car controlled by phone designed this board to make a car that can be controlled in different way's. The ESP32 is ideal for testing different ways of driving the car. Via Bluetooth, ESP now or with a portable phone. my last attempt was with a phone. This was a success, but it is still in the testing phase. The code is not yet optimized but it works. The car has an OLED display. This is used to display the IP address you need to make contact with the ESP32 so that you can send your commands to the car. 
 Attached  a picture of the PCB and the schematics of the PCB.
Also attached the programm consisting of the main.cpp, dc-motor.h and dc-motor.cpp. 
 
 I wanted to add a ESP32-CAM to the car so that I can make pictures or even video. The problem is that the ESP32-CAM gets a different IP address. If I switch to the cam, I lose control over the car. Still have not solved that problem so if you have an idee for a solution to this concern I would very much like to hear it.
 Huub Zegveld

//main.cpp
#include 
#include 
#include "dc-motor.h"
#include 
#include 
#include 
 
const int trigPin = 14;
const int echoPin = 12;
const int lichtPin = 15;
const int cabinePin = 23;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

//define sound speed in cm/uS
#define SOUND_SPEED 0.034

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

long duration;
float distanceCm;

const char *ssid = "Ziggo....";
const char *password = "xxxxxxx";
int snelheid = 200;

String actie;
String lastAction = "";

WiFiServer server(80); // Service Port

void afstandControle(){
     // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  if (distanceCm < 20){
    actie = "s";
    testDc_motor(actie, snelheid, lastAction);
    delay(500);
    testDc_motor("t", snelheid, lastAction);
    delay(1000);
    testDc_motor("s", snelheid, lastAction);    
    lastAction = "s";                
  }
}

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(lichtPin, OUTPUT);
  pinMode(cabinePin, OUTPUT);  
  digitalWrite(lichtPin, LOW);
  digitalWrite(cabinePin, LOW);  
  // connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  // Start de server
  server.begin();
  Serial.println("Server gestart");

  Serial.print("DC Motor...");
  Serial.print(WiFi.localIP());
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
    Serial.println("SSD1306 allocation failed");
    for (;;); // Don't proceed, loop forever
  }
  display.display();
  delay(2000);
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);// Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
  display.println("Gebruik dit IP adres.");
  display.setCursor(0, 16);
  display.println(WiFi.localIP());
  display.display();
}
  
void loop()
{
  // afstand controle
  afstandControle();
  
  // controle verbinding
  WiFiClient client = server.available();
  if (!client)
  {
    return;
  }

  // Wacht op data
  Serial.println("new client");
  while (!client.available())
  {
    delay(1);
  }

// Lees de eerste regel van de request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  //  client.flush();

  // Vergelijk de request
  if (request.indexOf("/start=vooruit") != -1)
  {
    actie = 'v';
    testDc_motor(actie, snelheid, lastAction);
    lastAction = "v";
  }
  else if (request.indexOf("/start=achteruit") != -1)
  {
    actie = "t";
    testDc_motor(actie, snelheid, lastAction);
    lastAction = "t";    
  }
  else if (request.indexOf("/start=harder") != -1)
  {
    if (snelheid < 245)
    {
      snelheid += 10;
    }
    actie = "h";
    testDc_motor(actie, snelheid, lastAction);
  }
  else if (request.indexOf("/start=zachter") != -1)
  {
    actie = "z";
    if (snelheid > 100)
    {
      snelheid -= 10;
    }
    testDc_motor(actie, snelheid, lastAction);
  }
  else if (request.indexOf("/start=stop") != -1)
  {
    actie = "s";
    testDc_motor(actie, snelheid, lastAction);
    lastAction = "s";
  }
  else if (request.indexOf("/start=linksaf") != -1)
  {
    actie = "l";
    testDc_motor(actie, snelheid, lastAction);
  }
  else if (request.indexOf("/start=rechtsaf") != -1)
  {
    actie = "r";
    testDc_motor(actie, snelheid, lastAction);
  }
  else if (request.indexOf("/start=licht") != -1)
  {
    actie = "l";
    digitalWrite(lichtPin,!digitalRead(lichtPin));
    digitalWrite(cabinePin,!digitalRead(cabinePin));
  }

  // Antwoord retourneren
String html = "HTTP/1.1 200 OK\n"
              "Content-Type: text/html\n\n"
              "\n"
              "\n"
              "\n"
              "\n"
              ".container {display: grid; grid-template-columns: auto auto auto;}\n"
              "body {background-color: green;}\n"
              "h2   {color: white; font-size: 48px; margin-left: 50px}\n"
              "a {margin-top: 20px; font-size: 48px;}\n"
              "\n"
              "\n"
              "\n"
              "

\n" "Aktie is nu: " + String(actie) + "\n" "
\n" "

\n" "vooruit

\n" "linksaf

\n" "rechtsaf

\n" "achteruit

\n" "harder

\n" "zachter

\n" "stop

\n" "licht

\n" "
\n" "

\n" "\n" "\n"; client.println(html); delay(1); } //dc-motor.cpp #include #include "dc-motor.h" void testDc_motor(String actie, int snelheid, String lastAction) { Serial.println(actie); int dutyCycle = snelheid; // Motor A int motor1Pin1 = 25; int motor1Pin2 = 26; int motor2Pin1 = 32; int motor2Pin2 = 33; int enable1Pin = 19; int enable2Pin = 18; // Setting PWM properties const int freq = 30000; const int pwmChannel = 2; const int pwmChannel2 = 3; const int resolution = 8; // sets the motor pins as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(motor2Pin1, OUTPUT); pinMode(motor2Pin2, OUTPUT); // configure LED PWM functionalitites ledcSetup(pwmChannel, freq, resolution); ledcSetup(pwmChannel2, freq, resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(enable1Pin, pwmChannel); ledcAttachPin(enable2Pin, pwmChannel2); // start test------------------------------------------------------------ // Move the DC motor forward at maximum speed if (actie == "v") { ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); Serial.println("Moving Forward"); digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, HIGH); digitalWrite(motor2Pin2, LOW); lastAction= "v"; } // Stop the DC motor if (actie == "s") { Serial.println("Motor stopped"); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); lastAction= "s"; } // Move DC motor backwards at maximum speed if (actie == "t") { Serial.println("Moving Backwards"); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); lastAction= "t"; } // Move DC motor to the right if (actie == "r") { if (lastAction == "t") { Serial.println("Moving to the Right"); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); } else { Serial.println("Moving to the Right"); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, HIGH); digitalWrite(motor2Pin2, LOW); } } // Move DC motor to the left if (actie == "l") { if (lastAction == "t") { Serial.println("Moving to the Left"); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); } else { Serial.println("Moving to the Left"); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); } } // Increasing speed if (actie == "h") { Serial.println(dutyCycle); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); } // Decreasing speed if (actie == "z") { Serial.println(dutyCycle); ledcWrite(pwmChannel, dutyCycle); ledcWrite(pwmChannel2, dutyCycle); } } //dc-motor.h #ifndef DCMOTORH #define DCMOTORH #include void testDc_motor(String actie, int snelheid, String lastAction); #endif