Please find code below where Flysky transmitter is used to control dc motors with L298N motor driver and Flysky receiver FS-iA6b connected to DOIT ESP32 DEVKIT VI.
I used the Flysky channel2 right stickY control motor movement forward/reverse using up/down stickY. The Flysky channel4 left stickX control the motor direction left/right. I'm using a 12v power supply to power up my motors hence plenty of amps and voltage to run my motors.
My problem is when I power up the complete project one of the motors is continously ON. It only stops when the Flysky trasnmitter is connected to the receiver. The right stickY of the transmitter is controlling forward/reverse movement of the dc motors no problem. However the transmitter left stickX is working and changing values when I move it left/right using the serial monitor but not moving the motors left/right.
When I code this individually using the left/right stickX on its own its working fine. When I combined it with the right stickY it will not work but showing values on the serial monitor when moving it left/right. This is one of the problem, the other one is when I power up the project one side of the motor is continously ON, it stops when the transmitter is connected. I tried changing the threshold but up/down it still no effect.
Can anyone help to solve my issues with this project.
Chito
/* RC Flysky Left stickX moves motor left/right direction (channel4)
Up/Down Right StickY moves motor forward/backward direction (channel2)
*/
const int channel4Pin = 34; // ChannelPinD for left/right (leftt StickX)
const int channel2Pin = 35; // ChannelPinF for forward/backward (right StickY)
const int motorPin1 = 22; // IN1
const int motorPin2 = 23; // IN2
const int enablePin1 = 25; // EN1
const int motorPin3 = 18; // IN3
const int motorPin4 = 19; // IN4
const int enablePin2 = 26; // EN2
const int speedPin = 100;
void setup() {
Serial.begin(115200);
pinMode(channel4Pin, INPUT);
pinMode(channel2Pin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin2, OUTPUT);
}
void loop() {
int ChannelPinD = pulseIn(channel4Pin, HIGH, 25000); // Channel 4 (left StickX for left/right)
int ChannelPinF = pulseIn(channel2Pin, HIGH, 25000); // Channel 2 (right StickY for forward/backward)
// Print PWM values for debugging
Serial.print("ChannelPinD PWM Value: ");
Serial.println(ChannelPinD);
Serial.print("ChannelPinF PWM Value: ");
Serial.println(ChannelPinF);
// Control motor Left/Right directions
if (ChannelPinD > 1500) { // Right
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin1, 100);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
analogWrite(enablePin2, 0);
Serial.println("Moving Right");
}
else if
(ChannelPinD < 1400) { // Left
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin1, 0);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enablePin2, 100);
Serial.println("Moving Left");
}
else { // Stop
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin1, 0);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
analogWrite(enablePin2, 0);
Serial.println("Stopping");
}
// Control Forward/Backward
if (ChannelPinF > 1700) { // Forward
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin1, speedPin);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
analogWrite(enablePin2, speedPin);
Serial.println("Moving Forward");
}
else if
(ChannelPinF < 1500) { // Backward
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
analogWrite(enablePin1, speedPin);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
analogWrite(enablePin2, speedPin);
Serial.println("Moving Backward");
}
else { // Stop
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin1, 0);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
analogWrite(enablePin2, 0);
Serial.println("Stopping");
}
delay(20); // Further reduced delay for immediate responsiveness
}