Bonsoir
Le montage et le code suivants permettent de faire tourner un des deux moteurs de type nema 17 ou les deux en même temps lorsque l'on appuie sur les boutons qui leurs sont dédiés de tel nombre de pas ou en continue s'il l'on reste appuyé sur le(s) bouton(s).
#include "AccelStepper.h" // https://www.pjrc.com/teensy/td_libs_AccelStepper.html
int boutonPin = 3;
int boutonPin2 = 4;
const int boutonPinEtatOn = LOW; // Etat lu du bouton quand pressé (LOW pour PULL_UP)
const int mpapDirPin = 5; // Pour driver MPAP pin pas
const int mpapStepPin = 6; // Pour driver MPAP pin direction
const int mpapDirPin2 = 2; // Pour driver MPAP pin pas
const int mpapStepPin2 = 7; // Pour driver MPAP pin direction
AccelStepper mpap1(1, mpapStepPin, mpapDirPin); // 1 pour A4988
AccelStepper mpap2(1, mpapStepPin2, mpapDirPin2);
int mpapNombreDePasAtourner = 200;
void setup()
{
mpap1.setMaxSpeed(2000);
mpap1.setAcceleration(2000);
mpap2.setMaxSpeed(1000);
mpap2.setAcceleration(1000);
pinMode(boutonPin, INPUT_PULLUP);
pinMode(boutonPin2, INPUT_PULLUP);
// DIR STEP ENA
mpap1.setPinsInverted(false, false, true);
mpap1.enableOutputs(); // Activer les signaux du MPAP
mpap2.setPinsInverted(false, false, true);
mpap2.enableOutputs(); // Activer les signaux du MPAP
}
void loop()
{
boolean etatBouton=digitalRead(boutonPin);
if (etatBouton == boutonPinEtatOn)
{
mpap1.move(mpapNombreDePasAtourner*2);
}
mpap1.run();
boolean etatBouton2=digitalRead(boutonPin2);
if (etatBouton2 == boutonPinEtatOn)
{
mpap2.move(mpapNombreDePasAtourner);
}
mpap2.run();
}
A présent j'aimerai associer le montage et code précédent avec le montage et code suivant qui fonctionnent avec des potentiomètres.
const byte pinPotIngredient1 = A1;
const byte pinPotIngredient2 = A2;
const int minPotIngredient1 = 0;
const int maxPotIngredient1 = 1023;
const int minPotIngredient2 = 0;
const int maxPotIngredient2 = 1023;
int Ingredient1 = constrain(map(analogRead(pinPotIngredient1), minPotIngredient1, maxPotIngredient1, 0, 100), 0, 100);
int Ingredient2 = constrain(map(analogRead(pinPotIngredient2), minPotIngredient2, maxPotIngredient2, 0, 100), 0, 100);
Serial.print("Ingredient1: "); Serial.print(ratio(Ingredient1, Ingredient2)); Serial.print("%\t");
Serial.print("Ingredient2: "); Serial.print(ratio(Ingredient2, Ingredient1)); Serial.println("%");
delay(500);
L'ennui est que même avec des moteurs de type nema 17 le code des potentiomètres empêche les moteurs de tourner correctement (ils semblent tourner comme les aiguilles d'une montre et s'arrêter après avoir parcouru le nombre de pas dans le programme) lorsque les deux programme sont combinés comme suit.
#include "AccelStepper.h"
int boutonPin = 3;
int boutonPin2 = 4;
const byte pinPotIngredient1 = A1;
const byte pinPotIngredient2 = A2;
const int minPotIngredient1 = 0;
const int maxPotIngredient1 = 1023;
const int minPotIngredient2 = 0;
const int maxPotIngredient2 = 1023;
const int boutonPinEtatOn = LOW; // Etat lu du bouton quand pressé (LOW pour PULL_UP)
const int mpapDirPin = 5; // Pour driver MPAP pin pas
const int mpapStepPin = 6; // Pour driver MPAP pin direction
const int mpapDirPin2 = 2; // Pour driver MPAP pin pas
const int mpapStepPin2 = 7; // Pour driver MPAP pin direction
AccelStepper mpap1(1, mpapStepPin, mpapDirPin); // 1 pour A4988
AccelStepper mpap2(1, mpapStepPin2, mpapDirPin2);
int mpapNombreDePasAtourner = 200;
int ratio(int nb1, int nb2){
if ((nb1 == 0) && (nb2 == 0)) return 0;
return (100.0 * nb1) / (nb1 + nb2);
}
void setup()
{
mpap1.setMaxSpeed(2000);
mpap1.setAcceleration(2000);
mpap2.setMaxSpeed(1000);
mpap2.setAcceleration(1000);
pinMode(boutonPin, INPUT_PULLUP);
pinMode(boutonPin2, INPUT_PULLUP);
// DIR STEP ENA
mpap1.setPinsInverted(false, false, true);
mpap1.enableOutputs(); // Activer les signaux du MPAP
mpap2.setPinsInverted(false, false, true);
mpap2.enableOutputs(); // Activer les signaux du MPAP
Serial.begin(115200);
}
void loop()
{
boolean etatBouton=digitalRead(boutonPin);
if (etatBouton == boutonPinEtatOn)
{
mpap1.move(mpapNombreDePasAtourner*2);
}
mpap1.run();
boolean etatBouton2=digitalRead(boutonPin2);
if (etatBouton2 == boutonPinEtatOn)
{
mpap2.move(mpapNombreDePasAtourner);
}
mpap2.run();
int Ingredient1 = constrain(map(analogRead(pinPotIngredient1), minPotIngredient1, maxPotIngredient1, 0, 100), 0, 100);
int Ingredient2 = constrain(map(analogRead(pinPotIngredient2), minPotIngredient2, maxPotIngredient2, 0, 100), 0, 100);
Serial.print("Ingredient1: "); Serial.print(ratio(Ingredient1, Ingredient2)); Serial.print("%\t");
Serial.print("Ingredient2: "); Serial.print(ratio(Ingredient2, Ingredient1)); Serial.println("%");
delay(500);
}
On peut placer les instructions d'affichage dans la condition comme suit mais cela empêches les moteurs de continuer à tourner en continue, or je voudrais que les moteurs effectuent un pas pour une pression et continuent de tourner si l'on reste appuyer sur leurs boutons.
#include "AccelStepper.h"
int boutonPin = 3;
int boutonPin2 = 4;
const byte pinPotIngredient1 = A1;
const byte pinPotIngredient2 = A2;
const int minPotIngredient1 = 0;
const int maxPotIngredient1 = 1023;
const int minPotIngredient2 = 0;
const int maxPotIngredient2 = 1023;
const int boutonPinEtatOn = LOW; // Etat lu du bouton quand pressé (LOW pour PULL_UP)
const int mpapDirPin = 5; // Pour driver MPAP pin pas
const int mpapStepPin = 6; // Pour driver MPAP pin direction
const int mpapDirPin2 = 2; // Pour driver MPAP pin pas
const int mpapStepPin2 = 7; // Pour driver MPAP pin direction
AccelStepper mpap1(1, mpapStepPin, mpapDirPin); // 1 pour A4988
AccelStepper mpap2(1, mpapStepPin2, mpapDirPin2);
int mpapNombreDePasAtourner = 200;
int ratio(int nb1, int nb2){
if ((nb1 == 0) && (nb2 == 0)) return 0;
return (100.0 * nb1) / (nb1 + nb2);
}
void setup()
{
mpap1.setMaxSpeed(2000);
mpap1.setAcceleration(2000);
mpap2.setMaxSpeed(1000);
mpap2.setAcceleration(1000);
pinMode(boutonPin, INPUT_PULLUP);
pinMode(boutonPin2, INPUT_PULLUP);
// DIR STEP ENA
mpap1.setPinsInverted(false, false, true);
mpap1.enableOutputs(); // Activer les signaux du MPAP
mpap2.setPinsInverted(false, false, true);
mpap2.enableOutputs(); // Activer les signaux du MPAP
Serial.begin(115200);
}
void loop()
{
boolean etatBouton=digitalRead(boutonPin);
if (etatBouton == boutonPinEtatOn)
{
mpap1.move(mpapNombreDePasAtourner*2);
int Ingredient1 = constrain(map(analogRead(pinPotIngredient1), minPotIngredient1, maxPotIngredient1, 0, 100), 0, 100);
int Ingredient2 = constrain(map(analogRead(pinPotIngredient2), minPotIngredient2, maxPotIngredient2, 0, 100), 0, 100);
Serial.print("Ingredient1: "); Serial.print(ratio(Ingredient1, Ingredient2)); Serial.print("%\t");
Serial.print("Ingredient2: "); Serial.print(ratio(Ingredient2, Ingredient1)); Serial.println("%");
delay(500);
}
mpap1.run();
boolean etatBouton2=digitalRead(boutonPin2);
if (etatBouton2 == boutonPinEtatOn)
{
mpap2.move(mpapNombreDePasAtourner);
int Ingredient1 = constrain(map(analogRead(pinPotIngredient1), minPotIngredient1, maxPotIngredient1, 0, 100), 0, 100);
int Ingredient2 = constrain(map(analogRead(pinPotIngredient2), minPotIngredient2, maxPotIngredient2, 0, 100), 0, 100);
Serial.print("Ingredient1: "); Serial.print(ratio(Ingredient1, Ingredient2)); Serial.print("%\t");
Serial.print("Ingredient2: "); Serial.print(ratio(Ingredient2, Ingredient1)); Serial.println("%");
delay(500);
}
mpap2.run();
}
J'ai donc essayer d'ajouter une fonction "affichage" ainsi:
#include "AccelStepper.h"
int boutonPin = 3;
int boutonPin2 = 4;
const byte pinPotIngredient1 = A1;
const byte pinPotIngredient2 = A2;
const int minPotIngredient1 = 0;
const int maxPotIngredient1 = 1023;
const int minPotIngredient2 = 0;
const int maxPotIngredient2 = 1023;
const int boutonPinEtatOn = LOW; // Etat lu du bouton quand pressé (LOW pour PULL_UP)
const int mpapDirPin = 5; // Pour driver MPAP pin pas
const int mpapStepPin = 6; // Pour driver MPAP pin direction
const int mpapDirPin2 = 2; // Pour driver MPAP pin pas
const int mpapStepPin2 = 7; // Pour driver MPAP pin direction
AccelStepper mpap1(1, mpapStepPin, mpapDirPin); // 1 pour A4988
AccelStepper mpap2(1, mpapStepPin2, mpapDirPin2);
int mpapNombreDePasAtourner = 200;
int ratio(int nb1, int nb2){
if ((nb1 == 0) && (nb2 == 0)) return 0;
return (100.0 * nb1) / (nb1 + nb2);
}
void setup()
{
mpap1.setMaxSpeed(2000);
mpap1.setAcceleration(2000);
mpap2.setMaxSpeed(1000);
mpap2.setAcceleration(1000);
pinMode(boutonPin, INPUT_PULLUP);
pinMode(boutonPin2, INPUT_PULLUP);
// DIR STEP ENA
mpap1.setPinsInverted(false, false, true);
mpap1.enableOutputs(); // Activer les signaux du MPAP
mpap2.setPinsInverted(false, false, true);
mpap2.enableOutputs(); // Activer les signaux du MPAP
Serial.begin(115200);
}
void loop(){
boolean etatBouton=digitalRead(boutonPin);
if (etatBouton == boutonPinEtatOn)
{
mpap1.move(mpapNombreDePasAtourner*2);
}
mpap1.run();
boolean etatBouton2=digitalRead(boutonPin2);
if (etatBouton2 == boutonPinEtatOn)
{
mpap2.move(mpapNombreDePasAtourner);
}
mpap2.run();
affichage(etatBouton, etatBouton2);
}
void affichage(boolean nombre1, boolean nombre2){
if ((nombre1==HIGH)&&(nombre2==HIGH)){
int Ingredient1 = constrain(map(analogRead(pinPotIngredient1), minPotIngredient1, maxPotIngredient1, 0, 100), 0, 100);
int Ingredient2 = constrain(map(analogRead(pinPotIngredient2), minPotIngredient2, maxPotIngredient2, 0, 100), 0, 100);
Serial.print("Ingredient1: "); Serial.print(ratio(Ingredient1, Ingredient2)); Serial.print("%\t");
Serial.print("Ingredient2: "); Serial.print(ratio(Ingredient2, Ingredient1)); Serial.println("%");
delay(500);
}
}
Après téléversement j'ai alors rencontré deux soucis:
1-le moteur ne continue pas de tourner lorsque l'on reste appuyé et attend le relachement avant d'avancer d'un pas
2-le dernier moteur dont le bouton a été appuyé (ou les deux si leur boutons ont été appuyé en même temps) continue à tourner une fois son bouton relaché de très petit pas comme les aiguilles d'une montre.
J'ai essayé de convertir le code accelstepper en respectant les normes de la bibliothèque MTstepStepper pour voir ce que cela donnerait de la manière suivante:
//#include "AccelStepper.h"
#include "MTstepStepper.h"
#include "MTbutton.h"
int boutonPin = 3;
int boutonPin2 = 4;
const byte pinPotIngredient1 = A1;
const byte pinPotIngredient2 = A2;
const int minPotIngredient1 = 0;
const int maxPotIngredient1 = 1023;
const int minPotIngredient2 = 0;
const int maxPotIngredient2 = 1023;
const int boutonPinEtatOn = LOW; // Etat lu du bouton quand pressé (LOW pour PULL_UP)
const int mpapDirPin = 5; // Pour driver MPAP pin pas
const int mpapStepPin = 6; // Pour driver MPAP pin direction
const int mpapDirPin2 = 2; // Pour driver MPAP pin pas
const int mpapStepPin2 = 7; // Pour driver MPAP pin direction
//AccelStepper mpap1(1, mpapStepPin, mpapDirPin);
//AccelStepper mpap2(1, mpapStepPin2, mpapDirPin2);
MTstepStepper mpap1(pin_Step mpapStepPin, pin_Dir mpapDirPin);
int mpapNombreDePasAtourner = 200;
int ratio(int nb1, int nb2){
if ((nb1 == 0) && (nb2 == 0)) return 0;
return (100.0 * nb1) / (nb1 + nb2);
}
void avance(void)
{
mpap1.move(500);
}
MTbutton Bouton(boutonPin, avance);
void setup()
{
//mpap1.setMaxSpeed(2000);
//mpap1.setAcceleration(2000);
//mpap2.setMaxSpeed(1000);
//mpap2.setAcceleration(1000);
pinMode(boutonPin, INPUT_PULLUP);
pinMode(boutonPin2, INPUT_PULLUP);
// DIR STEP ENA
//mpap1.setPinsInverted(false, false, true);
//mpap1.enableOutputs(); // Activer les signaux du MPAP
//mpap2.setPinsInverted(false, false, true);
//mpap2.enableOutputs(); // Activer les signaux du MPAP
Serial.begin(115200);
}
void loop(){
/*boolean etatBouton=digitalRead(boutonPin);
if (etatBouton == boutonPinEtatOn)
{
mpap1.move(mpapNombreDePasAtourner*2);
}
mpap1.run();
boolean etatBouton2=digitalRead(boutonPin2);
if (etatBouton2 == boutonPinEtatOn)
{
mpap2.move(mpapNombreDePasAtourner);
}
mpap2.run();*/
int Ingredient1 = constrain(map(analogRead(pinPotIngredient1), minPotIngredient1, maxPotIngredient1, 0, 100), 0, 100);
int Ingredient2 = constrain(map(analogRead(pinPotIngredient2), minPotIngredient2, maxPotIngredient2, 0, 100), 0, 100);
Serial.print("Ingredient1: "); Serial.print(ratio(Ingredient1, Ingredient2)); Serial.print("%\t");
Serial.print("Ingredient2: "); Serial.print(ratio(Ingredient2, Ingredient1)); Serial.println("%");
delay(500);
}
Il semblerait qu'a cause des mes connaissances limités de cette bibliothèque le programme ne soit pas au point. Comment puis je m'y prendre pour que le programme fasse marcher les moteurs comme voulu tout en affichant les valeurs des potentiomètres dans le moniteur série ?
Cordialement


