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