Last active
September 1, 2015 13:34
-
-
Save Gunisalvo/ca79bc21fa2aaa152625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define TAMANHO_MAXIMO_DO_SEGREDO 4 | |
#define REPETICOES_ESTADO_FINAL 3 | |
#define TEMPO_LUZ 1000 | |
#define TEMPO_PAUSA 500 | |
#define BOTAO_VERDE 8 | |
#define BOTAO_AMARELO 9 | |
#define BOTAO_VERMELHO 10 | |
#define BOTAO_AZUL 11 | |
enum leds { | |
INDEFINIDO = 0, | |
VERDE = 2, | |
AMARELO = 3, | |
VERMELHO = 4, | |
AZUL = 5 | |
}; | |
enum estadosDoJogo { | |
PRONTO_PARA_PROXIMA_RODADA, | |
USUARIO_RESPONDENDO, | |
USUARIO_SEGUIU_O_MESTRE, | |
USUARIO_NAO_SEGUIU_O_MESTRE | |
}; | |
int sequencia[TAMANHO_MAXIMO_DO_SEGREDO]; | |
int rodada = 0; | |
int passosRespondidos = 0; | |
void setup() { | |
Serial.begin(9600); | |
iniciaPortas(); | |
iniciaJogo(); | |
} | |
void iniciaPortas(){ | |
//inicia LEDs | |
pinMode(VERDE, OUTPUT); | |
pinMode(AMARELO, OUTPUT); | |
pinMode(VERMELHO, OUTPUT); | |
pinMode(AZUL, OUTPUT); | |
//inicia botoes | |
pinMode(BOTAO_VERDE, INPUT_PULLUP); | |
pinMode(BOTAO_AMARELO, INPUT_PULLUP); | |
pinMode(BOTAO_VERMELHO, INPUT_PULLUP); | |
pinMode(BOTAO_AZUL, INPUT_PULLUP); | |
} | |
void iniciaJogo() { | |
int jogo = analogRead(0); | |
Serial.print("Jogo Selecionado: "); | |
Serial.println(jogo); | |
randomSeed(jogo); | |
geraSequencia(); | |
} | |
void geraSequencia(){ | |
Serial.println("Gerando Sequencia..."); | |
for (int i = 0; i < TAMANHO_MAXIMO_DO_SEGREDO; i++) { | |
sequencia[i] = geraCor(); | |
Serial.print("posicao ["); | |
Serial.print(i); | |
Serial.print("]: "); | |
Serial.println(sequencia[i]); | |
} | |
Serial.println(); | |
} | |
int geraCor() { | |
int cor = random(VERDE, AZUL + 1); | |
return cor; | |
} | |
void loop() { | |
switch(estadoDoJogo()){ | |
case PRONTO_PARA_PROXIMA_RODADA: | |
novaRodada(); | |
break; | |
case USUARIO_RESPONDENDO: | |
processaRespostaUsuario(); | |
break; | |
case USUARIO_SEGUIU_O_MESTRE: | |
finalizadoComSucesso(); | |
break; | |
case USUARIO_NAO_SEGUIU_O_MESTRE: | |
finalizadoComFalha(); | |
break; | |
} | |
} | |
int estadoDoJogo() { | |
if(rodada <= TAMANHO_MAXIMO_DO_SEGREDO) { | |
if(rodada == passosRespondidos){ | |
return PRONTO_PARA_PROXIMA_RODADA; | |
}else{ | |
return USUARIO_RESPONDENDO; | |
} | |
} | |
if(rodada == TAMANHO_MAXIMO_DO_SEGREDO + 1) { | |
return USUARIO_SEGUIU_O_MESTRE; | |
} | |
return USUARIO_NAO_SEGUIU_O_MESTRE; | |
} | |
void novaRodada() { | |
rodada++; | |
if(rodada <= TAMANHO_MAXIMO_DO_SEGREDO){ | |
Serial.print("Iniciando rodada: "); | |
Serial.println(rodada); | |
passosRespondidos = 0; | |
piscaLedsDaRodada(); | |
}else{ | |
Serial.println("Todas as rodadas respondidas."); | |
Serial.println("Jogo finalizado com sucesso."); | |
} | |
} | |
void processaRespostaUsuario(){ | |
int resposta = checaBotaoPressionado(); | |
if(resposta == INDEFINIDO){ | |
//Aguardando resposta do usuario... | |
return; | |
} | |
if(sequencia[passosRespondidos] == resposta){ | |
Serial.println("Resposta Certa!"); | |
passosRespondidos++; | |
}else{ | |
Serial.println("Resposta Errada!"); | |
Serial.println("Jogo finalizado com falha."); | |
rodada = TAMANHO_MAXIMO_DO_SEGREDO + 2; | |
} | |
} | |
int checaBotaoPressionado(){ | |
if(digitalRead(BOTAO_VERDE) == LOW){ | |
return piscaLed(VERDE); | |
} | |
if(digitalRead(BOTAO_AMARELO) == LOW){ | |
return piscaLed(AMARELO); | |
} | |
if(digitalRead(BOTAO_VERMELHO) == LOW){ | |
return piscaLed(VERMELHO); | |
} | |
if(digitalRead(BOTAO_AZUL) == LOW){ | |
return piscaLed(AZUL); | |
} | |
return INDEFINIDO; | |
} | |
int piscaLed(int porta){ | |
digitalWrite(porta,HIGH); | |
delay(TEMPO_LUZ); | |
digitalWrite(porta,LOW); | |
delay(TEMPO_PAUSA); | |
return porta; | |
} | |
void piscaLedsDaRodada(){ | |
Serial.print("Tocando sequencia para a rodada ["); | |
Serial.print(rodada); | |
Serial.print("]: "); | |
for (int i = 0; i < rodada; i++) { | |
Serial.print(sequencia[i]); | |
piscaLed(sequencia[i]); | |
Serial.print(" "); | |
} | |
Serial.println(); | |
} | |
void finalizadoComSucesso(){ | |
piscaLed(VERDE); | |
piscaLed(AMARELO); | |
piscaLed(VERMELHO); | |
piscaLed(AZUL); | |
} | |
void finalizadoComFalha(){ | |
digitalWrite(VERDE,HIGH); | |
digitalWrite(AMARELO,HIGH); | |
digitalWrite(VERMELHO,HIGH); | |
digitalWrite(AZUL,HIGH); | |
delay(TEMPO_PAUSA); | |
digitalWrite(VERDE,LOW); | |
digitalWrite(AMARELO,LOW); | |
digitalWrite(VERMELHO,LOW); | |
digitalWrite(AZUL,LOW); | |
delay(TEMPO_PAUSA); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment