Pages: 1 2 [3]   Go Down
Author Topic: 2 motors controlled by one joystick  (Read 10298 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its actually very simple. A pot has a range from 0 - 1023, and being that you want to control a motor or servo, you will most likely want full forward, full reverse and stop.
Well forward is equal to anything above 512(mid point of pot), and reverse is anything below 512. Now you also want to stop the motor, and that done when your at 512.
So you will need an IF statement.


psudo:

Code:
int XSpeed = analogRead( XpotPin );

if ( XSpeed > (512 + deadzone) ) //go forward
...

else if ( XSpeed < (512 - deadzone) ) //go reverse
...

else //XSpeed = 512 +- deadzone, stop
deadzone is needed because your pot will almost never perfectly be centered at 512, so some slack is needed
« Last Edit: September 15, 2013, 01:30:48 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Its actually very simple.

Can you post the combined working code? The problem is solvable to a certain extent, but I haven't seen single code (that I understand) that provides forward/reverse and  left/right from a joystick, especially if the stick is pushed in a diagonal direction. I currently do any actual testing with pots and servos as my hardware is currently disconnected and in various places.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll post the full working code later when I get home.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I have here the simplest form of single joystick control. It by default assumes that the motor pins have pull down resistors, but if you don't have pull down resistors, then just uncomment the other lines.

Code:
/*
Simple motor control with square limit joystick

      Y(1023)
    |---------|
    |---------|
X(0)|---512---|X(1023)
    |---------|
    |---------|
       Y(0)
       
 Left Motor Forward/Reverse = LMF/LMR
 Right Motor Forward/Reverse = RMF/RMR
*/

const byte LMF = 3;
const byte LMR = 5;
const byte RMF = 6;
const byte RMR = 9;

const byte Xpot = A0;
const byte Ypot = A1;

volatile unsigned int X = 512, Y = 512; // pot values default 512 for center
unsigned int Xspeed = 0, Yspeed = 0; //motor speed default 0 for full stop
const int Xdeadzone = 5, Ydeadzone = 5; //amount of slack needed for pot values

void setup(){
  pinMode(LMF, OUTPUT);
  pinMode(LMR, OUTPUT);
  pinMode(RMF, OUTPUT);
  pinMode(RMR, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  X = analogRead(Xpot);
  Y = analogRead(Ypot);

  if (X >= (512 + Xdeadzone))//Forward
  {
    Xspeed = (X - 512) / 2; // 0 - 255
    if(Y > (512 + Ydeadzone)) //Left
    {
      Yspeed = (Y - 512) / 2;
      analogWrite(LMF, Xspeed - Yspeed); analogWrite(RMF, Xspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else if (Y < (512 - Ydeadzone)) //Right
    {
      Yspeed = (512 - Y) / 2;
      analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed - Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else
    {
      analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
  }

  else if (X <= (512 - Xdeadzone))//Reverse
  {
    Xspeed = (512 - X) / 2;
    if(Y > (512 + Ydeadzone)) //Left
    {
      Yspeed = (Y - 512) / 2;
      //digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
      analogWrite(LMR, Xspeed - Yspeed); analogWrite(RMR, Xspeed);
    }
    else if (Y < (512 - Ydeadzone)) //Right
    {
      Yspeed = (512 - Y) / 2;
      //digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
      analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed - Yspeed);
    }
    else
    {
      //digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
      analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed);
    }
  }

  else // X is between 512 +- deadzone
  {
    if(Y > (512 + Ydeadzone)) // zero point turn Left
    {
      digitalWrite(LMF, LOW); analogWrite(RMF, Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else if(Y < (512 - Ydeadzone))// zero point turn Right
    {
      analogWrite(LMF, Yspeed); digitalWrite(RMF, LOW);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else
    { // Full stop
      digitalWrite(LMF,LOW);
      digitalWrite(RMF,LOW);
      digitalWrite(LMR,LOW);
      digitalWrite(RMR,LOW);
    }
  }
}

« Last Edit: September 16, 2013, 06:13:47 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Simple motor control with square limit joystick

I assume this means no diagonal stick movement. Correct?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This use every position, which includes diagonal.

I called it square limit because the older joysticks like the Atari joystick have a square range. But the current ones like those used on ps2 controller, xbox, or anything else that uses a joystick, has a round limit. These I think need to use sine and cosine to get there full range of movement. I'll do some tests to confirm if that's true or not, later.

Atari joystick:
http://en.wikipedia.org/wiki/File:Joyopis.svg

The one I posted earlier is the one I am currently using, and it works for diagonal positions too. The only difference is that the first one uses the map function, whereas this one is straight forward using IF ELSE to take the difference of X and Y positions to go a certain direction.

The first one I posted is neater but a little more confusing, compared to this "simpler" code that breaks it all down. Not as nice but not as confusing.
« Last Edit: September 17, 2013, 03:42:32 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so this is the working combined code?




Code:
/*
Simple motor control with square limit joystick

      Y(1023)
    |---------|
    |---------|
X(0)|---512---|X(1023)
    |---------|
    |---------|
       Y(0)
       
 Left Motor Forward/Reverse = LMF/LMR
 Right Motor Forward/Reverse = RMF/RMR
*/

const byte LMF = 3;
const byte LMR = 5;
const byte RMF = 6;
const byte RMR = 9;

const byte Xpot = A0;
const byte Ypot = A1;

volatile unsigned int X = 512, Y = 512; // pot values default 512 for center
unsigned int Xspeed = 0, Yspeed = 0; //motor speed default 0 for full stop
const int Xdeadzone = 5, Ydeadzone = 5; //amount of slack needed for pot values

void setup(){
  pinMode(LMF, OUTPUT);
  pinMode(LMR, OUTPUT);
  pinMode(RMF, OUTPUT);
  pinMode(RMR, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  X = analogRead(Xpot);
  Y = analogRead(Ypot);

  if (X >= (512 + Xdeadzone))//Forward
  {
    Xspeed = (X - 512) / 2; // 0 - 255
    if(Y > (512 + Ydeadzone)) //Left
    {
      Yspeed = (Y - 512) / 2;
      analogWrite(LMF, Xspeed - Yspeed); analogWrite(RMF, Xspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else if (Y < (512 - Ydeadzone)) //Right
    {
      Yspeed = (512 - Y) / 2;
      analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed - Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else
    {
      analogWrite(LMF, Xspeed); analogWrite(RMF, Xspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
  }

  else if (X <= (512 - Xdeadzone))//Reverse
  {
    Xspeed = (512 - X) / 2;
    if(Y > (512 + Ydeadzone)) //Left
    {
      Yspeed = (Y - 512) / 2;
      //digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
      analogWrite(LMR, Xspeed - Yspeed); analogWrite(RMR, Xspeed);
    }
    else if (Y < (512 - Ydeadzone)) //Right
    {
      Yspeed = (512 - Y) / 2;
      //digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
      analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed - Yspeed);
    }
    else
    {
      //digitalWrite(LMF, LOW); digitalWrite(RMF, LOW);
      analogWrite(LMR, Xspeed); analogWrite(RMR, Xspeed);
    }
  }

  else // X is between 512 +- deadzone
  {
    if(Y > (512 + Ydeadzone)) // zero point turn Left
    {
      digitalWrite(LMF, LOW); analogWrite(RMF, Yspeed);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else if(Y < (512 - Ydeadzone))// zero point turn Right
    {
      analogWrite(LMF, Yspeed); digitalWrite(RMF, LOW);
      //digitalWrite(LMR, LOW); digitalWrite(RMR, LOW);
    }
    else
    { // Full stop
      digitalWrite(LMF,LOW);
      digitalWrite(RMF,LOW);
      digitalWrite(LMR,LOW);
      digitalWrite(RMR,LOW);
    }
  }
}

Logged

Pages: 1 2 [3]   Go Up
Jump to: