Moteur et bouton poussoir / motor and push button

Bonjour,
je souhaiterais contrôler mon moteur grâce à un bouton poussoir. Lorsque j’appuie sur le bouton, je voudrais que le moteur tourne pendant 30 seconde, et qu’il s’arrête une fois fini. Je ne vois pas où est l’erreur dans mon programme et le bouton est bien reconnu dans le moniteur série. J’utilise un bouton 2 broches mais sur le schéma c’est un 4 broches. J’utilise un Arduino Uno. Le but final serait de relier le tout à un écran LCD en I2C (la partie de l’écran avec le bouton est déjà terminée), afin de faire en sorte que lorsque j’appuie sur le bouton, le moteur tourne pendant 30 secondes et un opération se fait sur l’écran LCD.
/
Hello,
I would like to control my engine with a push button. When I press the button, I would like the engine to run for 30 seconds, and stop when finished. I don’t see where the error is in my program and the button is well recognized in the serial monitor. I use a 2 pin button but on the diagram it’s a 4 pin. I’m using an Arduino Uno. The final goal would be to connect everything to an LCD screen in I2C (the part of the screen with the button is already finished), so that when I press the button, the engine runs for 30 seconds and an operation is performed on the LCD screen.

Le programme / the program :

int pin1Moteur1=12; //cmd 1 du moteur 1
int pin2Moteur1=8; // cmd 2 du moteur 1
int pinPMoteur1=11; // PMM du moteur 1
int pinBouton=7; // bouton poussoir
int EtatBouton;

void setup()
{
Serial.begin(9600);
pinMode(7,INPUT); //Signale à l’Arduino que la connexion 7 doit pouvoir recevoir du courant
pinMode(12,OUTPUT); //Signale à l’Arduino que la connexion 13 doit pouvoir envoyer du courant
pinMode(8,OUTPUT); //Signale à l’Arduino que la connexion 8 doit pouvoir envoyer du courant
pinMode(11,OUTPUT); //Signale à l’Arduino que la connexion 11 doit pouvoir envoyer du courant
}

void loop()
{
boolean a=digitalRead(7);// et on l’affecte à la variable “a”
Serial.println(a); // on l’affiche sur le moniteur
delay(5);

if (EtatBouton == LOW);
{
digitalWrite(12,HIGH);
digitalWrite(8,LOW);
analogWrite(11,255);
delay(5000);
}

if (EtatBouton == HIGH);
{
digitalWrite(12,LOW);
digitalWrite(8,LOW);
analogWrite(11,0);
delay(5000);
}
}

Merci beaucoup / Thank you very much :slight_smile:

You are reading the position of the switch into varialble “a”, but then checking “EtatBouton” to decide whether to run the motor or not. Those need to be the same variable.

You should not need the 5 second delay when turning the motor on or off, but you do need a way to keep the motor on for 30 seconds once it has been turned on. Otherwise, with the code you have, it will turn off within 5 seconds of releasing the button.

You need some extra code to remember that when the button was pushed, and turn the motor off 30 seconds later. Also, if I understand the intent of your description, button presses and release should be ignored once the motor is turned on. Until it is turned off later. You should read some of the tutorials on the arduino site. I did not check if the commands you use to control the motor are correct. If they are, and ALL the program is supposed to do is control the motor, that can be handled with:
\
Vous lisez la position de l’interrupteur en varialble “a”, mais cochez ensuite “EtatBouton” pour décider de faire fonctionner le moteur ou non. Celles-ci doivent être la même variable.

Vous ne devriez pas avoir besoin du délai de 5 secondes lorsque vous allumez ou éteignez le moteur, mais vous avez besoin d’un moyen de garder le moteur allumé pendant 30 secondes une fois qu’il a été allumé. Sinon, avec le code que vous avez, il s’éteindra dans les 5 secondes suivant le relâchement du bouton.

Vous avez besoin d’un code supplémentaire pour * vous souvenir * que lorsque le bouton a été enfoncé et éteindre le moteur 30 secondes plus tard. De plus, si je comprends l’intention de votre description, les pressions et relâchements des boutons doivent être ignorés une fois le moteur allumé. Jusqu’à ce qu’il soit désactivé plus tard. Vous devriez lire certains des tutoriels sur le site Arduino. Je n’ai pas vérifié si les commandes que vous utilisez pour contrôler le moteur sont correctes. S’ils le sont, et * TOUS * le programme est censé faire est de contrôler le moteur, qui peut être géré avec:

void loop()
{
  boolean EtatBouton=digitalRead(7);// et on l’affecte à la variable “EtatBouton”
  
  if (EtatBouton == LOW);
  {
    Serial.println("Le bouton a été enfoncé");
    digitalWrite(12,HIGH);
    digitalWrite(8,LOW);
    analogWrite(11,255);
    delay(30000);
    Serial.println("Le moteur est allumé depuis 30 secondes");
    digitalWrite(12,LOW);
    digitalWrite(8,LOW);
    analogWrite(11,0);
  }
}

With the logic you describe, you never need to check when the button is released. Only when it is pressed. Once a press is detected, turn the motor on for 30 seconds, then turn it off. After that, start watching for the next button press.

With my example code, the int EtatBouton; line needs to be removed as well.

If the program needs to do more than control the motor, that code needs to be replaced. Learn about using the millis() function to time how long since the last button press.

For Arduino code questions, the Ardunio forums are a better place to go.
\
Avec la logique que vous décrivez, vous n’avez jamais besoin de vérifier quand le bouton est relâché. Seulement quand il est pressé. Une fois qu’une pression est détectée, allumez le moteur pendant 30 secondes, puis éteignez-le. Après cela, commencez à regarder la prochaine pression sur le bouton.

Avec mon exemple de code, la ligne int EtatBouton; doit également être supprimée.

Si le programme doit faire plus que contrôler le moteur, ce code doit être remplacé. Apprenez à utiliser la fonction millis () pour chronométrer le temps écoulé depuis la dernière pression sur un bouton.

Pour les questions sur le code Arduino, les forums Ardunio sont un meilleur endroit où aller.