4WD with XY-joystick and L293D issue

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

  }
}

Does it do what you want, whatever that is ?

Haha, no i cant even verify it. Well thats my problem. I cant find a code which i can read and understand for my setup.

Post the full error message (in code tags) copied from the IDE using the "Copy error message" button

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AFMotorController::AFMotorController()'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AFMotorController::AFMotorController()'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AFMotorController::latch_tx()'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AFMotorController::enable()'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_DCMotor::AF_DCMotor(unsigned char, unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_DCMotor::AF_DCMotor(unsigned char, unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_DCMotor::run(unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_DCMotor::setSpeed(unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_Stepper::AF_Stepper(unsigned int, unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_Stepper::AF_Stepper(unsigned int, unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_Stepper::setSpeed(unsigned int)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_Stepper::release()'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_Stepper::onestep(unsigned char, unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `microstepcurve'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

libraries\Adafruit_Motor_Shield_library\AFMotor.cpp.o (symbol from plugin): In function `AFMotorController::AFMotorController()':

(.text+0x0): multiple definition of `AF_Stepper::step(unsigned int, unsigned char, unsigned char)'

sketch\AFMotor.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\PC\AppData\Local\Temp\cca6MK7U.ltrans0.ltrans.o: In function `main':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:43: undefined reference to `setup'

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Arduino Uno.



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Hi, @naneki

Can you post a link to data/specs of the L293D board that you have please?

Have you tried one of the Example codes first to see if you are connected to the motor controller?
You appear to have no void setup() section in your code.

You need to develop your code in stages, not one big code at once.

Can I suggest you try and control ONE motor, to prove your hardware.
THEN 2 motors, then then 4.
Then write code to test your joysticks with the IDE serial monitor.
When you have that working properly, combine the two.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi,
Please run this code and see if motor4 runs.
Open the IDE Serial Monitor and check that it is set to 9600baud.

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

AF_DCMotor motor(4);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");

  // turn on motor
  motor.setSpeed(200);
 
  motor.run(RELEASE);
}

void loop() {
  uint8_t i;
  
  Serial.println("Motor Forward");
  
  motor.run(FORWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(10);
 }
 
  for (i=255; i!=0; i--) {
    motor.setSpeed(i);  
    delay(10);
 }
  
  Serial.println("Motor Backwards");

  motor.run(BACKWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(10);
 }
 
  for (i=255; i!=0; i--) {
    motor.setSpeed(i);  
    delay(10);
 }
  

  Serial.print("Motor Stop");
  motor.run(RELEASE);
  delay(1000);
}

Tom... :smiley: :+1: :coffee: :australia:

Hi Tom,

Using that code moves Motor 4 forward and backward.

This is the L293D part: Otronic L293D Motor Driver shield voor Arduino - Otronic

Works for all motors. Connections are good. How do i add all the motors in this coding and how do i add the joystick control?

CONNECTION JOYSTICK
VRY to A0 and VRX to A1 (+5V and GND)

Hi,
Great..
Try this code, it will test your joystick, check the IDE monitor for the values as you move the joystick.

#include <AFMotor.h>

//initial motors pin
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
int JoystickXPin = A0;
int JoystickYPin = A1;
int xAxis;
int yAxis;
char command;
void setup()
{
  Serial.begin(9600);
  Serial.println("Joystick Test Code");
}

void loop()
{
  xAxis = analogRead(JoystickXPin); // Read Joysticks X-axis
  xAxis = analogRead(JoystickXPin); // Read Joysticks X-axis twice to give ADC time to measure
  yAxis = analogRead(JoystickYPin); // Read Joysticks Y-axis
  yAxis = analogRead(JoystickYPin); // Read Joysticks Y-axis twice to give ADC time to measure
  Serial.print("xAxis value = \t");
  Serial.print(xAxis);
  Serial.print("\t yAxis value = \t");
  Serial.println(yAxis);
  delay(250);
}

Tom... :smiley: :+1: :coffee::australia:

This is NOT how you set the speed of the motor. That's what the motor1.setSpeed() function is for.

Try this for Backward. Then do the same changes for Forward.

  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // 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
    int motor1Speed = map(yAxis, 470, 0, 0, 255);
    int motor2Speed = map(yAxis, 470, 0, 0, 255);
    int motor3Speed = map(yAxis, 470, 0, 0, 255);
    int motor4Speed = map(yAxis, 470, 0, 0, 255);
    // Set Motor A backward
  motor1.setSpeed(motor1Speed); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(motor2Speed); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(motor3Speed); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(motor4Speed); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
  }

Keeps saying = 0 whatever i do. Tried with joystick on single arduino uno R3 board.

afbeelding

You might find use for this example sketch I wrote a while back:

const byte XPotPin = A0;
const byte YPotPin = A1;

const int PWM_LIMIT = 255;
const int DEAD_ZONE = 10;

void loop()
{
  int speedInput = analogRead(YPotPin); // Forward/Reverse
  int yawInput = analogRead(XPotPin); // Left/Right turn

  // map 'speed' to the range -PWM_LIMIT (backward), +PWM_LIMIT (forward)
  speedInput = map(speedInput, 0, 1023, -PWM_LIMIT, PWM_LIMIT);
  yawInput = map(yawInput, 0, 1023, -PWM_LIMIT, PWM_LIMIT);

  // Put in dead zones
  if (speedInput > -DEAD_ZONE && speedInput < DEAD_ZONE)
    speedInput = 0;
  if (yawInput > -DEAD_ZONE && yawInput < DEAD_ZONE)
    yawInput = 0;

  int leftSpeed = speedInput + (YAW_SENSITIVITY * yawInput);
  int rightSpeed = speedInput - (YAW_SENSITIVITY * yawInput);

  // neither motor can go faster than maximum speed
  leftSpeed = constrain(leftSpeed, -256, 256);
  rightSpeed = constrain(rightSpeed, -256, 256);

  // For positive speeds, motor goes forward at speed
  // For negative speeds, motor goes backward at -speed
  // For zero speeds, turn off motor

Alright, it is working! But.... I have an annoying buzzing sound when not moving. Does anyone has a solution for this?

#include <AFMotor.h>

//initial motors pin
AF_DCMotor motor1(1); 
AF_DCMotor motor2(2); 
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

int motorSpeed1 = 0;
int motorSpeed2 = 0;
int motorSpeed3 = 0;
int motorSpeed4 = 0;

char command;
void setup()
{

  Serial.begin(9600);
  Serial.println("Joystick Test Code");
}

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) {

  int motor1Speed = map(yAxis, 470, 0, 0, 255);
  int motor2Speed = map(yAxis, 470, 0, 0, 255);
  int motor3Speed = map(yAxis, 470, 0, 0, 255);
  int motor4Speed = map(yAxis, 470, 0, 0, 255);
    // Set Motor A backward
  motor1.setSpeed(motor1Speed); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(motor2Speed); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(motor3Speed); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(motor4Speed); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
  }

    else if (yAxis > 550) {
  int motor1Speed = map(yAxis, 550, 1023, 0, 255);
  int motor2Speed = map(yAxis, 550, 1023, 0, 255);
  int motor3Speed = map(yAxis, 550, 1023, 0, 255);
  int motor4Speed = map(yAxis, 550, 1023, 0, 255);
  
    // Set Motor A forward
  motor1.setSpeed(motor1Speed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(motor2Speed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(motor3Speed); //Define maximum velocity
  motor3.run(FORWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(motor4Speed); //Define maximum velocity
  motor4.run(FORWARD); //rotate the motor anti-clockwise
  }
 if (xAxis < 470) {

  int motor1Speed = map(xAxis, 470, 0, 0, 255);
  int motor2Speed = map(xAxis, 470, 0, 0, 255);
  int motor3Speed = map(xAxis, 470, 0, 0, 255);
  int motor4Speed = map(xAxis, 470, 0, 0, 255);
    // Go Right
  motor1.setSpeed(motor1Speed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(0); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(0); //Define maximum velocity
  motor3.run(FORWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(motor4Speed); //Define maximum velocity
  motor4.run(FORWARD); //rotate the motor anti-clockwise
  }

    else if (xAxis > 550) {
  int motor1Speed = map(xAxis, 550, 1023, 0, 255);
  int motor2Speed = map(xAxis, 550, 1023, 0, 255);
  int motor3Speed = map(xAxis, 550, 1023, 0, 255);
  int motor4Speed = map(xAxis, 550, 1023, 0, 255);
  
    // Go left
  motor1.setSpeed(0); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(motor2Speed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(motor3Speed); //Define maximum velocity
  motor3.run(FORWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(0); //Define maximum velocity
  motor4.run(FORWARD); //rotate the motor anti-clockwise
  }
  
}

You don't stop the motors when joystick values are between 470 and 550. Add a second 'else' to turn the motors off (.setSpeed(0)).

Hi,

You tried it on another UNO not the UNO with the controller board connected?

The aim was for you to load the code I supplied onto your existing project, hardware untouched, we are trying to check hardware.

The code worked for me, you need to check ALL your connections.

Tom... :smiley: :+1: :coffee: :australia:

Yeah Tom, i fixed it. Was an soldering error. But fixed now. I am trying to get the buzzing sound away.

What does that code looks like?

Give it a try.

Works great! Thank you very much. For the next step i will be adding a lcd (with i2c).

Could you tell me how i can display speed and pwm on the lcd? Installing the lcd is no issue and displaying hello word etc. Am i supposed to get an motor encoder? Can i do it without code? Any tips?