ENIB 2022 - groupe A : Memory Game

De Les Fabriques du Ponant
Révision datée du 14 janvier 2022 à 15:16 par Titouan (discussion | contributions) (Liste des composants)
Aller à : navigation, rechercher

photo de l'équipe

Photoenib2018.jpg

Que fait ce projet ?

Ce projet est un Simon avec 4 leds et 4 boutons poussoirs.

Prototype.png

Liste des composants

  • 4 leds de différentes couleurs
  • 4 boutons poussoirs
  • 1 carte arduino uno
  • 1 carte labdec
  • 1 haut parleur piezo
  • des câbles
  • 1 boite de glace chocolat menthe


Le code :

const int speaker = D8; // Set to the pin you connected the speaker to

//*****************************************

int LENGTH = 400; // Length of time to play the main notes

int notes[4] = {100, 350, 600, 850}; // Values for the 4 notes used in the game

int gamepattern[20]; // Array to store game pattern in during play

int difficulty = 1;

//*****************************************


void setup() {

 pinMode(D1, OUTPUT);
 pinMode(D2, OUTPUT); // Set up LED PINS
 pinMode(D3, OUTPUT);
 pinMode(D4, OUTPUT);

 pinMode(D6, INPUT); // Set up button pins
 pinMode(D7, INPUT);
 pinMode(D5, INPUT);
 pinMode(D0, INPUT);
 
 Serial.begin(9600); // Enable serial output so we can debug using the serial reader in the Arduino IDE

 }

void loop() {

 //setPins(); // Set the LED and Button pins HIGH
 
 //generate_game(); // Was used for testing a single-game mode before main menu was implimented
 //testButtons(); // Used to test buttons without playing the game
 main_menu();

}

void main_menu() {

 digitalWrite(D1,LOW);
 digitalWrite(D2,LOW);
 digitalWrite(D3,LOW);
 digitalWrite(D4,LOW);
 digitalWrite(D7,HIGH);
 digitalWrite(D6,HIGH);
 digitalWrite(D5,HIGH);
 digitalWrite(D0,HIGH);
 while (1 == 1)
 {
   if (digitalRead(D6) == LOW)
   {
     difficulty = 1;
     generate_game();
     play_game();
   }
   if (digitalRead(D7) == LOW)
   {
     difficulty = 2;
     generate_game();
     play_game();
   }
   if (digitalRead(D5) == LOW)
   {
     difficulty = 3;
     generate_game();
     play_game();
   }
   if (digitalRead(D0) == LOW)
   {
     difficulty = 4;
     generate_game();
     play_game();
   }
 }

}


void play_game() {

 int roundCount = 0;
 int playerTurn = 1;
 bool buttonPress = false;
 int currentNote;
 int userInput = 0;
 bool loss = false;
 play_note(1, 100);
 play_note(2, 100);
 play_note(3, 100);
 play_note(4, 100);
 delay(1000);
 for (int currentRound=1; (currentRound - 1)<=(difficulty * 5); currentRound++) // Number of rounds to play
   {
     roundCount += 1;
     playerTurn = 1;
     buttonPress = false;
     userInput = 0;
     for (int j = 1; j != currentRound; j++)
     {
       play_note(gamepattern[j - 1], LENGTH); // Play current round note(s)
     }
     while (playerTurn < currentRound) {
       currentNote = gamepattern[playerTurn - 1];
       Serial.println(currentNote);
       while (buttonPress == false) {
          if (digitalRead(D6) == LOW) // Button 1 Pressed
          {
            buttonPress = true;
            userInput = 1;
          }
          if (digitalRead(D7) == LOW) // Button 2 Pressed
          {
            buttonPress = true;
            
            userInput = 2;
          }
          if (digitalRead(D5) == LOW) // Button 3 Pressed
          {
            buttonPress = true;
            userInput = 3;
          }
          if (digitalRead(D0) == LOW) // Button 4 Pressed
          {
            buttonPress = true;
            userInput = 4;
          }
          if (buttonPress == true) // A button was Pressed
          {
            play_note(userInput, LENGTH); // Play the note pushed
            if (currentNote == userInput) // You pushed the right one!
            {
              playerTurn++;
            }
            else // You pushed the wrong one! :(
            {
              game_over(false);
            }
         }
           
   }
   buttonPress = false;
  }
 }
 if (loss == false){
   Serial.println("You Win!");
   game_over(true);
 }
}
   


void generate_game() {

 randomSeed(analogRead(D8));
 for (int i=0; i<(difficulty * 5); i++) // For each level of difficulty, add 5 more turns to the game
 {
   gamepattern[i] = random(1, 5);
 }

}

void play_note(int index, int notespeed) {

 digitalWrite(index + 1, HIGH);
 tone(speaker, notes[ index - 1 ], notespeed);
 if (index==2){
     digitalWrite(D3,HIGH);
 }
 delay(notespeed * 2);
 digitalWrite(index + 1, LOW);
   if (index==2){
     digitalWrite(D3,LOW                         );
 }

}

void game_over(bool win) {

 if (win) {
   Serial.println("You Win!");
    for (int i = 0; i < 10; i++){
     play_note(1, 50);
     play_note(2, 50);
     play_note(3, 50);
     play_note(4, 50);
   }
 }
 
 else {
   Serial.println("You Lose!");
   for (int i = 0; i < 6; i++){
     play_note(4, 100);
     play_note(3, 100);
     play_note(2, 100);
     play_note(1, 100);
   }
 }
 Serial.println("Game over");
 main_menu();

}

void testButtons() // Created this function to test buttons without having to run the entire game {

 while (1 == 1){
   if (digitalRead(D6) == LOW)
   {
     Serial.println("Button 1 Pressed");
     play_note(1, LENGTH);
   }
   if (digitalRead(D7) == LOW)
   {
     Serial.println("Button 2 Pressed");
     play_note(2, LENGTH);
   }
   if (digitalRead(D5) == LOW) 
   {
     Serial.println("Button 3 Pressed");
     play_note(3, LENGTH);
   }
   if (digitalRead(D0) == LOW)
   {
     Serial.println("Button 4 Pressed");
     play_note(4, LENGTH);
   }
 }

}

Catégories