1
resposta

Curso Arduino: Do zero ao jogo

Boa tarde galera, não estou conseguindo acompanhar a escrita do código fonte do jogo genius do arduino , alguém tem o código fonte pra postar aqui?

1 resposta

Fala aí Adilson

Vou te enviar o código fonte mais abaixo, mas tem algum problema para você não estar conseguindo acompanhar a escrita do código fonte?

Segue código fonte :

//Digital port's
const int kLedGreen      = 2;
const int kLedYellow     = 3;
const int kLedBlue       = 4;
const int kLedRed        = 5;
const int kButtonGreen   = 6;
const int kButtonYellow  = 7;
const int kButtonBlue    = 8;
const int kButtonRed     = 9;

//Analog port's
const int kPotentiometer = 0;


#define UNDEFINED             -1
#define LENGTH_ARRAY_PORTS    4
#define LENGTH_ARRAY_SEQUENCE 4
#define ONE_SECOND            1000
#define HALF_SECOND           ( ONE_SECOND / 2 )
#define QUARTER_SECOND        ( HALF_SECOND / 2 )
#define EIGHTH_SECOND         ( QUARTER_SECOND / 2 )

int portsOutput[] = {kLedGreen, kLedYellow, kLedBlue, kLedRed};
int portsInput[] = {kButtonGreen, kButtonYellow, kButtonBlue, kButtonRed};

int sequence[LENGTH_ARRAY_SEQUENCE];
int answer[LENGTH_ARRAY_SEQUENCE];

int roundGame;
bool stopGame = false;


void setup() {
  Serial.begin(9600);

  int seed = analogRead( kPotentiometer );
  randomSeed( seed );

  Serial.println( seed );

  initPorts(portsOutput, OUTPUT);
  initPorts(portsInput, INPUT_PULLUP);

  raffleSequence();

  blikToInitGame();

}

void loop() {

  if ( !gameOver() ) {
    playGame();
  }
}



void playGame(){
      flashingSequence();    
    getAnswerOfPlayer();    
    delay(ONE_SECOND);
}

/*
 * Check the game over
*/
bool gameOver() {    

    if (!stopGame){
      bool right = true;

      for( int i = 0; i < roundGame; ++i){        

        if ( answer[i] > 0 && answer[i] != sequence[i] ){
          right = false;
          break;
        }  

      }

      if( !right ){
        loser();
        stopGame = true;
      }else if( roundGame >= LENGTH_ARRAY_SEQUENCE ){
        winner();
        stopGame = true;
      }

      roundGame++;
    } 

    return stopGame;

}


/*
 * Blink loser game
*/
void loser(){

  Serial.println( "\n\n\n L  O S E R \n\n\n");

  int forSeconds = ONE_SECOND * 4;

  digitalWrite(kLedGreen,HIGH);
  digitalWrite(kLedYellow,HIGH);
  digitalWrite(kLedBlue,HIGH);
  digitalWrite(kLedRed,HIGH);


  delay( forSeconds );


  digitalWrite(kLedGreen,LOW);
  digitalWrite(kLedYellow,LOW);
  digitalWrite(kLedBlue,LOW);
  digitalWrite(kLedRed,LOW);
}

/*
 * Blink winner game
*/
void winner(){

  Serial.println( "\n\n\n W  I N N E R \n\n\n");

  for( int i = 0; i < 4; ++i){

    bool asc    = i%2 ==   0;
    int  first  = asc  ?   0                   : LENGTH_ARRAY_PORTS-1;
    int  last   = asc  ?   LENGTH_ARRAY_PORTS  : -1;


    for( int j = first; ( asc ? j < last : j > last); ( asc ? ++j : --j) ){
      flashingLedDelay(portsOutput[j]);

      if( j == last-1 || j == last+1){
        delay(EIGHTH_SECOND);
      }

    }

  }


  for( int i = 0 ; i < 3; ++i){
    digitalWrite(kLedGreen,HIGH);
    digitalWrite(kLedYellow,HIGH);
    digitalWrite(kLedBlue,HIGH);
    digitalWrite(kLedRed,HIGH);


    delay( EIGHTH_SECOND );


    digitalWrite(kLedGreen,LOW);
    digitalWrite(kLedYellow,LOW);
    digitalWrite(kLedBlue,LOW);
    digitalWrite(kLedRed,LOW);

    delay( EIGHTH_SECOND ); 
  }


}

/*
 * Get Player interactive
*/
void getAnswerOfPlayer(){  

  clearAnswer();

  for( int i = 0; i < roundGame; ++i){

      while( !(answer[i] > 0) ){
        answer[i] = checkPlayerChoice();        
      }

      if ( answer[i] != sequence[i] ){
        break;
      }

  }
}


/*
 * This function, raffle random sequence of game
*/
void raffleSequence(){

  roundGame = 0;  

  for( int i = 0 ; i < LENGTH_ARRAY_SEQUENCE; ++i){
    sequence[i] = random(kLedGreen, kLedRed + 1); // 2-5 (6);
  }

}



/*
 * Helper function to get choice of player pressed button.
*/
int checkPlayerChoice(){
  if( digitalRead(kButtonGreen) == LOW ){
    return flashingLedDelay(kLedGreen);
  }

  if( digitalRead(kButtonYellow) == LOW ){
    return flashingLedDelay(kLedYellow);
  }

  if( digitalRead(kButtonBlue) == LOW ){
    return flashingLedDelay(kLedBlue);
  }

  if( digitalRead(kButtonRed) == LOW ){
    return flashingLedDelay(kLedRed);
  }

  return UNDEFINED;

}


/*
 * This function, run sequence of led's
*/
void flashingSequence(){

  delay( EIGHTH_SECOND );

  for(int led = 0; led < roundGame ; ++led){
        flashingLedDelay( sequence[led] );
        delay( QUARTER_SECOND );

  }

}


/*
 * Helper Function to bind port's with ioType
*/
void initPorts( int ports[], int ioType ){

  for(int port = 0; port < LENGTH_ARRAY_PORTS ; ++port){
    pinMode( ports[port], ioType);
  }

}


/*
 * this function, turn on an led with delay between led turn on and off
*/
int flashingLedDelay( int ledPort ){
  digitalWrite(ledPort,HIGH);
  delay( ONE_SECOND );
  digitalWrite(ledPort,LOW);
//  delay( HALF_SECOND );
  return ledPort;
}




/*
  * This function used on start new game
*/
void blikToInitGame(){

  for( int i = 0; i < 3; ++i){

    digitalWrite(kLedGreen,HIGH);
    digitalWrite(kLedYellow,HIGH);
    digitalWrite(kLedBlue,HIGH);
    digitalWrite(kLedRed,HIGH);


    delay( QUARTER_SECOND );


    digitalWrite(kLedGreen,LOW);
    digitalWrite(kLedYellow,LOW);
    digitalWrite(kLedBlue,LOW);
    digitalWrite(kLedRed,LOW);

    delay( QUARTER_SECOND );
  }


  for( int i = 0 ; i < 3; ++i){
    digitalWrite(kLedGreen,HIGH);
    digitalWrite(kLedYellow,HIGH);
    digitalWrite(kLedBlue,HIGH);
    digitalWrite(kLedRed,HIGH);


    delay( EIGHTH_SECOND );


    digitalWrite(kLedGreen,LOW);
    digitalWrite(kLedYellow,LOW);
    digitalWrite(kLedBlue,LOW);
    digitalWrite(kLedRed,LOW);

    delay( EIGHTH_SECOND ); 
  }

}



void clearAnswer(){

  for( int i = 0; i < LENGTH_ARRAY_SEQUENCE; ++i){
    answer[i] = 0;
  }

}