Hi Everyone,
Most topics I find are with RF joysticks or bluetooth regarding a 4WD arduino car with L293D driver.
I am totally new to programming arduino. I have created a program, which i think is not good at all. Can someone please help me?
BOM:
- Arduino UNO R3
- L293D with pins to attach onto the arduino
- Soldered extra pins to A0-A5, 5V and GND
- 4 DC motors (3-6V)
- 6x 1,5V batteries
- XY-joystick (simple one)
CODE:
#include <AFMotor.h>
//initial motors pin
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
char command;
void loop() {
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor A backward
motor1.setSpeed(255); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(255); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motor1 = map(yAxis, 470, 0, 0, 255);
motor2 = map(yAxis, 470, 0, 0, 255);
motor3 = map(yAxis, 470, 0, 0, 255);
motor4 = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255);//Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(255);//Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motor1 = map(yAxis, 550, 1023, 0, 255);
motor2 = map(yAxis, 550, 1023, 0, 255);
motor3 = map(yAxis, 550, 1023, 0, 255);
motor4 = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motor1.setSpeed(0);
motor2.setSpeed(0);
motor3.setSpeed(0);
motor4.setSpeed(0);
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motor1.setSpeed(- xMapped);
motor2.setSpeed(+ xMapped);
motor3.setSpeed(+ xMapped);
motor4.setSpeed(- xMapped);
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motor1.setSpeed(+ xMapped);
motor2.setSpeed(- xMapped);
motor3.setSpeed(- xMapped);
motor4.setSpeed(+ xMapped);
}
}