Esp32d wroon problem to make my car move all direction


here is my wiring

then my code where i cant get the best result i need the car to go all directions with both motors on i calculated the wheels for more effective turns but dont see where iam wrong using pWM channels
could someone help please


#include <BluetoothSerial.h>
BluetoothSerial btSerial;
#define BT_NAME " vision" // Set bluetooth name

const int pinEnA = 2;
const int pinIn1 = 0;
const int pinIn2 = 4;

const int pinEnB = 18;
const int pinIn3 = 22;
const int pinIn4 = 23;


#define MAX_motorforward 1
#define MAX_motorlateral 3.14
#define PWM_FREQUENCY   1000
#define PWM_RESOLUTION  8
#define PWM_CHANNEL 0



boolean btConnected = false;
char key, previousKey;
float motorforward, motorlateral;
long previousMillis = 0;
int timeout = 1000;


void setup() {

  pinMode(pinEnA,OUTPUT);
  pinMode(pinEnB,OUTPUT);
  pinMode(pinIn1,OUTPUT);
  pinMode(pinIn2,OUTPUT);
  pinMode(pinIn3,OUTPUT);
  pinMode(pinIn4,OUTPUT);

   */
   digitalWrite(pinEnA,HIGH);
  digitalWrite(pinEnB,HIGH);
  //Parar motor Esquerda;
  digitalWrite(pinIn1,HIGH);
  digitalWrite(pinIn2,HIGH);
  //Parar motor Direita;
  digitalWrite(pinIn3,HIGH);
  digitalWrite(pinIn4,HIGH);

 
 
 ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
  
 
  ledcAttachPin(pinEnA,PWM_CHANNEL);
  ledcAttachPin(pinEnB, PWM_CHANNEL);


 Serial.begin(9600);
  btSerial.begin(BT_NAME);
  
  Serial.println("ESP32 Bluetooth Mobile Robot");
  Serial.println();
    
   
  
}


void loop() {
  
 control();
  delay(300);


// Move the DC motor forward at maximum speed
  Serial.println("Moving forward");
  digitalWrite(pinIn1, HIGH);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, HIGH);
  digitalWrite(pinIn4, LOW); 
  delay(2000);
/*
  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, LOW);
  delay(1000);

  
  // Move the DC motor right at maximum speed
  Serial.println("Moving right");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, HIGH);
  digitalWrite(pinIn3, HIGH);
  digitalWrite(pinIn4, LOW); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, LOW);
  delay(1000);

  
   // Move the DC motor left at maximum speed
  Serial.println("Moving left");
  digitalWrite(pinIn1, HIGH);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, HIGH); 
  delay(2000);
  
  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, LOW);
  delay(1000);

   // Move the DC motor backwards at maximum speed
  Serial.println("Moving backwards");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, HIGH);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, HIGH); 
  delay(2000);

  
  
  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, LOW);
  delay(1000);
  delay(2000);

  // Move DC motor forward with increasing speed
  digitalWrite(pinIn1, HIGH);
  digitalWrite(pinIn2, LOW);
   digitalWrite(pinIn3, HIGH);
  digitalWrite(pinIn4, LOW);
*/
  
if (btSerial.available()) {
    previousMillis = millis();
    
    char inChar = (char)btSerial.read();

    if (btConnected == false) {
      btConnected = true;
    //  digitalWrite(BUILTIN_LED, LOW); // Turn on led
      Serial.println("Bluetooth connected.");
    }
 if (inChar >= '0' && inChar <= '9') {
      key = inChar;
      if (key != previousKey) {
        
        switch (key) {
          case '0':
            Serial.println("Robot stop.");
           motorforward =0;
             motorlateral =0;
            
            break;

          case '1':
            Serial.println("Robot move forward.");
             motorforward = 0.1;
             motorlateral = 0;
             
            break;

          case '2':
            Serial.println("Robot move backward.");
          motorforward = -0.1;
             motorlateral = 0;
            break;

          case '3':
            Serial.println("Robot turn left.");
            motorforward = 0.1;
             motorlateral = 0.314;
            break;

          case '4':
            Serial.println("Robot turn right.");
           motorforward = 0.1;
             motorlateral =-0.314;
            break;
        }

         motorforward = constrain(  motorforward, -MAX_motorforward,MAX_motorforward);
        motorlateral = constrain( motorlateral, -MAX_motorlateral, MAX_motorlateral);
   control(  motorforward,motorlateral);
        previousKey = key;

      }
   }
}


if (millis() - previousMillis > timeout &&
      btConnected == true) {

    Serial.println("Bluetooth disconnected.");
  control(0,0);
   // digitalWrite(BUILTIN_LED, HIGH); // Turn off led
    btConnected = false;
      }
}

void control(float forward, float lateral ){


  float  wheelL, wheelR, l = 0.13, r = 0.03;
  
  
  wheelR = ((forward / r) + (lateral * l) / (2 * r)) * 60;
  wheelL = ((forward / r) - (lateral * l) / (2 * r)) * 60;

  Serial.print("Roda Direita:");
  Serial.println(wheelR);
  Serial.print("Roda Esquerda:");
  Serial.println(wheelL);

 wheelR =constrain( wheelR, -255,255);
 wheelL =constrain(  wheelL , -255,255);
  
  if (wheelR >= 0) {

 //float speedR = map(wheelR, 0, 255, 255, 0);
     ledcWrite(pinIn1_PWM_CHANNEL, wheelR);
     ledcWrite(pinIn1_PWM_CHANNEL, 0);
  }
  else {

//float speedR = map(wheelR, -255, 0, 0, 255);
   ledcWrite(pinIn1_PWM_CHANNEL, 0);
  ledcWrite(pinIn1_PWM_CHANNEL, abs(wheelR));
  }

///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

  if (wheelL >= 0) {
    
     
    //float speedL = map(wheelL, 0, 255, 255, 0);
    ledcWrite(pinIn1_PWM_CHANNEL, wheelL);
     ledcWrite(pinIn1_PWM_CHANNEL, 0);
  }
  else {
    
 //float speedL = map(wheelL, -255, 0, 0, 255);
    ledcWrite(pinIn1_PWM_CHANNEL, 0);
     ledcWrite(pinIn1_PWM_CHANNEL, abs(wheelL));
  }
}

You've got the end of a block comment in the middle of setup - does that even compile?

not like that it doesnt but its a small problem it does compile without it

Hi,
your code does not compile because the function asks for arguments (line 218) and they are not provided in the call (line 70).

here its the improvement i got today i have now got 3 direction but only one does not work
this one now compiles
check please

#include <BluetoothSerial.h>
BluetoothSerial btSerial;
#define BT_NAME "  vision" // Set bluetooth name

const int pinEnA = 2;
const int pinIn1 = 0;
const int pinIn2 = 4;

const int pinEnB = 18;
const int pinIn3 = 22;
const int pinIn4 = 23;


#define MAX_motorforward 1
#define MAX_motorlateral 3.14
#define PWM_FREQUENCY   1000
#define PWM_RESOLUTION  8
#define PWM_CHANNEL 0



boolean btConnected = false;
char key, previousKey;
float motorforward, motorlateral;
long previousMillis = 0;
int timeout = 1000;


void setup() {

  pinMode(pinEnA,OUTPUT);
  pinMode(pinEnB,OUTPUT);
  pinMode(pinIn1,OUTPUT);
  pinMode(pinIn2,OUTPUT);
  pinMode(pinIn3,OUTPUT);
  pinMode(pinIn4,OUTPUT);

   
 
 
 ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION);
  
 
  ledcAttachPin(pinEnA,PWM_CHANNEL);
  ledcAttachPin(pinEnB, PWM_CHANNEL);


 Serial.begin(9600);
  btSerial.begin(BT_NAME);
  
  Serial.println("ESP32 Bluetooth Mobile Robot");
  Serial.println();
    
   
  
}


void loop() {
  

  
if (btSerial.available()) {
    previousMillis = millis();
    
    char inChar = (char)btSerial.read();

    if (btConnected == false) {
      btConnected = true;
    //  digitalWrite(BUILTIN_LED, LOW); // Turn on led
      Serial.println("Bluetooth connected.");
    }
 if (inChar >= '0' && inChar <= '9') {
      key = inChar;
      if (key != previousKey) {
        
        switch (key) {
          case '0':
            Serial.println("Robot stop.");
           motorforward =0;
             motorlateral =0;
            
            break;

          case '1':
            Serial.println("Robot move forward.");
             motorforward = 0.15;
             motorlateral = 0;
             forward();
             
            break;

          case '2':
            Serial.println("Robot move backwards.");
          motorforward = -0.15;
             motorlateral = 0;
             backwards();
            break;

          case '3':
            Serial.println("Robot turn left.");
            motorforward = 0;
             motorlateral = 1.5;
             left();
            break;

          case '4':
            Serial.println("Robot turn right.");
           motorforward = 0;
             motorlateral =-1.5;
             right();
            break;
        }

         motorforward = constrain(  motorforward, -MAX_motorforward,MAX_motorforward);
        motorlateral = constrain( motorlateral, -MAX_motorlateral, MAX_motorlateral);
         control(  motorforward,motorlateral);
        previousKey = key;

      }
   }
}

if (millis() - previousMillis > timeout &&
      btConnected == true) {

    Serial.println("Bluetooth disconnected.");
  control(0,0);
   // digitalWrite(BUILTIN_LED, HIGH); // Turn off led
    btConnected = false;
      }

      
}


void control(float forward,float lateral){
 
  float  wheelL, wheelR, l = 0.13, r = 0.03;
  
  
  wheelR = ((forward / r) + (lateral * l) / (2 * r)) * 60;
  wheelL = ((forward / r) - (lateral * l) / (2 * r)) * 60;

  Serial.print("Roda Direita:");
  Serial.println(wheelR);
  Serial.print("Roda Esquerda:");
  Serial.println(wheelL);
   wheelR = constrain(  wheelR, -255,255);
   wheelL = constrain( wheelL, -255, 255);
  
   if (wheelR >= 0) {
    
    ledcWrite(PWM_CHANNEL, wheelR);
  
  }
  else {
    
    ledcWrite(PWM_CHANNEL, abs(wheelR));

    
  }
  if (wheelL >= 0) {
    
    ledcWrite(PWM_CHANNEL, wheelL);
    
  }
  else {
    
    ledcWrite(PWM_CHANNEL, abs(wheelL));
  }

}


//////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////

void forward (){


  Serial.println("Moving forward");
  digitalWrite(pinIn1, HIGH);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, HIGH);
  digitalWrite(pinIn4, LOW); 
  
 }
 
  void right (){
  // Move the DC motor right at maximum speed
  Serial.println("Moving right");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, HIGH);
  digitalWrite(pinIn3, HIGH);
  digitalWrite(pinIn4, LOW); 

    }
  
void left (){
  
   // Move the DC motor left at maximum speed
  Serial.println("Moving left");
  digitalWrite(pinIn1, HIGH);
  digitalWrite(pinIn2, LOW);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, HIGH); 
 
  }
  
void backwards (){
   // Move the DC motor backwards at maximum speed
  Serial.println("Moving backwards");
  digitalWrite(pinIn1, LOW);
  digitalWrite(pinIn2, HIGH);
  digitalWrite(pinIn3, LOW);
  digitalWrite(pinIn4, HIGH); 
  delay(2000);

}


Hi,
now compiled.

Questions:

  1. Is this line printing on the serial monitor? "Serial.println("Bluetooth connected.");

  2. Do any of these lines print something to the serial monitor?
    Serial.println("Robot stop.");
    or
    Serial.println("Robot moves forward.");
    or
    Serial.println("Robot moves backwards.");
    or
    Serial.println("Robot turn left.");
    or
    Serial.println("Robot turn right.");

  3. Do any of these lines print something to the serial monitor?
    Serial.print("Right Wheel:");
    Serial.println(wheelR);
    Serial.print("Left Wheel:");
    Serial.println(wheelL);

  4. Does the car move in any direction?

1-yes it does and connects normally
2-yes forward shows the value as well as backwards the rest its just info
3- left wheel and right wheels just info the wheelL and wheelR print the values calculated using the parameter from the switch case to the control() function the switch case
4-car moves 3 directions but doesnt move backwards also the pwm is negative using the function abs should be positive but on thr plotter monitor its shows negative

the l298 buzzes when i press the buttons to control 1 2 3 4 as i have written on the code where the switch case is at but button 2 backwards doesnt work
im also seeing plotter monitor and the pwm is negative there eventhough i got the abs function there

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