Using esp 32Dwroom to build a car controlled via bluetooth

i have a concern when it cames to the channels to control the motor i need to move forward back left and right but with both motors running
can someone guide me here is my code

#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() {
  

  
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));
  }
}


Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your topic was MOVED to its current forum category as it is more suitable than the original

What made you think that it had anything to do with Covid ?

iam sorry i was seaching for a esp32 and didnt notice that it was the wrong one i got your advice thanks

This is going to be hard.

The ESP32 has a built in MCPWM. Motor Control Pulse Width Modulator (MCPWM) - ESP32 - — ESP-IDF Programming Guide latest documentation that works great at generating motor control signals to be sent to the motor driver.

iam sorry this post doesnt look good a reposted so that it could be better to get it
the esp iam using is 32dwroom and the diagram is already there
but thanks im reading yr post doc now thanks for helping

edit your post and put the code in code tags, please.

done thanks

What's the concern?

i cant make it work i have wheelR and wheelL that should send PWM result in the channel but they dont do it i dont see why also dont know if one channel its ok to run both motors
but i tryied with another code and its ok to do so but here doesnt work

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