Using a joystick

Good Afternoon (EST),

I am a relative newbie at using Arduino. Currently, I am trying to create a program to control four motors using a parallax joystick and the Adafruit motorshield. However, I cannot get the joystick to communicate with the motors. Can someone please help point me in the right direction?

Much appreciated.

final_draft.ino (1.93 KB)

Hi,
How have you got your joysticks connected to the arduino?
Did you program this sketch part by part, that is, get the joystick part of the sketch working first then get the motor to work, then integrate them?
A link to the specs of the joystick will also help.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks… Tom… :slight_smile:

Hi,

The code is indeed written section by section. The joystick and motor parts each worked individually, but when I tried to link them, it didn’t work out. The code complies perfectly, so I think that the code is just written badly, but I cannot solve it- I suspect that the issue has to do with how I have set up the loops.

Here are the links to the joystick specs:

http://learn.parallax.com/KickStart/27800
https://www.parallax.com/downloads/2-axis-joystick-schematic-pdf (The websites are currently down)

Here is my code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
//------------------------------------------
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
//------------------------------------------

int UD = 0;
int LR = 0;
  
void setup() { 
  
  Serial.begin(9600);
  AFMS.begin();   
  
}

void loop () {
  uint8_t i;

  UD = analogRead(A0);
  LR = analogRead(A1);
  
  Serial.println("UD = ");
  Serial.println(UD, DEC);
  Serial.println(", LR = ");
  Serial.println(LR, DEC); 
  delay(200);
  
 while( UD = 490 && LR == 1023)
  {
  myMotor1->run(FORWARD);
  for (i=0; i=255; i++) {
    myMotor1->setSpeed(i);  
    delay(0);
   }

//-----------------------  
  myMotor2->run(FORWARD);
  for (i=0; i=255; i++) {
    myMotor2->setSpeed(i);  
    delay(0);
    }

//------------------------ 
  myMotor3->run(FORWARD);
  for (i=0; i=255; i++) {
    myMotor3->setSpeed(i);  
    delay(0);
  }
break;
};
//-------------------------

while (UD = 1023 && LR == 1023)
  {
  myMotor1->run(RELEASE);
  for (i=0; i=255; i++) {
    myMotor1->setSpeed(i);  
    delay(0);
  }

//-----------------------  
  myMotor2->run(RELEASE);
  for (i=0; i=255; i++) {
    myMotor2->setSpeed(i);  
    delay(0);
    }

//------------------------ 
    myMotor3->run(RELEASE);
  for (i=0; i=255; i++) {
    myMotor3->setSpeed(i);  
    delay(0);
  }
break;
};

//-------------------------
while (UD < 490 && LR == 1023)
 {
  myMotor1->run(BACKWARD);
  for (i=0; i=255; i++) {
    myMotor1->setSpeed(i);  
    delay(0);
  }

//-----------------------  
  myMotor2->run(BACKWARD);
  for (i=0; i=255; i++) {
    myMotor2->setSpeed(i);  
    delay(0);
    }

//------------------------ 
    myMotor3->run(BACKWARD);
  for (i=0; i=255; i++) {
    myMotor3->setSpeed(i);  
    delay(0);
  }
break;
};
} //void loop bracket
 while( UD = 490 && LR == 1023)

This doesn't look right. You have one equal sign for one number and two equal signs for another number. You must mean == not =. You better get that fixed, in multiple locations.

I can understand that 1023 means holding the stick all the way but how can you be so sure your other stick is at exactly 490?

Hi,
luidr has found a couple of problems.
Can you tell us what you want to happen when you

  1. push joystick UP
  2. push joystick DOWN
  3. push joystick LEFT
  4. push joystick RIGHT.
  5. joystick at rest in middle position.

What are your analog input values for LEFT- MIDDLE - RIGHT
What are your analog input values for UP - MIDDLE - DOWN

Thanks Tom… :slight_smile:

Hi,

Thanks luidr- adding the equal signs made a huge difference, it is now partially responding.

TomGeorge:

  • The analog values for UMD run from 102 to 513 to 1023
  • LMR runs from 0 to 513 to 1023

  • When the joystick is pressed up, all the motors should spin clockwise

  • In the middle, the motors should freeze

  • Pressed back, the motors should run counterclockwise

Everyone:

  • In the interest of saving everyone's time, I am going to just stick with the UMD positions until the issue with responding to the joystick is resolved.

  • As for the joystick, it is a 2-axis joystick that is capable of connecting to the serial monitor, so I check the values for the UMD and LMR positions by checking the values printed to the monitor.

Many thanks,

EAWDlucas

if (abs(UMD_value-513)<=10)&&(LMR_value==1023)) // do something

Will a tolerance range of, say 10, stabilize your control?

If you’re trying to steer the motors, then use the joystick at 45 degrees rotated so when you push it forward both axes are getting large values. Then you use one axis to drive one side motors. Takes care of moving forward/backward/turn/stop.

Many thanks to everyone!

Almost all of the problems are fixed now. The only unresolved issue left now is that when the joystick is pressed, the motors initially start, but then do not exit the loop(s) when the joystick’s direction is changed. As a result, I have to keep manually resetting the joystick. Any ideas on how to fix this?

Here is my code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
//------------------------------------------
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
//------------------------------------------

int UD = 0;
int LR = 0;
  
void setup() { 
  
  Serial.begin(9600);
  AFMS.begin();   
  
}

void loop () {
  uint8_t i;

  UD = analogRead(A0);
  LR = analogRead(A1);
  
  Serial.println("UD = ");
  Serial.println(UD, DEC);
  Serial.println(", LR = ");
  Serial.println(LR, DEC); 
  delay(200);

if ( UD > 513)
 {
  myMotor1->run(FORWARD);
  for (i=0; i=255; i++) {
    myMotor1->setSpeed(i);  
    delay(0);
   }

//-----------------------  
  myMotor2->run(FORWARD);
  for (i=0; i=255; i++) {
    myMotor2->setSpeed(i);  
    delay(0);
    }

//------------------------ 
  myMotor3->run(FORWARD);
  for (i=0; i=255; i++) {
    myMotor3->setSpeed(i);  
    delay(0);
  } 
}
//-------------------------

else if (UD == 513)
  {
  myMotor1->run(RELEASE);
  for (i=0; i=255; i++) {
    myMotor1->setSpeed(i);  
    delay(0);
  }

//-----------------------  
  myMotor2->run(RELEASE);
  for (i=0; i=255; i++) {
    myMotor2->setSpeed(i);  
    delay(0);
    }

//------------------------ 
    myMotor3->run(RELEASE);
  for (i=0; i=255; i++) {
    myMotor3->setSpeed(i);  
    delay(0);
  }
}

//-------------------------
else (UD < 510);{
  myMotor1->run(BACKWARD);
  for (i=0; i=255; i++) {
    myMotor1->setSpeed(i);  
    delay(0);
  }

//-----------------------  
  myMotor2->run(BACKWARD);
  for (i=0; i=255; i++) {
    myMotor2->setSpeed(i);  
    delay(0);
    }

//------------------------ 
    myMotor3->run(BACKWARD);
  for (i=0; i=255; i++) {
    myMotor3->setSpeed(i);  
    delay(0);
  }
};
} //void loop bracket

Thanks again!
EAWDlucas

What does a delay(0) do? You should not try to ramp up a motor speed in a for loop. Use some other ways.

Thank you everyone for helping me out. Everything is now fixed and works consistently.

liudr- your suggestion about the for loop worked fantastically. Thank you so much for your input.

Happy coding,
EAWDlucas