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