Compare commits

...

1 Commits

Author SHA1 Message Date
scayac 9fbf03ffc7 Premier commit branche mode-memory 8 months ago
  1. 91
      src/main.cpp

91
src/main.cpp

@ -16,6 +16,9 @@ @@ -16,6 +16,9 @@
#define CLK_PIN 52
#define MAX_DEVICES 9
#define LONG_PRESS_DURATION 1000 // 1 second for long press
#define MEMORY_MODE_START_DELAY 1000 // Delay between memory sequence steps
//Définitions des pins pour les boutons et les LEDs
const int player1Buttons[NUM_BUTTONS] = {7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27};
const int player2Buttons[NUM_BUTTONS] = {45, 47, 49, 35, 37, 39, 41, 43, 29, 31, 33};
@ -40,6 +43,13 @@ unsigned long lastDebounceTime2[NUM_BUTTONS] = {0}; @@ -40,6 +43,13 @@ unsigned long lastDebounceTime2[NUM_BUTTONS] = {0};
bool testMode = false;
int gameMode = 1; // Default to mode 1
bool modeSelected = false;
int memorySequence[NUM_BUTTONS * 2] = {0};
int memorySequenceLength = 3;
int memorySequenceIndex = 0;
bool memoryInputActive = false;
void waitForStartButton() {
digitalWrite(startLEDPin, HIGH); // Turn on the start LED
while (digitalRead(startButtonPin) == HIGH) {
@ -211,6 +221,69 @@ void displayMotif(const boolean motif[],int size, int dt) { @@ -211,6 +221,69 @@ void displayMotif(const boolean motif[],int size, int dt) {
}
}
// Function to display the current mode on the timer display
void displayMode(int mode) {
myDisplay.displayReset();
String modeStr = "MOD" + String(mode);
myDisplay.displayZoneText(2, modeStr.c_str(), PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
myDisplay.displayAnimate();
}
// Function to generate a new memory sequence
void generateMemorySequence() {
for (int i = 0; i < memorySequenceLength; i++) {
memorySequence[i] = random(NUM_BUTTONS);
}
}
// Function to display the memory sequence
void displayMemorySequence() {
for (int i = 0; i < memorySequenceLength; i++) {
digitalWrite(player1LEDs[memorySequence[i]], HIGH);
delay(MEMORY_MODE_START_DELAY);
digitalWrite(player1LEDs[memorySequence[i]], LOW);
delay(MEMORY_MODE_START_DELAY);
}
memoryInputActive = true;
memorySequenceIndex = 0;
}
// Function to check player input in memory mode
void checkMemoryInput() {
for (int i = 0; i < NUM_BUTTONS; i++) {
if (digitalRead(player1Buttons[i]) == LOW) {
delay(DEFAULT_DEBOUNCE_DELAY); // Debounce delay
if (i == memorySequence[memorySequenceIndex]) {
memorySequenceIndex++;
if (memorySequenceIndex == memorySequenceLength) {
player1Score += memorySequenceLength;
memorySequenceLength++;
generateMemorySequence();
displayMemorySequence();
}
} else {
player1Score = max(0, player1Score - 1);
generateMemorySequence();
displayMemorySequence();
}
break;
}
}
}
// Function to handle mode selection
void selectGameMode() {
unsigned long pressStartTime = millis();
while (digitalRead(startButtonPin) == LOW) {
if (millis() - pressStartTime > LONG_PRESS_DURATION) {
modeSelected = true;
return;
}
}
gameMode = (gameMode == 1) ? 2 : 1;
displayMode(gameMode);
}
void setup() {
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(player1Buttons[i], INPUT_PULLUP);
@ -242,12 +315,23 @@ void setup() { @@ -242,12 +315,23 @@ void setup() {
if (digitalRead(startButtonPin) == LOW) {
testMode = true;
displayTestMode();
} else {
while (!modeSelected) {
if (digitalRead(startButtonPin) == LOW) {
selectGameMode();
}
}
if (gameMode == 2) {
generateMemorySequence();
displayMemorySequence();
} else {
displayMotif(motif1, 242, 150);
waitForStartButton();
startTime = millis();
initializeLEDs();
}
}
}
void loop() {
@ -256,12 +340,17 @@ void loop() { @@ -256,12 +340,17 @@ void loop() {
return;
}
if (gameMode == 1) {
unsigned long currentTime = millis();
if (currentTime - startTime >= GAME_DURATION) {
endGame();
return;
}
updateDisplays(currentTime);
checkButtons();
} else if (gameMode == 2) {
if (memoryInputActive) {
checkMemoryInput();
}
}
}
Loading…
Cancel
Save