Combinaison à 6 boutons

Bonjour bonjour,

je souhaiterai soumettre un problème que je n’arrive pas à résoudre malgré toutes mes tentatives.

Pour le travail, j’ai programmé une machine qui s’active à en entrant une combinaison à 6 boutons.

Les boutons sont reliés à mon arduino qui comporte ce code :

const int button1 = 7;
const int button2 = 6;
const int button3 = 5;
const int button4 = 4;
const int button5 = 3;
const int button6 = 2;
const int button7 = 1;
const int redLed = 1; //red LED is on pin 4
const int aimant = 8; //aimant is pin 8
const int voyant = 9;

int valeurbouton7 = 0;
int pikoboo = LOW;
int code = {1,2,3,4,5,6}; //the desired code is entered in this array,
//separated by commas

int entered[6]; //create a new empty array for the code entered by
//the user (has 4 elements)

void setup(){ //run once at sketch startup
Serial.begin(9600); //begin Serial
pinMode(pikoboo, INPUT_PULLUP);
pinMode(button1, INPUT_PULLUP); //button 1 is an input
pinMode(button2, INPUT_PULLUP); //button 2 is an input
pinMode(button3, INPUT_PULLUP); //button 3 is an input
pinMode(button4, INPUT_PULLUP); //button 4 is an input
pinMode(button5, INPUT_PULLUP); //button 3 is an input
pinMode(button6, INPUT_PULLUP); //button 4 is an input
pinMode(button7, INPUT_PULLUP); //button 7 is an input
pinMode(aimant, OUTPUT); // the green LED is an output
pinMode(voyant, OUTPUT); // the green LED is an output
digitalWrite(aimant,HIGH); //aimant alimanté
digitalWrite(voyant,HIGH); //voyant eteint
Serial.println(“setup”);
for (int i = 0; i < 6;i++){ //work through numbers 0-3
Serial.println(code*); //print each digit of the code*
_ Serial.println(entered*); //print each element of the entered_
_
//array (this was for me to check that it*_
* //started at 0*
* }*
* delay(200);*
}
void checkEntered1(int button1 /* define the 1,2,3 or 4 as an integer called button */){ //check the first element of the entered[] array
* if (entered[0] != 0){ //if it is not a zero, i.e. it has already been inputted*
* checkEntered2(button1); //move on to checkEntered2, passing it “button”*
* }*

* else if(entered[0] == 0){ //if it is zero, i.e. if it hasn’t been defined with a button yet*
* // si bouton bon set valeur*
* if (button1 == code[0])*
* entered[0] = button1;*
* }*
}
void checkEntered2(int button2){ //check the second element of the entered[] array
* if (entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted*
* checkEntered3(button2); //move on to checkEntered3, passing it “button”*
* }*

* else if(entered[1] == 0){ //if it is zero, i.e. if it hasn’t been defined with a button yet*
* // si bouton bon set valeur*
* if (button2 == code[1])*
* entered[1] = button2;*
* else*
* resetSaisie();*
* }*

}
void checkEntered3(int button3){ //check the third element of the entered[] array
* if (entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted*
* checkEntered4(button3); //move on to checkEntered4, passing it “button”*
* }*

* else if (entered[2] == 0){ //if it is zero, i.e. if it hasn’t been defined with a button yet*
* // si bouton bon set valeur*
* if (button3 == code[2])*
* entered[2] = button3;*
* else*
* resetSaisie();*
* }*

}
void checkEntered4(int button4){ //check the third element of the entered[] array
* if (entered[3] != 0){ //if it is not a zero, i.e. it has already been inputted*
* checkEntered5(button4); //move on to checkEntered4, passing it “button”*
* }*

* else if (entered[3] == 0){ //if it is zero, i.e. if it hasn’t been defined with a button yet*
* // si bouton bon set valeur*
* if (button4 == code[3])*
* entered[3] = button4;*
* else*
* resetSaisie();*
* }*

}
void checkEntered5(int button5){ //check the third element of the entered[] array
* if (entered[4] != 0){ //if it is not a zero, i.e. it has already been inputted*
* checkEntered6(button5); //move on to checkEntered4, passing it “button”*
* }*

* else if (entered[4] == 0){ //if it is zero, i.e. if it hasn’t been defined with a button yet*
* // si bouton bon set valeur*
* if (button5 == code[4])*
* entered[4] = button5;*
* else*
* resetSaisie();*
* }*

}
void checkEntered6(int button6){ //check the fourth element of the entered[] array
* if (entered[5] == 0){ //if it is zero, i.e. if it hasn’t been defined with a button yet*
* entered[5] = button6; //set the final element as the button that has been pressed*
* delay(100); //allow time for processing*
* compareCode(); //call the compareCode function*
* }*
* else*
* resetSaisie();*
}
void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
* for (int i = 0; i<6;i++){ //these three lines are for debugging*
_ Serial.print(entered*, 128);
Serial.print(" - “);
}
Serial.println(”.");
Serial.println(“Hallelujah !!!”);
digitalWrite(aimant, LOW); //turn the green LED on*
* pikoboo = LOW;
delay(10000);
digitalWrite(aimant, HIGH); // realimente l’aimant*
* resetSaisie();
}
void resetSaisie(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
// reset*
* Serial.println(“Reset saisie”);
for (int i = 0; i < 6; i++){
entered = 0;
delay(200);
}
}
void loop(){ //run repeatedly
while ( pikoboo == LOW)
{
Serial.println(“En attente”);
delay(150);
valeurbouton7 = digitalRead(button7);*_

* if (valeurbouton7 == LOW)*
* {*
* pikoboo = HIGH;*
* digitalWrite(voyant,LOW); //voyant allumé*
* Serial.println(“Console activée”);*
* } else {*
* if (digitalRead(button1)==LOW && digitalRead(button6)==LOW)*
* {*
* digitalWrite(voyant, HIGH); //voyant eteint*
* Serial.println(“Comboooooo”);*
* }*
* }*
* delay(50);*
* }*

* if (digitalRead(button1) == LOW){ //if button1 is pressed*
* Serial.println(“Bouton1”);*
* checkEntered1(1); //call checkEntered and pass it a 1*
* delay(1000);//wait, needed for correct functioning, otherwise*
* while (digitalRead(button1) == LOW) {delay(250);};*

* }*
* else if (digitalRead(button2) == LOW){ //if button2 is pressed*
* Serial.println(“Bouton2”);*
* checkEntered1(2); //call checkEntered1 and pass it a 2*
* delay(1000);//wait, needed for correct functioning, otherwise*
* while (digitalRead(button2) == LOW) {delay(250);};*
* }*
* else if (digitalRead(button3) == LOW){ //if button3 is pressed*
* Serial.println(“Bouton3”);*
* checkEntered1(3); //call checkEntered1 and pass it a 3*
* delay(1000);//wait, needed for correct functioning, otherwise*
* while (digitalRead(button3) == LOW) {delay(250);};*
* }*
* else if (digitalRead(button4) == LOW){ //if button4 is pressed*
* Serial.println(“Bouton4”);*
* checkEntered1(4); //call checkEntered1 and pass it a 4*
* delay(1000);//wait, needed for correct functioning, otherwise*
* while (digitalRead(button4) == LOW) {delay(250);};*
* }*
* else if (digitalRead(button5) == LOW){ //if button3 is pressed*
* Serial.println(“Bouton5”);*
* checkEntered1(5); //call checkEntered1 and pass it a 3*
* delay(1000);//wait, needed for correct functioning, otherwise*
* while (digitalRead(button4) == LOW) {delay(250);};*
* }*
* else if (digitalRead(button6) == LOW){ //if button4 is pressed*
* Serial.println(“Bouton6”);*
* checkEntered1(6); //call checkEntered1 and pass it a 5*
* delay(1000);//wait, needed for correct functioning, otherwise*
* while (digitalRead(button6) == LOW) {delay(250);};*
* }*

}
J’ai bien réussi à faire en sorte qu’une erreur de boutons entraîne un reset de la dite combinaison mais malheureusement le dernier bouton pose problème : quelque soit le dernier bouton sur lequel j’appuie, la machine s’active. Par exemple 1,2,3,4,5,2 ou 1,2,3,4,5,5 fonctionne.
J’ai beau regardé mon code, je ne trouve pas où le problème se trouve.
Pouvez me venir en aide je vous prie ?
Merci infiniment ! :slight_smile:

const int button6 = 2;
const int button7 = 1;

les pins 1 et 2 sont utilisées par la classe Serial pour tous vos affichages

int entered[6]; //create a new empty array for the code entered by
                //the user (has [color=red]4 elements[/color])

→ non 6 :slight_smile:

int pikoboo = LOW;
...
  pinMode(pikoboo, INPUT_PULLUP);

pikoboo est un numéro de pin ou un état ?

  digitalWrite(aimant,HIGH); //aimant alimanté comment est connecté l’aimant à votre Arduino?

pas mal d’autres soucis…si vous voulez vous pouvez étudier cette librairie (ou l’utiliser)

corrigez votre post ci dessus et rajoutez les code tags autour du code:
[code]`` [color=blue]// votre code ici[/color] ``[/code].

ça doit ressembler à cela:// votre code ici
(faites aussi ctrl-T (PC) or cmd-T (Mac) dans l’IDE avant de copier le code pour qu’il soit indenté correctement)

Merci pour votre réponse rapide.

Oui en effet il y a des lignes de commentaires que je n'ai pas modifié, je m'étais basé sur un code pour une combinaison à 4 boutons mais il ne mettait pas le reset.

Alors Pikoboo est un état, s'il n'est pas allumé, il est impossible de composer la moindre combinaison, l'arduino attend que le Pikoboo soit allumé.

L'aimant est relié sur le Pin 8, nous avons relié l'Arduino à un relais 12V afin de pouvoir alimenter l'aimant une fois la bonne combinaison entrée.

J'ai essayé de mettre le codetags mais du coup mon code devient en commentaire si je ne dis pas de bêtise.