Hello Everybody,
I'm a beginner at Arduino. i'm making a project that has two DC Motors, one Servo Motor and one joystick.
to control the two DC motors i'm using an L293D motor controller chip to power the L293D im using a BreadBoard Power supply. i'm using the 5V and GND from the arduino uno to power the Servo motor and the joystick. the X-axis of the joystick controls the speed of both DC motors, the Y-axis of the joystick controls the angle of the Servo motor. when Wrote the code for the X-axis of the joystick to control the two DC motors, it was working as i intended. when i wrote the program for the X-axis to control the Servo motors angle it worked as i intended. when i combined those two programs one of the DC motor stopped running when the joystick is in the middle position but it runs in the highest position of the joystick.
is there a way that i can fix the error? and how?
this is my code,
// Servo Motor
#include <Servo.h>
int servo_pin = 11;
int servo_angle;
Servo my_servo;
// JoyStick
int x_pin = A0;
int y_pin = A1;
int x_val;
int y_val;
//DC Motor 1
int DC_speed_pin = 10;
int dir_pin0 = 9;
int dir_pin1 = 8;
int DC_val;
// DC Motor 2
int DC_speed2_pin = 6;
int dir_pin2 = 5;
int dir_pin3 = 4;
int DC_val2;
void setup() {
// put your setup code here, to run once:
//Servo Motor
my_servo.attach(servo_pin);
pinMode(servo_pin, OUTPUT);
//JoyStick
pinMode(x_pin, INPUT);
pinMode(y_pin, INPUT);
//DC Motor 1
pinMode(DC_speed_pin, OUTPUT);
pinMode(dir_pin0, OUTPUT);
pinMode(dir_pin1, OUTPUT);
//DC Motor 2
pinMode(DC_speed2_pin, OUTPUT);
pinMode(dir_pin2, OUTPUT);
pinMode(dir_pin3, OUTPUT);
Serial.begin(2000000);
}
void loop() {
// put your main code here, to run repeatedly:
x_val = analogRead(x_pin);
y_val = analogRead(y_pin);
servo_angle = (180./1023. * (x_val));
DC_val = (255. / 1023. * (y_val));
DC_val2 = (255. / 1023. * (y_val));
Serial.println(DC_val);
my_servo.write(servo_angle);
// DC Motor 1
digitalWrite(dir_pin0, HIGH);
digitalWrite(dir_pin1, LOW);
// DC Motor 2
digitalWrite(dir_pin2, LOW);
digitalWrite(dir_pin3, HIGH);
// DC Motor 1
analogWrite(DC_speed_pin, DC_val);
// DC Motor 2
analogWrite(DC_speed2_pin, DC_val2);
Serial.println(DC_val);
}
in the circuit I attached below, the DC Motor on the left is "DC Motor 1" in the code, the DC Motor in the right is "DC Motor 2" in the code. i used 2 potenciometers in the attached circuit because i could not find a joystick, in real life i'm using a joystick. in the attachment the potenciometer on the left is "X_pin" in the code and the one on the right is "Y_pin". its "DC_motor 1" that stops running when the joystick is in its middle position.