Genius (Simon Game)
Autor: Fellipe Couto | Publicado: 2014-04-11T23:21:00-03:00




Quem não lembra desse clássico jogo da memória? O jogador precisa repetir a sequência exibida, e a cada acerto vai ficando mais difícil. Um bom passatempo e um ótimo exercício pra cabeça!

O jogo memoriza a pontuação máxima. Assim você pode verificar qual foi o seu recorde. Para isso, basta ligar o jogo com o botão do led verde pressionado. Isso fará com que o led pisque o número de vezes da pontuação recorde. Se quiser resetar a memória, mantenha o botão pressionado até o led parar de piscar. E quando um recorde de pontuação for batido, ao final do jogo o led verde piscará.
Então o objetivo é ver o led verde piscar! É recorde de pontuação batido!

Fiz o circuito utilizando a plataforma Arduino, e montei numa placa de fenolite de 8x7 cm.







Segue o esquema elétrico do circuito e o código fonte do microcontrolador.

Abraços!




/*-----------------------------
&nbsp;Programa : GENIUS (SIMON GAME)
&nbsp;Autor&nbsp;&nbsp;&nbsp; : Fellipe Couto
&nbsp;Data&nbsp;&nbsp;&nbsp;&nbsp; : 04/04/2014
&nbsp;-----------------------------*/

#include &lt;EEPROM.h&gt; //Biblioteca da memória EEPROM

long previousMillis = 0;
long interval = 1000; //Tempo do intervalo dentro do loop. 1000 milisegundos (1 segundo)
int timeControl = 0; //Controla o tempo de ociosidade
int led[4] = {12, 11, 10, 9}; //Pinos dos Leds
int but[4] = {5, 6, 7, 8}; //Pinos dos botões
int sequence[100]; //Array com a sequencia do jogo
int countGame = 0;
int countInput = 0;
int maxScore = EEPROM.read(0); //Retorna o valor do score armazenado na EEPROM
boolean play = true;

/*----- SETUP -----*/
void setup() {
&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp; pinMode(led[i], OUTPUT); //Configura os pinos como saída
&nbsp;&nbsp;&nbsp; pinMode(but[i], INPUT); //Configura os pinos como entrada
&nbsp; }
&nbsp; randomSeed(analogRead(0)); //Para o random não repetir a sequencia
&nbsp; score(); //Função para verificar e exibir a pontuação
&nbsp; start(); //Inicia o jogo
}

/*----- LOOP PRINCIPAL -----*/
void loop() {
&nbsp; unsigned long currentMillis = millis();

&nbsp; //Intervalo de 1 segundo
&nbsp; if (currentMillis - previousMillis &gt; interval) {
&nbsp;&nbsp;&nbsp; previousMillis = currentMillis;
&nbsp;&nbsp;&nbsp; timeControl++; //Incrementa a variável de controle de tempo
&nbsp;&nbsp;&nbsp; if (timeControl &gt;= 10) { //Ficando 10 segundos sem pressionar, inicia o jogo novamente
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start();
&nbsp;&nbsp;&nbsp; }
&nbsp; }

&nbsp; if (play == true) {
&nbsp;&nbsp;&nbsp; game();
&nbsp; }
&nbsp; else {
&nbsp;&nbsp;&nbsp; player();
&nbsp; }
}

/*----- SORTEIO DO JOGO -----*/
void game() {
&nbsp; countGame++;
&nbsp; sequence[countGame] = random(4); //de 0 a 3
&nbsp; for (int i = 1; i &lt;= countGame; i++) {
&nbsp;&nbsp;&nbsp; digitalWrite(led[sequence[i]], HIGH);
&nbsp;&nbsp;&nbsp; delay(400);
&nbsp;&nbsp;&nbsp; digitalWrite(led[sequence[i]], LOW);
&nbsp;&nbsp;&nbsp; delay(150);
&nbsp; }
&nbsp; play = false;
}

/*----- JOGADOR -----*/
void player() {
&nbsp; int input = 4;
&nbsp; int t = 300;

&nbsp; //Leitura dos botões
&nbsp; for (int b = 0; b &lt; 4; b++) {
&nbsp;&nbsp;&nbsp; if (digitalRead(but[b]) == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[b], HIGH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(t);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; input = b;
&nbsp;&nbsp;&nbsp; }
&nbsp; }

&nbsp; //Apaga todos os leds
&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp; digitalWrite(led[i], LOW);
&nbsp; }

&nbsp; if (input != 4) { //Se algum botão foi pressionado
&nbsp;&nbsp;&nbsp; timeControl = 0;
&nbsp;&nbsp;&nbsp; countInput++;
&nbsp;&nbsp;&nbsp; if (sequence[countInput] != input) { //Errou a sequencia
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(t);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blinkLeds(8, 150); //Pisca os leds indicando o erro
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; score(); //Verifica a pontuação
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(3000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start(); //Inicia o jogo novamente
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else if (countInput == countGame) { //Acertou a sequencia
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; countInput = 0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; play = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(t);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blinkLeds(1, 1000);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
}

/*----- SEQUENCIA INICIAL DE PISCAR DOS LEDS -----*/
void start() {
&nbsp; countGame = 0;
&nbsp; countInput = 0;
&nbsp; timeControl = 0;
&nbsp; play = true;

&nbsp; //Pisca os leds randomicamente
&nbsp; for (int i = 0; i &lt; 15; i++) {
&nbsp;&nbsp;&nbsp; int x = random(4);
&nbsp;&nbsp;&nbsp; digitalWrite(led[x], HIGH);
&nbsp;&nbsp;&nbsp; delay(20);
&nbsp;&nbsp;&nbsp; digitalWrite(led[x], LOW);
&nbsp;&nbsp;&nbsp; delay(100);
&nbsp; }

&nbsp; //Apaga e acende os leds em sequencia
&nbsp; for (int y = 0; y &lt; 3; y++) {
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[i], LOW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(80);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[i], HIGH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(80);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
&nbsp; delay(1000);

&nbsp; //Apaga todos os leds
&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp; digitalWrite(led[i], LOW);
&nbsp; }
&nbsp; delay(1000);
}

/*----- PISCA OS LEDS -----*/
void blinkLeds(int qtd, int t) {
&nbsp; //qtd = quantidade de piscadas / t = tempo
&nbsp; for (int y = 0; y &lt; qtd; y++) {
&nbsp;&nbsp;&nbsp; //Acende todos os leds
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[i], HIGH);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; delay(t);
&nbsp;&nbsp;&nbsp; //Apaga todos os leds
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 4; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[i], LOW);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; delay(t);
&nbsp; }
}

/*----- VERIFICA, EXIBE E GRAVA A PONTUAÇÃO -----*/
void score() {
&nbsp; //Zera a EEPROM caso esteja acima de 100.
&nbsp; if (maxScore &gt; 100) {
&nbsp;&nbsp;&nbsp; EEPROM.write(0, 0);
&nbsp;&nbsp;&nbsp; maxScore = 0;
&nbsp; }
&nbsp; //Verifica a pontuação
&nbsp; if (countGame - 1 &gt; maxScore) {
&nbsp;&nbsp;&nbsp; EEPROM.write(0, countGame - 1);
&nbsp;&nbsp;&nbsp; maxScore = countGame - 1;
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 100; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[0], HIGH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(50);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[0], LOW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(50);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
&nbsp; else if (digitalRead(but[0]) == 1) {
&nbsp;&nbsp;&nbsp; //Se o verde estiver pressionado, exibe a pontuação piscando o led verde
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; maxScore; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[0], HIGH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(500);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(led[0], LOW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(500);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; delay(1000);
&nbsp;&nbsp;&nbsp; if (digitalRead(but[0]) == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Após exibir a pontuação, efetua o reset dos pontos caso o botão verde estiver pressionado
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EEPROM.write(0, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxScore = 0;
&nbsp;&nbsp;&nbsp; }
&nbsp; }
}