Motor shield V3 one joystick two motors

hello,

I am completely new in the world of Arduino and this is my project:

] A DC motor on the X axis: mobilizing the object horizontally/:m]
] A DC motor on the Y axis: mobilizing the object vertically/:m]
] An Arduino Motor shield V3 map including: Motor Shield V3 Arduino + Arduino Mega 2560 Rev3/:m]
] A plaque assay, 10 ohm resistors, potentiometers breadboard cables for M / M/:m]
] A joystick with integrated card deployment: robotshop.com/eu/fr/mini-cap … ytron.html/:m]
] Four limit switches placed at the ends X and Y for the respective motor stop/:m]

I wish that the object can move with the joystick in the direction X or Y or both simultaneously in a constant engine speed but reduced compared to the original with an engine stop when the switch concerned limit is engaged.

Can you help me in terms of connections (I do not understand the theoretical frameworks)? Can you help me with the code for the Arduino board?

Thank you

Welcome to our forums!

The first thing to do would be to connect your joystick to the Arduino and make some basic code to ensure that it works. The joystick is really easy to use: it has two analog output signals that need to be connected to analog input pins. Since the shield is going to be using A0 and A1, I would recommend using pins A2 and A3 for the X and Y of the joystick.

For the code, I would recommend starting with the AnalogInOutSerial example that comes with the Arduino IDE. Change the analog input pin to A2, and test the code with the Serial Monitor to see if everything is working properly.

Then, modify the code to work with both axes of the the joystick.

Once you get this working, let us know and we can see what the next steps are. If you can, please post your code here too!

Cheers,

Thank you so much
I’ll try and post the code

Hi Jeffrey,

I’m just trying to write the code and it doesn’t work. Can you help me ?

[code]const int
PWM_A = 3,
DIR_A = 12,
BRAKE_A = 9,
SNS_A = A0;
int ledPin = 13;
int joyPin1 = 2; // slider variable connecetd to analog pin 2
int joyPin2 = 3; // slider variable connecetd to analog pin 3
int value1 = 0; // variable to read the value from the analog pin 2
int value2 = 0; // variable to read the value from the analog pin 3

void setup() {
// Configure the A output
pinMode(BRAKE_A, OUTPUT); // Brake pin on channel A
pinMode(DIR_A, OUTPUT); // Direction pin on channel A

// Open Serial communication
Serial.begin(9600);
Serial.println(“Motor shield DC motor Test:\n”);

pinMode(ledPin, OUTPUT); // initializes digital pins 0 to 7 as outputs
Serial.begin(9600);
}

int treatValue(int data) {
return (data * 9 / 1024) + 48;
}

void loop() {
// Set the outputs to run the motor forward

digitalWrite(BRAKE_A, LOW); // setting brake LOW disable motor brake
digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward

analogWrite(PWM_A, 255); // Set the speed of the motor, 255 is the maximum value

delay(5000); // hold the motor at full speed for 5 seconds
Serial.print("current consumption at full speed: ");
Serial.println(analogRead(SNS_A));

// Brake the motor

Serial.println(“Start braking\n”);
// raising the brake pin the motor will stop faster than the stop by inertia
digitalWrite(BRAKE_A, HIGH); // raise the brake
delay(5000);

// Set the outputs to run the motor backward

Serial.println(“Backward”);
digitalWrite(BRAKE_A, LOW); // setting againg the brake LOW to disable motor brake
digitalWrite(DIR_A, LOW); // now change the direction to backward setting LOW the DIR_A pin

analogWrite(PWM_A, 255); // Set the speed of the motor

delay(5000);
Serial.print("current consumption backward: ");
Serial.println(analogRead(SNS_A));

// now stop the motor by inertia, the motor will stop slower than with the brake function
analogWrite(PWM_A, 0); // turn off power to the motor

Serial.print("current brake: ");
Serial.println(analogRead(A0));
Serial.println(“End of the motor shield test with DC motors. Thank you!”);

while(1);
// reads the value of the variable resistor
value1 = analogRead(joyPin1);
// this small pause is needed between reading
// analog pins, otherwise we get the same value twice
delay(100);
// reads the value of the variable resistor
value2 = analogRead(joyPin2);

digitalWrite(ledPin, HIGH);
delay(value1);
digitalWrite(ledPin, LOW);
delay(value2);
Serial.print(‘J’);
Serial.print(treatValue(value1));
Serial.println(treatValue(value2));
}
[/code]

THANK YOU

By looking at your code, it seems like it’s setup to make the motor forward for 5 seconds, stop for 5 seconds, backwards for 5 seconds, and then finally stop. Is that what it’s supposed to do? Does the motor do this?

I see that the while(1); line means the program never gets to the joystick part of the code: is that supposed to be like that?

hi
I worked on the code. The previous was zero.
What I want is that when I press my joystick:

  • To the right: A motor rotates clockwise
  • To the left: A motor rotates counterclockwise
  • Top : B motor rotates clockwise
  • Down : B motor rotates counterclockwise

thank you very much

Here is my new code

// --- Déclaration des constantes ---

// --- Inclusion des librairies ---

// --- Déclaration des constantes utiles ---

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

const int vitesseMotA=3; // Constante pour la broche 3
const int sensMotA=12; // Constante pour la broche 12
const int freinMotA=9; // Constante pour la broche 9
const int intensiteMotA=A0; // intensité du moteur 0

const int vitesseMotB=11; // Constante pour la broche 11
const int sensMotB=13; // Constante pour la broche 13
const int freinMotB=8; // Constante pour la broche 8
const int intensiteMotB=A1; // intensité du moteur 1


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


//const int Voie[6]={0,1,2,3,4,5}; //declaration constante de broche analogique


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


// --- Déclaration des objets utiles pour les fonctionnalités utilisées ---

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

const int axe1Joystick=2; // Constante pour la broche analogique 2
const int axe2Joystick=3; // Constante pour la broche analogique 3

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



// ////////////////////////// 2. FONCTION SETUP = Code d'initialisation ////////////////////////// 
// La fonction setup() est exécutée en premier et 1 seule fois, au démarrage du programme

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

// --- ici instructions à exécuter 1 seule fois au démarrage du programme --- 

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

Serial.begin(115200); 

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

// ------- Broches en entrées numériques ------- 
 pinMode (bpJoystick,INPUT); // Broche bpJoystick configurée en entrée

// ------- Activation si besoin du rappel au + (pullup) des broches en entrées numériques -------  

// Les BP du shield Joystick + BPx2 dispose d'un rappel au plus sur le shield


// ------- Activation si besoin du rappel au + (pullup) des broches en entrées numériques -------  

// ------- Initialisation des variables utilisées -------  

// ------- Codes d'initialisation utile -------  
digitalWrite(vitesseMotA,LOW); // a l'arret
digitalWrite(sensMotA,LOW); 
digitalWrite(freinMotA,LOW); // frein off 

digitalWrite(vitesseMotB,LOW); // à l'arret 
digitalWrite(sensMotB,LOW);  
digitalWrite(freinMotB,LOW); // frein off

} // fin de la fonction setup()
// ********************************************************************************

////////////////////////////////// 3. FONCTION LOOP = Boucle sans fin = coeur du programme //////////////////
// la fonction loop() s'exécute sans fin en boucle aussi longtemps que l'Arduino est sous tension

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) {
  digitalWrite(vitesseMotA,HIGH);
digitalWrite(sensMotA,HIGH);
}

if (positionAxe1<300) {
  digitalWrite(vitesseMotA,HIGH);
digitalWrite(sensMotA,LOW);
}

if (positionAxe2>700) { 
  digitalWrite(vitesseMotB,HIGH);
digitalWrite(sensMotB,HIGH);
}

if (positionAxe2<300) {
   digitalWrite(vitesseMotA,HIGH);
digitalWrite(sensMotA,LOW);
} 

while(1); // stop loop
} 
// fin de la fonction loop() - le programme recommence au début de la fonction loop sans fin
// ********************************************************************************

From the code, it seems your read your joysticks once, set the motor speeds, and stop the program. What happens if you hold the joystick Axe1 in the up position when you power up the Arduino? Based on what I can see, it should turn MotA on in either the forward or reverse direction. Does this happen?

Note that since your program stops at the end of the main loop() function, the code will not change the speed of the motor once it is turned on the first time. Also, there doesn’t seem to be any code to turn off the motor if the joystick goes back to neutral. I would recommend changing your code a bit to look like this:

if (positionAxe1>700) {
  digitalWrite(vitesseMotA,HIGH);
  digitalWrite(sensMotA,HIGH);
} else if (positionAxe1<300) { // Added 'else' to the if condition
  digitalWrite(vitesseMotA,HIGH);
  digitalWrite(sensMotA,LOW);
} else { // Added a final 'else' condition to turn the motor off
  digitalWrite(vitesseMotA,LOW);
}

The same needs to be done for MotB.

Also, I noticed in your setup() function that the pinMode(freinMotB, OUTPUT) is missing, so that’s probably why MotB isn’t working.

Hope this helps,

Hi Jeffrey,

I’ve modified some things in the code but the joystick doesn’t influence anything

Here is my modifications and thank you for your help !

PS : A question => can the motors work with only the USB connection on the Arduino ? Do you know the number of the pin for A and B motors’s connections on the motorshield ?

[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 vitesseMotB=11; // Constante pour la broche 11
int sensMotB=13; // Constante pour la broche 13

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

int axe1Joystick = A2; // Constante pour la broche analogique 2
int axe2Joystick = A3; // Constante pour la broche analogique 3
int JoyStick_Z = A4; // Constante pour la broche analogique 4

// — 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

// ------- 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()
while(1) {
//----- 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

int x,y,z;
if (positionAxe1>0) {
digitalWrite(vitesseMotA,HIGH);
digitalWrite(sensMotA,HIGH);
}
else if (positionAxe1<0) {
digitalWrite(vitesseMotA,HIGH);
digitalWrite(sensMotA,LOW);
}
else if (positionAxe2>0) {
digitalWrite(vitesseMotB,HIGH);
digitalWrite(sensMotB,HIGH);
}
else if (positionAxe2<0) {
digitalWrite(vitesseMotB,HIGH);
digitalWrite(sensMotB,LOW);
}
else if (analogRead(JoyStick_Z)) {
digitalWrite(vitesseMotA,LOW);
digitalWrite(vitesseMotB,LOW);
}
else {
digitalWrite(vitesseMotA,LOW); // turn motor off when between points
digitalWrite(vitesseMotB,LOW); // turn motor off when between points
}
}// stop loop
}

[/code]

You’ve changed a lot of things in this code and it makes it hard to follow… We recommend doing small incremental changes to fix one thing at a time.

For instance, I think you need to keep the pinMode(OUTPUT) and digitalWrite(LOW) for both brake pins, or else the motors might never start. Also, I it’s probably best not to do pinMode(INPUT) for the two joysticks: the analog pins don’t need to do this, and doing so might be introducing problems…

Have you tried using a minimal program to test the joysticks by themselves and check that everything is working properly? We’d recommend using the AnalogInOutSerial example from the Arduino IDE.

The USB voltage/current might be enough for your motors: it would depend on the motors that you use. We would recommend using a minimal program to test those by themselves also, to ensure that everything is working well. We found this Instructable that shows how to get started and has some sample code:

Hope this helps,

Hi

I downloaded the code. Good news: the joystick moves the motors

But when I go:

  • Right: B motor rotates clockwise
  • Left: A motor rotates clockwise
  • Top: The A motor rotates clockwise
  • Bottom: The A motor rotates clockwise
    When I press the push button (Joystick_Z): it does not stop the motors

When I release the neutral position, often the A or both engine running again …

Thank you for your help

Ok, I think you need to adjust your “if” conditions.

The the analogRead function will give a value between 0 and 1023, comparing against 0 won’t work well. When the axes of the joystick are in a neutral position, their values should be around 512 (with an error margin of maybe 100). So, you should probably be checking if your axes are greater than something like 700 for moved one direction, and checking if they’re less than 300 for moved in the other direction. The range of 300-700 should then be neutral. These values might be different for you, depending on the parts you’re using and how they’re connected.

When adjusting the values, we recommend adding a comment for each if / else-if condition to indicate if it means up/down/left/right: it’s hard to tell for us otherwise…

Also, do you want the motors to stop when the joystick in its neutral position, or do you need the motors to stay on until the JoyStick_Z button is pressed?

If you could upload some pictures of your setup, that could also help.

Hi

I would like the motor stays until the button is pressed or until it changes in its direction.

Here is a photo Thanks

[code]void loop(){ // debut de la fonction loop()
//----- lecture position Joytstick
positionAxe1=analogRead(axe1Joystick); // acquisition conversion analogique numérique sur broche analogique axe 1
delay(100);
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 (analogRead(JoyStick_Z)) // Joystick Button STOP
{ digitalWrite(vitesseMotA,LOW);
digitalWrite(vitesseMotB,LOW);
}
else {
digitalWrite(vitesseMotA,LOW); // turn motor off when between points
digitalWrite(vitesseMotB,LOW); // turn motor off when between points

}

} [/code]

Ok, in that case, I would recommend removing the final “else” condition, and also changing the analogRead(JoyStick_Z) to check if the value is less than 200: the joystick produces a low (0V) signal when the button is pressed, but we will use 200 to give some margin for signal error.

else if (analogRead(JoyStick_Z) < 200) // Joystick Button STOP  // When button pressed, analog read should be 0 or almost 0
{   digitalWrite(vitesseMotA,LOW);
  digitalWrite(vitesseMotB,LOW);
}
/* Removed
else {
digitalWrite(vitesseMotA,LOW);  // turn motor off when between points
digitalWrite(vitesseMotB,LOW);  // turn motor off when between points
}
*/