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
} } }