Created
August 31, 2015 19:38
-
-
Save steppat/2fdd4dbea067dd4fe2b6 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 ledVerde 2 | |
#define ledAmarelo 3 | |
#define ledVermelho 4 | |
#define ledBranco 5 | |
#define botaoVerde 8 | |
#define botaoAmarelo 9 | |
#define botaoVermelho 10 | |
#define botaoBranco 11 | |
enum cores { | |
VERDE = 1, | |
AMARELO = 2, | |
VERMELHO = 3, | |
BRANCO = 4 | |
}; | |
int chave[TAMANHO_MAXIMO_DO_SEGREDO]; | |
int rodada = 1; | |
void setup() { | |
//inicia monitor serial | |
Serial.begin(9600); | |
iniciaPortas(); | |
iniciaJogo(); | |
} | |
void iniciaPortas(){ | |
pinMode(ledVerde, OUTPUT); | |
pinMode(ledAmarelo, OUTPUT); | |
pinMode(ledVermelho, OUTPUT); | |
pinMode(ledBranco, OUTPUT); | |
//inicia botoes | |
pinMode(botaoVerde, INPUT_PULLUP); | |
pinMode(botaoAmarelo, INPUT_PULLUP); | |
pinMode(botaoVermelho, INPUT_PULLUP); | |
pinMode(botaoBranco, INPUT_PULLUP); | |
} | |
void iniciaJogo() { | |
// TODO nao precisa desse comentario no codigo pra valer certo? | |
//inicia gerador de numeros pseudo-aleatorios para que cada jogo seja diferente | |
//a porta analogica esta em estado indefinido ja que e um circuito aberto logo a voltagem do ambiente sera lida e convertida para um numero de 0 ate 1023 | |
randomSeed(analogRead(0)); | |
geraSequencia(); | |
} | |
enum estadosDoJogo { | |
JOGO_EM_ANDAMENTO, | |
JOGO_FINALIZANDO, | |
JOGO_COM_ERRO | |
}; | |
int estadoDoJogo() { | |
if(rodada <= TAMANHO_MAXIMO_DO_SEGREDO) { | |
return JOGO_EM_ANDAMENTO; | |
} | |
if(rodada == TAMANHO_MAXIMO_DO_SEGREDO + 1) { | |
return JOGO_FINALIZANDO; | |
} | |
return JOGO_COM_ERRO; | |
} | |
void novaRodada() { | |
if(validaInputDoUsuario()){ | |
rodada++; | |
if(estadoDoJogo() == JOGO_FINALIZANDO) { | |
finalizadoComSucesso(); | |
} else { | |
piscaLedsDaRodada(); | |
} | |
} | |
} | |
void loop() { | |
if(estadoDoJogo() == JOGO_EM_ANDAMENTO){ | |
novaRodada(); | |
} | |
} | |
/* | |
* GERACAO DE SEQUENCIA DE SEGREDO | |
*/ | |
int geraCor() { | |
int cor = random(1, 5); | |
return cor; | |
} | |
void geraSequencia(){ | |
Serial.println("Gerando Sequencia..."); | |
for (int i = 0; i < TAMANHO_MAXIMO_DO_SEGREDO; i++) { | |
chave[i] = geraCor(); | |
Serial.print("posicao ["); | |
Serial.print(i); | |
Serial.print("]: "); | |
Serial.println(chave[i]); | |
} | |
Serial.println(); | |
piscaLedsDaRodada(); | |
} | |
int validaInputDoUsuario(){ | |
int resposta = checaBotaoPressionado(); | |
//Serial.println("Usuario apertou botao: "); | |
//Serial.println(resposta); | |
if(resposta == 0) return 0; | |
if(chave[rodada - 1] == resposta){ | |
Serial.println("Resposta Certa!"); | |
return 1; | |
}else{ | |
Serial.println("Resposta Errada!"); | |
Serial.println(rodada); | |
Serial.println(chave[rodada]); | |
Serial.println(resposta); | |
return 0; | |
} | |
} | |
int JOGADOR_NAO_RESPONDEU = 0; | |
// TODO ainda nao sou fa desse e dos outros codigos onde temos quatro ifs, mas atacamos ele depois dessa priemira batida | |
int checaBotaoPressionado(){ | |
// TODO se em arduino nao precisa declarar tudo antes, declarar cada uma dessas variaveis somente a medida do necessario. nao sei dizer isso pq em compilador C moderno nao precisa, isso? ansi precisa? nao sei dizer, so conferir. | |
int estadoVerde = digitalRead(botaoVerde); | |
int estadoAmarelo = digitalRead(botaoAmarelo); | |
int estadoVermelho = digitalRead(botaoVermelho); | |
int estadoBranco = digitalRead(botaoBranco); | |
if(estadoVerde == LOW){ | |
return piscaVerde(); | |
} | |
if(estadoAmarelo == LOW){ | |
return piscaAmarelo(); | |
} | |
if(estadoVermelho == LOW){ | |
return piscaVermelho(); | |
} | |
if(estadoBranco == LOW){ | |
return piscaBranco(); | |
} | |
return JOGADOR_NAO_RESPONDEU; | |
} | |
int piscaVerde(){ | |
piscaLed(ledVerde); | |
return VERDE; | |
} | |
int piscaAmarelo(){ | |
piscaLed(ledAmarelo); | |
return AMARELO; | |
} | |
int piscaVermelho(){ | |
piscaLed(ledVermelho); | |
return VERMELHO; | |
} | |
int piscaBranco(){ | |
piscaLed(ledBranco); | |
return BRANCO; | |
} | |
void piscaLed(int porta){ | |
digitalWrite(porta,HIGH); | |
delay(TEMPO_LUZ); | |
digitalWrite(porta,LOW); | |
delay(TEMPO_PAUSA); | |
} | |
/* | |
* OUTPUT DE LUZES | |
*/ | |
void piscaLedsDaRodada(){ | |
Serial.print("Tocando sequencia para a rodada: "); | |
Serial.println(rodada); | |
for (int i = 0; i < rodada; i++) { | |
Serial.print(chave[i]); | |
if(chave[i] == VERDE){ | |
piscaVerde(); | |
} | |
if(chave[i] == AMARELO){ | |
piscaAmarelo(); | |
} | |
if(chave[i] == VERMELHO){ | |
piscaVermelho(); | |
} | |
if(chave[i] == BRANCO){ | |
piscaBranco(); | |
} | |
} | |
Serial.println(); | |
Serial.println(); | |
} | |
void finalizadoComSucesso(){ | |
Serial.println("Jogo finalizado com sucesso."); | |
for(int i = 0; i < REPETICOES_ESTADO_FINAL; i++){ | |
piscaLed(ledVerde); | |
piscaLed(ledAmarelo); | |
piscaLed(ledVermelho); | |
piscaLed(ledBranco); | |
} | |
} | |
void finalizadoComFalha(){ | |
Serial.println("Jogo finalizado com falha."); | |
for(int i = 0; i < REPETICOES_ESTADO_FINAL; i++){ | |
digitalWrite(ledVerde,HIGH); | |
digitalWrite(ledAmarelo,HIGH); | |
digitalWrite(ledVermelho,HIGH); | |
digitalWrite(ledBranco,HIGH); | |
delay(TEMPO_PAUSA); | |
digitalWrite(ledVerde,LOW); | |
digitalWrite(ledAmarelo,LOW); | |
digitalWrite(ledVermelho,LOW); | |
digitalWrite(ledBranco,LOW); | |
delay(TEMPO_PAUSA); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment