Combiner 2 sketch

Bonjour a tous,

Je tente vainement de combiner 2 sketch qui chacun de leur coté fonctionne parfaitement, Voici donc A, B et AB...je pense que le stress vient du loop, mais je ne suis pas certain (je suis débutant et apprend de mes erreurs) le A est un jeux "simon" avec une ouverture de porte via un servo, le B est un lecteur MP3 avec un bouton play...(Je retire volontairement une partie pour pouvoir poster ici)

A:

/*
 simon-says
                     
 Simon tones from Wikipedia

*/
 
#include <Servo.h>
 
/*************************************************
* Public Constants
*************************************************/
 
// NOTE FREQUENCIES

 
// DURATION OF THE NOTES 

 
// CHECKS FOR BUTTON AND LIGHT POSITIONS

 
// DEFINE PIN LOCATIONS

 
// GAME PARAMETERS
#define ENTRY_TIME_LIMIT 3000 //Amount of time to press a button before game times out. 3000 ms = 3 sec
int ROUNDS_TO_WIN = 3; //Number of rounds to succeasfully remember before you win.
 
// GAME STATE
byte gameBoard[32]; //Contains the combination of buttons as we advance
byte gameRound = 0; //Counts the number of succesful rounds the player has made it through
 
Servo servo;
int pos = 0;
 
void setup() // Run once when power is connected
{    
  servo.attach(SERVOPIN);
  servo.write(0); // Degree position
 
  pinMode(BUTTON_RED, INPUT_PULLUP);
  pinMode(BUTTON_GREEN, INPUT_PULLUP);
  pinMode(BUTTON_BLUE, INPUT_PULLUP);
  pinMode(BUTTON_YELLOW, INPUT_PULLUP);
 
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
}
 
 
void loop()
{
  attractMode(); // Blink lights while waiting for user to press a button
 
  // Indicate the start of game play
  setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW); // Turn all LEDs on
  delay(1000);
  setLEDs(CHOICE_OFF); // Turn off LEDs
  delay(250);
 
  // Play memory game and handle result
  if (play_memory() == true) 
    play_winner(); // Player won, play winner tones
  else 
    play_loser(); // Player lost, play loser tones
 
}
 
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//THE FOLLOWING FUNCTIONS CONTROL GAME PLAY
 
// Play the memory game
// Returns 0 if player loses, or 1 if player wins
boolean play_memory(void)
{
  randomSeed(millis()); // Seed the random generator with random amount of millis()
  gameRound = 0; // Reset the game to the beginning
 
  while (gameRound < ROUNDS_TO_WIN) 
  {
    add_to_moves(); // Add a button to the current moves, then play them back
    playMoves(); // Play back the current game board
 
    // Then require the player to repeat the sequence.
    for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
    {
      byte choice = wait_for_button(); // See what button the user presses
      if (choice == 0) return false; // If wait timed out, player loses
      if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses
    }
 
    delay(1000); // Player was correct, delay before playing moves
  }
 
  return true; // Player made it through all the rounds to win!
}
 
 
// Plays the current contents of the game moves
void playMoves(void)
{
  for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++) 
  {
    toner(gameBoard[currentMove]);
 
    // Wait some amount of time between button playback
    // Shorten this to make game harder
    delay(150); // 150 works well. 75 gets fast.
  }
}
 
// Adds a new random button to the game sequence, by sampling the timer
void add_to_moves(void)
{
  byte newButton = random(0, 4); //min (included), max (exluded)
 
  // We have to convert this number, 0 to 3, to CHOICEs
  if(newButton == 0)      newButton = CHOICE_RED;
  else if(newButton == 1) newButton = CHOICE_GREEN;
  else if(newButton == 2) newButton = CHOICE_BLUE;
  else if(newButton == 3) newButton = CHOICE_YELLOW;
 
  gameBoard[gameRound++] = newButton; // Add this new button to the game array
}
 
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//THE FOLLOWING FUNCTIONS CONTROL THE HARDWARE
 
// Lights a given LEDs
// Pass in a byte that is made up from CHOICE_RED, CHOICE_YELLOW, etc
void setLEDs(byte leds)
{
  if ((leds & CHOICE_RED) != 0)
    digitalWrite(LED_RED, HIGH);
  else
    digitalWrite(LED_RED, LOW);
 
  if ((leds & CHOICE_GREEN) != 0)
    digitalWrite(LED_GREEN, HIGH);
  else
    digitalWrite(LED_GREEN, LOW);
 
  if ((leds & CHOICE_BLUE) != 0)
    digitalWrite(LED_BLUE, HIGH);
  else
    digitalWrite(LED_BLUE, LOW);
 
  if ((leds & CHOICE_YELLOW) != 0)
    digitalWrite(LED_YELLOW, HIGH);
  else
    digitalWrite(LED_YELLOW, LOW);
}
 
// Wait for a button to be pressed. 
// Returns one of LED colors (LED_RED, etc.) if successful, 0 if timed out
byte wait_for_button(void)
{
  long startTime = millis(); // Remember the time we started the this loop
 
  while ( (millis() - startTime) < ENTRY_TIME_LIMIT) // Loop until too much time has passed
  {
    byte button = checkButton();
 
    if (button != CHOICE_NONE)
    { 
      toner(button); // Play the button the user just pressed
      while(checkButton() != CHOICE_NONE) ;  // Now let's wait for user to release button
      delay(10); // This helps with debouncing and accidental double taps
      return button;
    }
 
  }
  return CHOICE_NONE; // If we get here, we've timed out!
}
 
// Returns a '1' bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.
byte checkButton(void)
{
  if (digitalRead(BUTTON_RED) == 0) return(CHOICE_RED); 
  else if (digitalRead(BUTTON_GREEN) == 0) return(CHOICE_GREEN); 
  else if (digitalRead(BUTTON_BLUE) == 0) return(CHOICE_BLUE); 
  else if (digitalRead(BUTTON_YELLOW) == 0) return(CHOICE_YELLOW);
 
  return(CHOICE_NONE); // If no button is pressed, return none
}
 
// Light an LED and play tone
// Red, upper left:     440Hz - A4
// Green, upper right:  880Hz - A5
// Blue, lower left:    587.33Hz - D5
// Yellow, lower right: 784Hz - G5
void toner(byte which)
{
  setLEDs(which); //Turn on a given LED
 
  //Play the sound associated with the given LED
  switch(which) 
  {
  case CHOICE_RED:
    play(LA4, Q); 
    break;
  case CHOICE_GREEN:
    play(LA5, Q); 
    break;
  case CHOICE_BLUE:
    play(D5, Q); 
    break;
  case CHOICE_YELLOW:
    play(G5, Q); 
    break;
  }
 
  setLEDs(CHOICE_OFF); // Turn off all LEDs
}
 
 
// Play the winner sound and lights
void play_winner(void)
{  
 
  winner_sound();
  
  //Move servo 90 degrees to pull on latch
  for(pos = 0; pos < 40; pos += 2)  
  {                                  
    servo.write(pos);              
    delay(10);                       
  }
  
// wait 1/2 second for door to open
  delay(500);                      
  
  // Move servo back to initial position
  for(pos = 40; pos>=1; pos-=2)     
  {                                
    servo.write(pos);             
    delay(10);
  }
  
  attractMode();  
}
 
 
// Play the winner sound
void winner_sound(void)
{
  play(F5,  W);
  play(E5,  Q);
  play(F5,  Q);
  play(E5,  Q);
  play(C5,  T);
  play(LA4, Q);
  play(D5,  H);
  play(LA4,  W); 
}
 
// Play the loser sound/lights
void play_loser(void)
{
  setLEDs(CHOICE_RED | CHOICE_GREEN);
  play(B3,Q);
 
  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
  play(B3,Q);
 
  setLEDs(CHOICE_RED | CHOICE_GREEN);
  play(B3,Q);
 
  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
  play(B3,Q);
}
 
// Show an "attract mode" display while waiting for user to press button.
void attractMode(void)
{
  while(1) 
  {
    setLEDs(CHOICE_RED);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
 
    setLEDs(CHOICE_BLUE);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
 
    setLEDs(CHOICE_GREEN);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
 
    setLEDs(CHOICE_YELLOW);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
  }
}
 
 
void play(long note, int duration) {
  tone(BUZZER,note,duration);
  delay(1+duration);
}

B:

#include <SoftwareSerial.h>
#define ARDUINO_RX A0//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX A1//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;

#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22

const int buttonPin1 = 2;
int buttonState1 = 0;



void setup() 
{
    mySerial.begin(9600);
          Serial.begin(9600);
      delay(500);//Wait chip initialization is complete
      sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card  
      delay(200);//wait for 200ms

     pinMode(buttonPin1, INPUT);
 
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);


  if (buttonState1 == HIGH) {
sendCommand(CMD_PLAY_W_VOL, 0x0f01);// volume 0x0f = 15%
  }
  

 
}

void sendCommand(int8_t command, int16_t dat)
{
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for(uint8_t i=0; i<8; i++)//
  {
    mySerial.write(Send_buf[i]) ;
  }
}

AB :

#include <Servo.h>
#include <SoftwareSerial.h>

// definition MP3

#define ARDUINO_RX A0//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX A1//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;

#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22

const int buttonPin1 = 2;
int buttonState1 = 0;
 
// NOTE FREQUENCIES
 
// DURATION OF THE NOTES 
 
// CHECKS FOR BUTTON AND LIGHT POSITIONS
 
// DEFINE PIN LOCATIONS
#define LED_RED       8
#define LED_GREEN     10
#define LED_BLUE      12
#define LED_YELLOW    6
#define BUTTON_RED    7
#define BUTTON_GREEN  9
#define BUTTON_BLUE   11
#define BUTTON_YELLOW 5
#define BUZZER        4
#define SERVOPIN      13
 
// GAME PARAMETERS
#define ENTRY_TIME_LIMIT 3000 
int ROUNDS_TO_WIN = 3; 
 
// GAME STATE
byte gameBoard[32]; 
byte gameRound = 0; 
 
Servo servo;
int pos = 0;
 
void setup() // Run once when power is connected
{    

mySerial.begin(9600); //SETUP MP3
          Serial.begin(9600);
      delay(500);//Wait chip initialization is complete
      sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card  
      delay(200);//wait for 200ms
     pinMode(buttonPin1, INPUT);

  servo.attach(SERVOPIN);
  servo.write(0); // Degree position

  pinMode(BUTTON_RED, INPUT_PULLUP);
  pinMode(BUTTON_GREEN, INPUT_PULLUP);
  pinMode(BUTTON_BLUE, INPUT_PULLUP);
  pinMode(BUTTON_YELLOW, INPUT_PULLUP);

  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);

}

void loop()
{
//MP3
buttonState1 = digitalRead(buttonPin1);

  if (buttonState1 == HIGH) {
sendCommand(CMD_PLAY_W_VOL, 0x0f01);// volume 0x0f = 15%
  }
  
  attractMode(); // Blink lights while waiting for user to press a button
 
  // Indicate the start of game play
  setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW); // Turn all LEDs on
  delay(1000);
  setLEDs(CHOICE_OFF); // Turn off LEDs
  delay(250);

  // Play memory game and handle result
  if (play_memory() == true) 
    play_winner(); // Player won, play winner tones
  else 
    play_loser(); // Player lost, play loser tones
}
void sendCommand(int8_t command, int16_t dat) //MP3
{
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for(uint8_t i=0; i<8; i++)//
  {
    mySerial.write(Send_buf[i]) ;
  }
} //finMP3

//THE FOLLOWING FUNCTIONS CONTROL GAME PLAY
 
// Play the memory game
// Returns 0 if player loses, or 1 if player wins
boolean play_memory(void)
{
  randomSeed(millis()); // Seed the random generator with random amount of millis()
  gameRound = 0; // Reset the game to the beginning
 
  while (gameRound < ROUNDS_TO_WIN) 
  {
    add_to_moves(); // Add a button to the current moves, then play them back
    playMoves(); // Play back the current game board
 
    // Then require the player to repeat the sequence.
    for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
    {
      byte choice = wait_for_button(); // See what button the user presses
      if (choice == 0) return false; // If wait timed out, player loses
      if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses
    }
 
    delay(1000); // Player was correct, delay before playing moves
  }
 
  return true; // Player made it through all the rounds to win!
}
 
 
// Plays the current contents of the game moves
void playMoves(void)
{
  for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++) 
  {
    toner(gameBoard[currentMove]);
 
    // Wait some amount of time between button playback
    // Shorten this to make game harder
    delay(150); // 150 works well. 75 gets fast.
  }
}
 
// Adds a new random button to the game sequence, by sampling the timer
void add_to_moves(void)
{
  byte newButton = random(0, 4); //min (included), max (exluded)
 
  // We have to convert this number, 0 to 3, to CHOICEs
  if(newButton == 0)      newButton = CHOICE_RED;
  else if(newButton == 1) newButton = CHOICE_GREEN;
  else if(newButton == 2) newButton = CHOICE_BLUE;
  else if(newButton == 3) newButton = CHOICE_YELLOW;
 
  gameBoard[gameRound++] = newButton; // Add this new button to the game array
}
 
//THE FOLLOWING FUNCTIONS CONTROL THE HARDWARE
 
// Lights a given LEDs
// Pass in a byte that is made up from CHOICE_RED, CHOICE_YELLOW, etc
void setLEDs(byte leds)
{
  if ((leds & CHOICE_RED) != 0)
    digitalWrite(LED_RED, HIGH);
  else
    digitalWrite(LED_RED, LOW);
 
  if ((leds & CHOICE_GREEN) != 0)
    digitalWrite(LED_GREEN, HIGH);
  else
    digitalWrite(LED_GREEN, LOW);
 
  if ((leds & CHOICE_BLUE) != 0)
    digitalWrite(LED_BLUE, HIGH);
  else
    digitalWrite(LED_BLUE, LOW);
 
  if ((leds & CHOICE_YELLOW) != 0)
    digitalWrite(LED_YELLOW, HIGH);
  else
    digitalWrite(LED_YELLOW, LOW);
}
 
// Wait for a button to be pressed. 
// Returns one of LED colors (LED_RED, etc.) if successful, 0 if timed out
byte wait_for_button(void)
{
  long startTime = millis(); // Remember the time we started the this loop
 
  while ( (millis() - startTime) < ENTRY_TIME_LIMIT) // Loop until too much time has passed
  {
    byte button = checkButton();
 
    if (button != CHOICE_NONE)
    { 
      toner(button); // Play the button the user just pressed
      while(checkButton() != CHOICE_NONE) ;  // Now let's wait for user to release button
      delay(10); // This helps with debouncing and accidental double taps
      return button;
    }
 
  }
  return CHOICE_NONE; // If we get here, we've timed out!
}
 
// Returns a '1' bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.
byte checkButton(void)
{
  if (digitalRead(BUTTON_RED) == 0) return(CHOICE_RED); 
  else if (digitalRead(BUTTON_GREEN) == 0) return(CHOICE_GREEN); 
  else if (digitalRead(BUTTON_BLUE) == 0) return(CHOICE_BLUE); 
  else if (digitalRead(BUTTON_YELLOW) == 0) return(CHOICE_YELLOW);
 
  return(CHOICE_NONE); // If no button is pressed, return none
}
 
// Light an LED and play tone
// Red, upper left:     440Hz - A4
// Green, upper right:  880Hz - A5
// Blue, lower left:    587.33Hz - D5
// Yellow, lower right: 784Hz - G5
void toner(byte which)
{
  setLEDs(which); //Turn on a given LED
 
  //Play the sound associated with the given LED
  switch(which) 
  {
  case CHOICE_RED:
    play(LA4, Q); 
    break;
  case CHOICE_GREEN:
    play(LA5, Q); 
    break;
  case CHOICE_BLUE:
    play(D5, Q); 
    break;
  case CHOICE_YELLOW:
    play(G5, Q); 
    break;
  }
 
  setLEDs(CHOICE_OFF); // Turn off all LEDs
}
 
 
// Play the winner sound and lights
void play_winner(void)
{  
 
  winner_sound();
  
  //Move servo 90 degrees to pull on latch
  for(pos = 0; pos < 40; pos += 2)  
  {                                  
    servo.write(pos);              
    delay(10);                       
  }
  
// wait 1/2 second for door to open
  delay(500);                      
  
  // Move servo back to initial position
  for(pos = 40; pos>=1; pos-=2)     
  {                                
    servo.write(pos);             
    delay(10);
  }
  
  attractMode();  
}
 
 
// Play the winner sound
// We are the Champions!
void winner_sound(void)
{
  play(F5,  W);
  play(E5,  Q);
  play(F5,  Q);
  play(E5,  Q);
  play(C5,  T);
  play(LA4, Q);
  play(D5,  H);
  play(LA4,  W); 
}
 
// Play the loser sound/lights
void play_loser(void)
{
  setLEDs(CHOICE_RED | CHOICE_GREEN);
  play(B3,Q);
 
  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
  play(B3,Q);
 
  setLEDs(CHOICE_RED | CHOICE_GREEN);
  play(B3,Q);
 
  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);
  play(B3,Q);
}
 
// Show an "attract mode" display while waiting for user to press button.
void attractMode(void)
{
  while(1) 
  {
    setLEDs(CHOICE_RED);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
 
    setLEDs(CHOICE_BLUE);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
 
    setLEDs(CHOICE_GREEN);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
 
    setLEDs(CHOICE_YELLOW);
    delay(100);
    if (checkButton() != CHOICE_NONE) return;
  }
}

 
void play(long note, int duration) {
  tone(BUZZER,note,duration);
  delay(1+duration);
}

Il faut en dire plus sur ce qui ne va pas,on ne va pas scruter 300 lignes de code pour chercher une erreur que tu n'indiques pas... As-tu seulement essayé de compiler AB ? Si oui, que se passe-t-il ? Peux-tu copier les éventuels messages d'erreur dans un nouveau post (en code) ?

oups en effet :confused:

quand je compile tout fonctionne bien je televese et le jeux Simon fonctionne normalement par contre pas le mp3. Quand je maintien le bouton après un reset cela démarre mais ce coupe directement. ce qui me dis que c'est dans loop.

j'avance à petits pas lol

voici le void loop ()

void loop()
{
  
  attractMode(); // Blink lights while waiting for user to press a button
 
  // Indicate the start of game play
  setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW); // Turn all LEDs on
  delay(1000);
  setLEDs(CHOICE_OFF); // Turn off LEDs
  delay(250);
 
  // Play memory game and handle result
  if (play_memory() == true) 
    play_winner(); // Player won, play winner tones
  else 
    play_loser(); // Player lost, play loser tones
    
  mp3bouton(); //MP3

}

Quand je laisse juste "mp3bouton();" le MP3 fonctionne...sinon, juste le jeu Simon ...

PS : mp3bouton(); :

void mp3bouton(void)
{
buttonState1 = digitalRead(buttonPin1);


  if (buttonState1 == HIGH) {
sendCommand(CMD_PLAY_W_VOL, 0x0f01);// volume 0x0f = 15%
  }
}

J'ai trouvé! j'ai simplement mis le " mp3bouton();" dans le "void attractMode(void)" et ça fait le job :wink: