Joystick motorshield 2 DC motors

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

Joystick motorshield 2 DC motors

Postby Tranber72 » Mon Dec 01, 2014 9:28 pm

Hi,

A DC motor on the X axis: mobilizing the object horizontally (forward and backward)
A DC motor on the Y axis: mobilizing the object vertically (forward and backward)
An Arduino Motor shield V3 map including: Motor Shield V3 Arduino + Arduino Mega 2560 Rev3
A plaque assay, 10 ohm resistors, potentiometers breadboard cables for M / M
A joystick with integrated card deployment: http://www.robotshop.com/en/cytron-mini ... ensor.html

I wish that the object can move with the joystick in the direction X or Y or both simultaneously in a constant speed. When the joystick is on a neutral position, I wish that the motors are off.

I've worked on the code but actually it doesn't work : the motors are moving randomly even if the joystick can influence them a little bit. Can you help me ?

Thank you

You can see on the photo of my first try with only the joystick and the motors

And here is my code wich doesn't work actually

CODE: SELECT_ALL_CODE
// --- Déclaration des constantes des broches E/S numériques ---

int vitesseMotA=3; // Constante pour la broche 3
int sensMotA=12; // Constante pour la broche 12
int freinMotA=9; // Constante pour la broche 9
int vitesseMotB=11; // Constante pour la broche 11
int sensMotB=13; // Constante pour la broche 13

// --- Déclaration des constantes des broches analogiques ---

int axe1Joystick=16; // Constante pour la broche 16
int axe2Joystick=17; // Constante pour la broche 17
int JoyStick_Z=18; // Constante pour la broche 18

// --- Déclaration des variables globales ---

int mesure_brute=0;// Variable pour acquisition résultat brut de conversion analogique numérique
float mesuref=0.0;// Variable pour calcul résultat décimal de conversion analogique numérique

int positionAxe1=0; // Variable pour acquisition résultat brut de conversion analogique numérique axe 1 Joystick
int positionAxe2=0; // Variable pour acquisition résultat brut de conversion analogique numérique axe 2 Joystick

void setup()   { // debut de la fonction setup()

// ------- Initialisation fonctionnalités utilisées ------- 

Serial.begin(115200);

// ------- Broches en sorties numériques ------- 
pinMode (vitesseMotA,OUTPUT); // Broche vitesseMotA configurée en sortie
pinMode (vitesseMotB,OUTPUT); // Broche vitesseMotB configurée en sortie
pinMode (sensMotA,OUTPUT); // Broche vitesseMotA configurée en sortie
pinMode (sensMotB,OUTPUT); // Broche vitesseMotB configurée en sortie
pinMode (freinMotA,OUTPUT); // Broche freinMotA configurée en sortie

// ------- Broches en entrées numériques -------
pinMode (axe1Joystick,INPUT); // Broche axe1joystick configurée en entrée
pinMode (axe2Joystick,INPUT); // Broche axe2joystick configurée en entrée
pinMode (JoyStick_Z,INPUT); // Broche JoyStick_Z configurée en entrée

// ------- Codes d'initialisation utile ------- 
digitalWrite(vitesseMotA,LOW); // a l'arret
digitalWrite(sensMotA,LOW);
digitalWrite(vitesseMotB,LOW); // à l'arret
digitalWrite(sensMotB,LOW);

 
}
void loop(){ // debut de la fonction loop()
//----- lecture position Joytstick
positionAxe1=analogRead(axe1Joystick); // acquisition conversion analogique numérique sur broche analogique axe 1
positionAxe2=analogRead(axe2Joystick); // acquisition conversion analogique numérique sur broche analogique axe 2

if (positionAxe1>700) // Joystick right
{  digitalWrite(vitesseMotA,HIGH);
  digitalWrite(sensMotA,HIGH);
}
else if (positionAxe1<300) // Joystick left
{  digitalWrite(vitesseMotA,HIGH);
  digitalWrite(sensMotA,LOW);
}
else if (positionAxe2>700) // Joystick up
{  digitalWrite(vitesseMotB,HIGH);
  digitalWrite(sensMotB,HIGH);
}
else if (positionAxe2<300) // Joystick down
{   digitalWrite(vitesseMotB,HIGH);
   digitalWrite(sensMotB,LOW);
}

else if (positionAxe2>=300 && positionAxe2<=700)
{digitalWrite(vitesseMotB,LOW);  // turn motor off when between points
}


else if (analogRead(JoyStick_Z)) // Joystick Button STOP
{   digitalWrite(vitesseMotA,LOW);
   digitalWrite(vitesseMotB,LOW);
}
else if (positionAxe1>=300 && positionAxe1<=700)
{digitalWrite(vitesseMotA,LOW);  // turn motor off when between points
} } }




Attachments
montage joystick.jpg
Tranber72
Fledgling
 
Posts: 1
Joined: Mon Dec 01, 2014 9:19 pm

Re: Joystick motorshield 2 DC motors

Postby yonghui » Tue Dec 02, 2014 7:39 am

Hi, quite hard to read the code u posted, but for the joystick, suggested u put some dead zone near the zero of x and y axes, means for example analog values of 0 to 5 on both x and y axes is assumed zero and motor don't move.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm


Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 16 guests

cron