Arduino bar 2 moteurs PAP en simultaner et gestion de vitesse

Bonjour tout le monde.

Je suis actuellement en cour de fabrication d'une machine a cocktail gérer par arduino.
Pour l'instant j'ai fabriqué la partie qui servira a actionner les doseurs et une des deux translations.
Les moteurs PAP sont piloté par 2 cartes L298N.
(Voir vidéo çà sera plus simple à comprendre)

Mes 2 problèmes :

-Peut t-on modifier la vitesse d'un moteur PAP sans toucher à la mécanique ? (car la translation linéaire axe y avance pas).
-Avez vous des exemples de code pour actionner 2 moteur PAP en même temps.
(J'ai essayé ce tuto Tutoriel Arduino 03 en ayant programmer 2 moteurs, mais les translations ne sont pas simultanée.

Et voici mon code actuel qui actionne bien les 2 moteurs ensemble mais un peut lourd a mon gout.
J'ai dû bidouiller les stepDelay car dès que je fais fonctionner 2 moteurs çà me ralenti les moteurs.

//Moteur
int motor1inA1 = 10; // input 1 moteur doseur
int motor1inA2 = 11; // input 2 
int motor1inB1 = 12; // input 3 
int motor1inB2 = 13; // input 4 
int motor2inA1 = 6; // input 1 moteur
int motor2inA2 = 7; // input 2 
int motor2inB1 = 8; // input 3 
int motor2inB2 = 9; // input 4 
int stepDelay = 2; // Delay between steps in milliseconds
int stepDelay1 = 1; // Delay between steps in milliseconds
//Bouton
const int buttonPin = 53; //input 53 button on
int buttonState = 0;

void setup() {  
//Moteur  
 pinMode(motor1inA1, OUTPUT);     
 pinMode(motor1inA2, OUTPUT);     
 pinMode(motor1inB1, OUTPUT);     
 pinMode(motor1inB2, OUTPUT); 
 pinMode(motor2inA1, OUTPUT);     
 pinMode(motor2inA2, OUTPUT);     
 pinMode(motor2inB1, OUTPUT);     
 pinMode(motor2inB2, OUTPUT);  
//Bouton
 pinMode(buttonPin, INPUT);
}

void step1() {
 digitalWrite(motor1inA1, LOW);   
 digitalWrite(motor1inA2, HIGH);   
 digitalWrite(motor1inB1, HIGH);   
 digitalWrite(motor1inB2, LOW);
 
 }
void step11() {  
 digitalWrite(motor2inA1, LOW);   
 digitalWrite(motor2inA2, HIGH);   
 digitalWrite(motor2inB1, HIGH);   
 digitalWrite(motor2inB2, LOW);     

}
void step2() {
 digitalWrite(motor1inA1, LOW);   
 digitalWrite(motor1inA2, HIGH);   
 digitalWrite(motor1inB1, LOW);   
 digitalWrite(motor1inB2, HIGH);
 
  }
void step12() {  
 digitalWrite(motor2inA1, LOW);   
 digitalWrite(motor2inA2, HIGH);   
 digitalWrite(motor2inB1, LOW);   
 digitalWrite(motor2inB2, HIGH);     

}
void step3() {
 digitalWrite(motor1inA1, HIGH);   
 digitalWrite(motor1inA2, LOW);   
 digitalWrite(motor1inB1, LOW);   
 digitalWrite(motor1inB2, HIGH); 

  }
void step13() {  
 digitalWrite(motor2inA1, HIGH);   
 digitalWrite(motor2inA2, LOW);   
 digitalWrite(motor2inB1, LOW);   
 digitalWrite(motor2inB2, HIGH);   

}
void step4() {
 digitalWrite(motor1inA1, HIGH);   
 digitalWrite(motor1inA2, LOW);   
 digitalWrite(motor1inB1, HIGH);   
 digitalWrite(motor1inB2, LOW);

   }
void step14() { 
 digitalWrite(motor2inA1, HIGH);   
 digitalWrite(motor2inA2, LOW);   
 digitalWrite(motor2inB1, HIGH);   
 digitalWrite(motor2inB2, LOW);     

}
void stopMotor() {
 digitalWrite(motor1inA1, LOW);   
 digitalWrite(motor1inA2, LOW);   
 digitalWrite(motor1inB1, LOW);   
 digitalWrite(motor1inB2, LOW);  
 digitalWrite(motor2inA1, LOW);   
 digitalWrite(motor2inA2, LOW);   
 digitalWrite(motor2inB1, LOW);   
 digitalWrite(motor2inB2, LOW);   
}

void loop() {
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH) { 
 
  for (int i=0; i<=400; i++){ 
   step11(); 
   delay(stepDelay);  
   step12();
   delay(stepDelay);
   step13();
   delay(stepDelay);
   step14();
   delay(stepDelay);
 }  
   
 for (int i=0; i<=800; i++){ 
   step1(); 
   delay(stepDelay1);
   step11(); 
   delay(stepDelay1);
   step2();
   delay(stepDelay1);
   step12();
   delay(stepDelay1);
   step3();
   delay(stepDelay1);
   step13();
   delay(stepDelay1);
   step4();
   delay(stepDelay1);
   step14();
   delay(stepDelay1);
 } 

 
 for (int i=0; i<=200; i++){ 
   step1(); 
   delay(stepDelay);  
   step2();
   delay(stepDelay);
   step3();
   delay(stepDelay);
   step4();
   delay(stepDelay);
 }
 for (int i=0; i<=200; i++){
   step4(); 
   delay(stepDelay);
   step3();
   delay(stepDelay);
   step2();
   delay(stepDelay);
   step1();
   delay(stepDelay);
 }

 
 for (int i=0; i<=800; i++){
   step4(); 
   delay(stepDelay1);
   step14(); 
   delay(stepDelay1);
   step3();
   delay(stepDelay1);
   step13();
   delay(stepDelay1);
   step2();
   delay(stepDelay1);
   step12();
   delay(stepDelay1);
   step1();
   delay(stepDelay1);
   step11();
    delay(stepDelay1);
 }
 
for (int i=0; i<=400; i++){ 
   step14(); 
   delay(stepDelay);  
   step13();
   delay(stepDelay);
   step12();
   delay(stepDelay);
   step11();
   delay(stepDelay);
 } 
 stopMotor();
 delay(2000);
}
}

Merci si vous avez lu ce topique jusque-là je suis ouvert à tous aides, ou proposition de tuto et même si vous avez des questions ou quoi que ce soit hésiter pas.

RE

Bon j'ai réussi à alléger mon code pour le fonctionnement des 2 moteurs PAP en simultanée.
Je vous le fait partager.

Il me reste juste le problème de vitesse a régler si vous avez des idées :

#include <Stepper.h>
int stepDelay = 2;
Stepper moteur1(200, 10, 11, 12, 13); //Moteur1 doseur
Stepper moteur2(200, 6, 7, 8, 9);  //Moteur2 translation
const int buttonPin = 53; //input 53 button on
int buttonState = 0;

void setup()
{
moteur1.setSpeed(200);
moteur2.setSpeed(200);
pinMode(buttonPin, INPUT);
}

void loop()
{
buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
 
          for (int i=0; i<=2000; i++){
          moteur2.step(1);
          delay(stepDelay);
          }

      
          for (int i=0; i<=2000; i++){
          moteur1.step(1);
          moteur2.step(1);
          delay(stepDelay);
          }

          for (int i=0; i<=2000; i++){ 
          moteur1.step(-1);
          moteur2.step(-1);
          delay(stepDelay);
          }
          
          for (int i=0; i<=2000; i++){
          moteur2.step(-1);
          delay(stepDelay);
          }          
    }
}

steven63:
RE

Bon j'ai réussi à alléger mon code pour le fonctionnement des 2 moteurs PAP en simultanée.
Je vous le fait partager.

Il me reste juste le problème de vitesse a régler si vous avez des idées :

bonjour
ça depend comment/où et pourquoi tu souhaite gerer les vitesses
peut etre deriver les setspeed dans le loop ?
pour test avec 2 potentiometres "vitesses" essaye ce code
compile mais pas testé

#include <Stepper.h>
int stepDelay = 2;
Stepper moteur1(200, 10, 11, 12, 13); //Moteur1 doseur
Stepper moteur2(200, 6, 7, 8, 9);  //Moteur2 translation
const int buttonPin = 53; //input 53 button on
int buttonState = 0;
int pinVM1 = A1;    // select the input pin for the potentiometer 1
int pinVM2 = A2;    // select the input pin for the potentiometer 2

int ValM1 = 0;  //
int ValM2 = 0;  //
void setup()
{

  pinMode(buttonPin, INPUT);
}

void loop()
{
  ValM1 = analogRead(pinVM1);
  ValM2 = analogRead(pinVM2);

  
  ValM1 = map(ValM1, 0, 1023, 0, 1023); // map eventuel pour contraindre 0/1023 en une autre etendue
  ValM2 = map(ValM2, 0, 1023, 0, 1023); 
  
  moteur1.setSpeed(ValM1);
  moteur2.setSpeed(ValM2);
  
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {

    for (int i = 0; i <= 2000; i++) {
      moteur2.step(1);
      delay(stepDelay);
    }


    for (int i = 0; i <= 2000; i++) {
      moteur1.step(1);
      moteur2.step(1);
      delay(stepDelay);
    }

    for (int i = 0; i <= 2000; i++) {
      moteur1.step(-1);
      moteur2.step(-1);
      delay(stepDelay);
    }

    for (int i = 0; i <= 2000; i++) {
      moteur2.step(-1);
      delay(stepDelay);
    }
  }
}