ENIB 2022 - groupe A : Memory Game : Différence entre versions

De Les Fabriques du Ponant
Aller à : navigation, rechercher
(Que fait ce projet ?)
(Le code)
 
(12 révisions intermédiaires par 2 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
==photo de l'équipe==
 
[[Fichier:Photoenib2018.jpg|600px]]
 
 
 
==Que fait ce projet ? ==
 
==Que fait ce projet ? ==
Ce projet est un Simon avec 4 leds et 4 boutons poussoirs.
+
Ce projet est un Simon avec 4 leds, 4 boutons poussoirs et du son.
 +
Le joueur doit reproduire la combinaison de led en appuyant sur des boutons.
 +
Il y a différents niveaux de difficulté.
 
[[Fichier:Prototype.png|vignette]]
 
[[Fichier:Prototype.png|vignette]]
  
==Liste des composants==
+
[[Fichier:Memory Game.jpg|vignette]]
 +
 
 +
==Liste des composants ==
 
*4 leds de différentes couleurs
 
*4 leds de différentes couleurs
 
*4 boutons poussoirs
 
*4 boutons poussoirs
 +
*4 resistances de 10 K ohm
 
*1 carte arduino uno
 
*1 carte arduino uno
 
*1 carte labdec
 
*1 carte labdec
 
*1 haut parleur piezo
 
*1 haut parleur piezo
 
*des câbles
 
*des câbles
 +
*1 boite de glace chocolat menthe
 +
 +
[[Fichier:Schéma de cablage.jpg|vignette]]
 +
==Le code==
  
 +
Code réalisé en grande partie par James Cameron, 2020
 +
https://www.youtube.com/watch?v=fB4h0hNWz2o&ab_channel=Print%27NPlay
 +
 +
<pre>
  
// Simple SIMON Code
 
// By James Cameron, 2020
 
//
 
// For Arduino UNO
 
//
 
 
// Pin Definitions :
 
// Pin Definitions :
// Pin 2 - LED 1 (-)
+
// Pin D1 - LED 1 (-)
// Pin 3 - LED 2 (-)
+
// Pin D2 - LED 2 (-)
// Pin 4 - LED 3 (-)
+
// Pin D3- LED 3 (-)
// Pin 5 - LED 4 (-)
+
// Pin D4 - LED 4 (-)
// Pin 6 - Button 1 (+)
+
// Pin D5 - Button 1 (+)
// Pin 7 - Button 2 (+)
+
// Pin D6 - Button 2 (+)
// Pin 8 - Button 3 (+)
+
// Pin D7 - Button 3 (+)
// Pin 9 - Button 4 (+)
+
// Pin D0 - Button 4 (+)
// Pin 11 - Speaker (+)
+
// Pin D8 - Speaker (+)
  
const int speaker = D1; // Set to the pin you connected the speaker to
+
const int speaker = D8; // défini la branche à laquelle vous connectez le haut parleur
  
 
//*****************************************
 
//*****************************************
  
int LENGTH = 400; // Length of time to play the main notes
+
int LENGTH = 400;  
  
int notes[4] = {100, 350, 600, 850}; // Values for the 4 notes used in the game
+
int notes[4] = {100, 350, 600, 850}; // Valeurs des 4 notes jouées
  
int gamepattern[20]; // Array to store game pattern in during play
+
int gamepattern[20];  
  
int difficulty = 1;
+
int difficulty = 1; // dificulté de base du jeu
  
 
//*****************************************
 
//*****************************************
Ligne 47 : Ligne 52 :
  
 
void setup() {
 
void setup() {
 +
  pinMode(D1, OUTPUT);
 
   pinMode(D2, OUTPUT); // Set up LED PINS
 
   pinMode(D2, OUTPUT); // Set up LED PINS
 
   pinMode(D3, OUTPUT);
 
   pinMode(D3, OUTPUT);
 
   pinMode(D4, OUTPUT);
 
   pinMode(D4, OUTPUT);
  pinMode(D1, OUTPUT);
+
  
 
   pinMode(D6, INPUT); // Set up button pins
 
   pinMode(D6, INPUT); // Set up button pins
Ligne 60 : Ligne 66 :
 
   
 
   
 
   }
 
   }
 +
 +
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 loop(){
+
void main_menu()
digitalRead(D6);
+
{
digitalRead(D7);
+
  digitalWrite(D1,LOW);
 +
  digitalWrite(D2,LOW);
 +
  digitalWrite(D3,LOW);
 +
  digitalWrite(D4,LOW);
  
if (digitalRead(D6) == LOW)
+
  digitalWrite(D7,HIGH);
 +
  digitalWrite(D6,HIGH);
 +
  digitalWrite(D5,HIGH);
 +
  digitalWrite(D0,HIGH);
 +
  while (1 == 1)
 +
  {
 +
    if (digitalRead(D6) == LOW)
 
     {
 
     {
       digitalWrite (D2,HIGH);
+
       difficulty = 1;
 +
      generate_game();
  
 +
      play_game();
 +
    }
 +
    if (digitalRead(D7) == LOW)
 +
    {
 +
      difficulty = 2;
 +
      generate_game();
 +
      play_game();
 
     }
 
     }
     else{    
+
     if (digitalRead(D5) == LOW)
       digitalWrite (D2,LOW);
+
    {
       }
+
       difficulty = 3;
     
+
      generate_game();
if (digitalRead(D7) == LOW)
+
       play_game();
 +
    }
 +
    if (digitalRead(D0) == LOW)
 
     {
 
     {
       digitalWrite (D3,HIGH);
+
       difficulty = 4;
 +
      generate_game();
 +
      play_game();
 
     }
 
     }
     else{    
+
  }
       digitalWrite (D3,LOW);
+
}
 +
 
 +
 
 +
 
 +
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);
 +
  }
 +
}
 +
   
  
if (digitalRead(D5) == LOW)
+
 
 +
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)  
 
     {
 
     {
       digitalWrite (D4,HIGH);
+
       Serial.println("Button 3 Pressed");
 +
      play_note(3, LENGTH);
 
     }
 
     }
     else{     
+
     if (digitalRead(D0) == LOW)
      digitalWrite (D4,LOW);
 
      }
 
 
 
      if (digitalRead(D0) == LOW)
 
 
     {
 
     {
       digitalWrite (D1,HIGH);
+
       Serial.println("Button 4 Pressed");
 +
      play_note(4, LENGTH);
 
     }
 
     }
    else{     
+
  }
      digitalWrite (D1,LOW);
 
      }
 
 
}
 
}
 +
</pre>
  
 
==Catégories==
 
==Catégories==
  
 
[[Catégorie:Enib2022]]
 
[[Catégorie:Enib2022]]

Version actuelle datée du 14 janvier 2022 à 17:04

Que fait ce projet ?

Ce projet est un Simon avec 4 leds, 4 boutons poussoirs et du son. Le joueur doit reproduire la combinaison de led en appuyant sur des boutons. Il y a différents niveaux de difficulté.

Prototype.png
Memory Game.jpg

Liste des composants

  • 4 leds de différentes couleurs
  • 4 boutons poussoirs
  • 4 resistances de 10 K ohm
  • 1 carte arduino uno
  • 1 carte labdec
  • 1 haut parleur piezo
  • des câbles
  • 1 boite de glace chocolat menthe
Schéma de cablage.jpg

Le code

Code réalisé en grande partie par James Cameron, 2020 https://www.youtube.com/watch?v=fB4h0hNWz2o&ab_channel=Print%27NPlay


// Pin Definitions :
// Pin D1 - LED 1 (-)
// Pin D2 - LED 2 (-)
// Pin D3- LED 3 (-)
// Pin D4 - LED 4 (-)
// Pin D5 - Button 1 (+)
// Pin D6 - Button 2 (+)
// Pin D7 - Button 3 (+)
// Pin D0 - Button 4 (+)
// Pin D8 - Speaker (+)

const int speaker = D8; // défini la branche à laquelle vous connectez le haut parleur

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

int LENGTH = 400; 

int notes[4] = {100, 350, 600, 850}; // Valeurs des 4 notes jouées

int gamepattern[20]; 

int difficulty = 1; // dificulté de base du jeu

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


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