Created
June 1, 2015 18:18
-
-
Save zxmarcos/7590e29e1e9693ba9627 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
| int numero_secreto = 0; | |
| int estado = 0; | |
| int contador = 0; | |
| int botao_anterior = 0; | |
| int numeros[10][7] = | |
| { | |
| // A B C D E F G | |
| { 1, 1, 1, 1, 1, 1, 0 }, // Num 0 | |
| { 0, 1, 1, 0, 0, 0, 0 }, // Num 1 | |
| { 1, 1, 0, 1, 1, 0, 1 }, // Num 2 | |
| { 1, 1, 1, 1, 0, 0, 1 }, // Num 3 | |
| { 0, 1, 1, 0, 0, 1, 1 }, // Num 4 | |
| { 1, 0, 1, 1, 0, 1, 1 }, // Num 5 | |
| { 1, 0, 1, 1, 1, 1, 1 }, // Num 6 | |
| { 1, 1, 1, 0, 0, 0, 0 }, // Num 7 | |
| { 1, 1, 1, 1, 1, 1, 1 }, // Num 8 | |
| { 1, 1, 1, 0, 0, 1, 1 }, // Num 9 | |
| }; | |
| int portas[7] = { | |
| // A B C D E F G | |
| 6,5,2,3,4,7,8 | |
| }; | |
| void exibir_numero(int num) | |
| { | |
| if (num < 10) { | |
| int i = 0; | |
| while (i < 7) { | |
| int porta = portas[i]; | |
| if (numeros[num][i] == 1) { | |
| // Acender o LED | |
| digitalWrite(porta, HIGH); | |
| } else { | |
| // Apagar o LED | |
| digitalWrite(porta, LOW); | |
| } | |
| i++; | |
| } | |
| } | |
| } | |
| void setup() { | |
| int i = 2; | |
| while (i < 9) { | |
| pinMode(i, OUTPUT); | |
| digitalWrite(i, HIGH); | |
| i++; | |
| } | |
| // | |
| pinMode(9, OUTPUT); | |
| pinMode(10, OUTPUT); | |
| digitalWrite(9, HIGH); | |
| digitalWrite(10, HIGH); | |
| pinMode(11, INPUT_PULLUP); | |
| estado = 1; | |
| Serial.begin(9600); | |
| } | |
| void estado1() | |
| { | |
| numero_secreto = random(0, 10); | |
| Serial.print("O número secreto é: "); | |
| Serial.println(numero_secreto); | |
| estado = 2; | |
| } | |
| void estado2() | |
| { | |
| int botao = digitalRead(11); | |
| // verifica se o botão foi pressionado | |
| if (botao_anterior == HIGH && botao == LOW) { | |
| // acertou | |
| if (contador == numero_secreto) { | |
| estado = 3; | |
| // errou | |
| } else { | |
| estado = 4; | |
| } | |
| } | |
| exibir_numero(contador); | |
| botao_anterior = botao; | |
| contador = (contador + 1) % 10; | |
| delay(100); | |
| } | |
| void estado3() | |
| { | |
| digitalWrite(10, LOW); | |
| for (int i = 0; i < 3; i++) { | |
| digitalWrite(9, HIGH); | |
| delay(100); | |
| digitalWrite(9, LOW); | |
| delay(100); | |
| } | |
| estado = 1; | |
| } | |
| void estado4() | |
| { | |
| digitalWrite(9, LOW); | |
| for (int i = 0; i < 3; i++) { | |
| digitalWrite(10, HIGH); | |
| delay(100); | |
| digitalWrite(10, LOW); | |
| delay(100); | |
| } | |
| estado = 2; | |
| } | |
| void loop() { | |
| switch (estado) { | |
| case 1: estado1(); break; | |
| case 2: estado2(); break; | |
| case 3: estado3(); break; | |
| case 4: estado4(); break; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment