Skip to content

Instantly share code, notes, and snippets.

@arantesxyz
Last active November 11, 2020 23:45
Show Gist options
  • Select an option

  • Save arantesxyz/a733a938985cc13509decb2d072ad1d0 to your computer and use it in GitHub Desktop.

Select an option

Save arantesxyz/a733a938985cc13509decb2d072ad1d0 to your computer and use it in GitHub Desktop.
Jogo arduíno
// Constantes
int nums[17][7] = {
{1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 1},
{0, 1, 0, 1, 1, 1, 1},
{0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 1, 1, 0},
{0, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 1, 0, 0},
{1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 0},
};
int displayPins[7] = {2, 3, 4, 5, 6, 7, 8};
int LEDPins[3] = {9, 10, 11};
// Cores do LED
int GREEN = 9,
BLUE = 10,
RED = 11;
// Periodo em milisegundos para atualizar o display
int UPDATE_NUMBER_PERIOD = 500;
// Funções
// Mostra o valor HEX no display.
void display(int value) {
for (int i=0; i<7; ++i) {
digitalWrite(displayPins[i], nums[value+1][i]);
}
}
// Configura todos os pinos de um array
void setupPins(int arr[], int size, int mode) {
for (int i=0; i<size; ++i) {
pinMode(arr[i], mode);
}
}
// Acende o LED com uma cor passada.
void LED(int color) {
for (int i=0; i<sizeof(LEDPins); ++i) {
int pin = LEDPins[i];
digitalWrite(pin, color == pin);
}
}
void blink(int period, int times, int LEDColor, int displayValue) {
for (int i=0; i<times; ++i) {
display(displayValue);
LED(LEDColor);
delay(period);
display(-1);
LED(0);
delay(period);
}
}
void setup() {
setupPins(displayPins, sizeof(displayPins), OUTPUT);
setupPins(LEDPins, sizeof(LEDPins), OUTPUT);
LED(RED);
}
int CURRENT_NUMBER = 0;
int LAST_TIME = millis();
long int guess = -1;
bool gameOver = false;
void loop(){
if (gameOver) {
blink(400, 3, GREEN, guess -1);
return;
}
if(guess == -1) {
// Vai sempre ser 7 por causa do tempo de execução
guess = random(15);
// Subtraímos 1 para mostrar para o usuário
// Pois como o código é sincrono e o display demora
// para atualizar o clique não é reconhecido
blink(400, 3, BLUE, guess -1);
LED(RED);
}
int buttonPress = digitalRead(13);
if(buttonPress == HIGH && CURRENT_NUMBER == guess) {
gameOver = true;
return;
}
int currentTime = millis();
if (currentTime > LAST_TIME + UPDATE_NUMBER_PERIOD) {
LAST_TIME = currentTime;
display(CURRENT_NUMBER++);
if (CURRENT_NUMBER > 15) {
CURRENT_NUMBER = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment