Bonjour à tous et à toutes,
Pour un escape game, nous avons créer une machine où les joueurs doivent rentrer une combinaison de touches. Pour cela, nous avons relier des boutons à un arduino que nous avons charger avec le code que vous trouverez plus loin.
Le code est correct (cela a été confirmer par une personne douée en Arduino) cependant un problème persiste.
Lorsque les joueurs presse le premier bouton (button1), il arrive environ 3 fois sur 5 qu'au lieu d'attendre l'appuie du second bouton, l'arduino provoque la resetSaisie de la combinaison.
Peut être est-ce du à des éléments extérieurs connecté à l'arduino (un récepteur laser connecté à un picoboo qui est un connnecteur, ce connecteur est lui même relié à l'arduino. Tant que le picoboo n'est pas actif, le programme de l'arduino ne peut être lancé.
Voici un lien expliquant ce qu'est un picoboo http://www.frightprops.com/controllers-electronics/frightideas-controllers/picoboo-controllers/picoboo.html
Voici mon code entrer dans l'arduino :
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 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 6 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[i]); //print each digit of the code
Serial.println(entered[i]); //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++){
Serial.print(entered[i], 128);
Serial.print(" - ");
}
if ((entered[0]==code[0]) && (entered[1]==code[1]) && (entered[2]==code[2]) && (entered[3]==code[3]) && (entered[4]==code[4]) && (entered[5]==code[5])){
Serial.println(".");
Serial.println("Hallelujah !!!");
digitalWrite(aimant, LOW); //turn the green LED on
pikoboo = LOW;
delay(10000);
digitalWrite(aimant, HIGH); // realimente l'aimant
}
else
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[i] = 0;
delay(50);
// digitalWrite(voyant, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(100); // wait for a second
//digitalWrite(voyant, LOW); // turn the LED off by making the voltage LOW
//delay(100);
}
}
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);
digitalWrite(pikoboo, LOW);//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(500);//wait, needed for correct functioning, otherwise
while (digitalRead(button1) == LOW) {delay(1000);};
}
else if (digitalRead(button2) == LOW){ //if button2 is pressed
Serial.println("Bouton2");
checkEntered1(2); //call checkEntered1 and pass it a 2
delay(500);//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(500);//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(500);//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(500);//wait, needed for correct functioning, otherwise
while (digitalRead(button5) == 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(500);//wait, needed for correct functioning, otherwise
while (digitalRead(button6) == LOW) {delay(250);};
}
}
Pouvez vous déterminer s'il s'agit d'une erreur dans mon code ou un problème du à un élément externe je vous prie ?
Je suis désolé pour ce post long mais je cherche à être le plus précis possible.
Je vous remercie.