Bonjour à tous,
J'ai enfin reçu mon starting kit qui est livré avec un guide d'initiation.
En suivant le cours OC, j'avais vu cette logique pour gérer un poussoir en on/off:
/*
le bouton poussoir est connecté au pin 2 pour un mode INPUT_PULLUP
la Led est connectée au pins 4 avec une résistance de 220Ω
*/
//déclaration des variables
int pinBouton, pinLed;
boolean etatAllumage;
void setup()
{
//initialisation des variables
Serial.begin(9600);
pinBouton = 2;
pinLed = 4;
etatAllumage=0;
//définition des modes
pinMode(pinBouton, INPUT_PULLUP);
pinMode(pinLed, OUTPUT);
}
void loop()
{
Serial.print(etatAllumage);
if (etatAllumage) //on teste si etatAllumage est à 1
{
digitalWrite(pinLed, HIGH);//on allume la LED
}
else //sinon
{
digitalWrite(pinLed, LOW); //on éteint la LED
}
//lecture de l'état du bouton et stockage dans etatBouton
boolean etatPinBouton = digitalRead(pinBouton);
Serial.println(etatPinBouton);
//test des conditions
if (!etatPinBouton)//si bouton appuyé (donc le pin indique 0 car il est en mode INPUT_PULLUP)
{
if (etatAllumage) //si etatAllumage à 1
{
etatAllumage=0; //on le passe à 0
}
else //sinon
{
etatAllumage=1; //on le passe à 1
}
}
delay(200);
}
En tant que développeur, c'est une approche que j'aurais pu avoir
Cependant, dans le guide que j'ai en main, ils insistent sur le phénomène de jitter et préconisent le code suivant:
int ledpin=11; //definition digital 11 pins as pin to control the LED
int btnpin=2; //Set the digital 2 to button interface
volatile int state = LOW; // Defined output status LED Interface
void setup()
{
pinMode(ledpin,OUTPUT);//Set digital 11 port mode, the OUTPUT for the output
pinMode(btnpin,INPUT); //Set digital 2 port mode, the INPUT for the input
}
void loop()
{
if(digitalRead(btnpin)==LOW) //Detection button interface to low
{
delay(10); //Delay 10ms for the elimination of key leading-edge jitter
if(digitalRead(btnpin)==LOW) //Confirm button is pressed
{
while(digitalRead(btnpin)==LOW);//Wait for key interfaces to high
delay(10); //delay 10ms for the elimination of key trailing-edge jitter
while(digitalRead(btnpin)==LOW);//Confirm button release
state = !state; //Negate operation, each time you run the program here, state the HGIH becomes LOW, or the state becomes the LOW HGIH.
digitalWrite(ledpin,state); //Output control status LED, ON or OFF
}
}
}
ou encore la meme chose pour le jitter mais en utilisant les interruptions:
int ledpin=11; //definition digital 11 pins as pin to control the LED
int btnpin=2; //Set the digital 2 to button interface
volatile int state = LOW; //Defined output status LED Interface
void setup()
{
pinMode(ledpin, OUTPUT); //Set digital 11 port mode, the OUTPUT for the output
attachInterrupt(0, stateChange, FALLING); //Monitoring Interrupt 0 (Digital PIN 2) changes in the input pins FALLING
}
void loop()
{
digitalWrite(ledpin, state); //Output control status LED, ON or OFF
}
void stateChange() //Interrupt function
{
if(digitalRead(btnpin)==LOW) //Detection button interface to low
{
delayMicroseconds(10000); //Delay 10ms for the elimination of key leading-edge jitter
if(digitalRead(btnpin)==LOW) //Confirm button is pressed
{
state = !state; //Negate operation, each time you run the program here, state the HGIH becomes LOW, or the state becomes the LOW HGIH.
}
}
}
Bon déjà, d'après la doc des fonctions Arduino, ils utilisent mal attachInterrupt à qui il est déconseillé de passer directement le n° de la pin mais vous, qui avez de l'expérience, quelle est la bonne pratique ?
Le but du montage est d'activer ou non un relais en fonction de différentes actions:
- Via un poussoir on/off
- Via un RTC
- Via une instruction reçue en wifi
L'idée des interruptions me plait bien avec de la gestion événementielle. Pour le jitter, vaut il mieux le gérer coté soft comme ici ou bien coté hard avec un condo ?
Merci à vous tous
Chris