ENIB 2022 - groupe A : Memory Game : Différence entre versions
(→Liste des composants et le code) |
(→Le code) |
||
| Ligne 19 : | Ligne 19 : | ||
==Le code== | ==Le code== | ||
| + | |||
| + | Code réalisé en grande partie par James Cameron, 2020 | ||
| + | // | ||
| + | / | ||
| + | // | ||
| + | // 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 (+) | ||
<pre> | <pre> | ||
| − | const int speaker = D8; // | + | const int speaker = D8; // défini la branche à laquelle vous connectez le haut parleur |
//***************************************** | //***************************************** | ||
| − | int LENGTH = 400; | + | int LENGTH = 400; |
| − | int notes[4] = {100, 350, 600, 850}; // | + | int notes[4] = {100, 350, 600, 850}; // Valeurs des 4 notes jouées |
| − | int gamepattern[20]; | + | int gamepattern[20]; |
| − | int difficulty = 1; | + | int difficulty = 1; // dificulté de base du jeu |
//***************************************** | //***************************************** | ||
Version du 14 janvier 2022 à 15:48
photo de l'équipe
Que fait ce projet ?
Ce projet est un Simon avec 4 leds et 4 boutons poussoirs.
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
Le code
Code réalisé en grande partie par James Cameron, 2020 // / // // 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);
}
}
}
