Bonjour à tous
Je suis assez nouveau avec la programmation Arduino et je bloque sur un programme.
Le but est de démarrer, à l’aide d’un bouton poussoir, un moteur CC sur une durée de temps prédéfinie de 1 minute donc avec une temporisation. Le moteur est géré par un contrôleur de type HBridge. J’utilise une Arduino Nano ESP32.
Jusque là, pas de problème j’ai fait un programme qui fonctionne.
Le voici:
int in1Pin = D4; // pin to connect to motor controller board
int in2Pin = D5; // pin to connect to motor controller board
int sleepPin = D6; // pin to disable motor controller sleep mode
const int buttonPin = D3; // switch
const int ledPin = D13; // Arduino Nano integrated LED
int buttonState = 0; // variable for reading the button status
void setup() {
pinMode(buttonPin, INPUT); // Set the push button pin as input
pinMode(in1Pin, OUTPUT); // Set the motor control pins as outputs
pinMode(in2Pin, OUTPUT); // Set the motor control pins as outputs
pinMode(sleepPin, OUTPUT); // Set the motor control sleep pin as output
pinMode(ledPin, OUTPUT); // Set the led pin as output
digitalWrite(sleepPin, LOW); // enable the motor controller sleep mode
digitalWrite(in2Pin, LOW); // set the motor direction
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the button value
if (buttonState == HIGH) {
digitalWrite(sleepPin, HIGH); // disable the motor controller sleep mode
delay (500); // waits for 500ms
digitalWrite(in1Pin, HIGH); // turn the motor on
digitalWrite(ledPin, HIGH); // turn the led on
delay (60000); // waits for 60 seconds (0.7 liter)
digitalWrite(in1Pin, LOW); // turn the motor off
digitalWrite(ledPin, LOW); // turn the led off
}
else {
digitalWrite(in1Pin, LOW); // if the switch is low, motor will turn off
digitalWrite(ledPin, LOW); // turn the led off
digitalWrite(sleepPin, LOW); // enable the motor controller sleep mode
}
}
Je souhaite cependant pouvoir arrêter le cycle à tout moment, en faisant un appuis long sur le bouton poussoir, et c’est là que je butte…
J’ai décidé d’utiliser la bibliothèque Onebutton pour différencier les fonctions ‘appuis court’ et ‘appui long’, mais mon programme un comportement erratique et je n’arrive toujours pas à interrompre le cycle d’une minute…
Pourriez éclairer ma chandelle?
Voici le programme qui compile mais ne fonctionne pas…
#include <OneButton.h>
const byte in1Pin = D4; // pin to connect to motor controller board
const byte in2Pin = D5; // pin to connect to motor controller board
const byte sleepPin = D6; // pin to disable motor controller sleep mode
const byte buttonPin = D3; // switch
const byte ledPin = D13; // Arduino Nano integrated LED
const unsigned long seuilTemps = 3000; // seuil de temps d'appui long
OneButton bouton(buttonPin); // set the IMPUT_PULLUP
// dans cette fonction, donc le clique simple, on demarre le moteur
void simpleClick() {
digitalWrite(sleepPin, HIGH); // disable the motor controller sleep mode
digitalWrite(ledPin, HIGH);
digitalWrite(in1Pin, HIGH); // turn the motor on
digitalWrite(in2Pin, LOW); // set the motor direction
delay (10000); // waits for 10 seconds
digitalWrite(in1Pin, LOW); // turn the motor off
digitalWrite(in2Pin, LOW);
digitalWrite(sleepPin, LOW); // enable the motor controller sleep mode
digitalWrite(ledPin, LOW);
}
// dans cette fonction, donc le click long, on arrete le moteur et on active le mode sleep
void clickLong() {
digitalWrite(sleepPin, LOW); // enable the motor controller sleep mode
digitalWrite(in1Pin, LOW); // turn the motor off
digitalWrite(in2Pin, LOW);
}
void setup() {
pinMode(in1Pin, OUTPUT); // Set the motor control pins as outputs
pinMode(in2Pin, OUTPUT); // Set the motor control pins as outputs
pinMode(sleepPin, OUTPUT); // Set the motor control sleep pin as output
pinMode(ledPin, OUTPUT); // Set the led pin as output
bouton.setPressMs(seuilTemps); // temps d'appui long
bouton.attachClick(simpleClick); // fonction appelée en cas d'appui court pour lancer le moteur
bouton.attachLongPressStop(clickLong); // fonction appelée en cas d'appui long pour le mode veille
}
void loop() {
bouton.tick(); // keep watching the push button
// You can implement other code in here or just wait a while
delay (10);
}