#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ===== SPRITES =====
// Maria Felipa (frame 1)
byte maria1[8] = {
B00100,B01110,B00100,B01110,
B10100,B00110,B01001,B10001
};
// Maria Felipa (frame 2)
byte maria2[8] = {
B00100,B01110,B00100,B01110,
B00101,B01100,B10010,B00011
};
// Caravela
byte caravela[8] = {
B00100,B00110,B00111,B11111,
B11111,B01110,B01110,B00000
};
// Canhao
byte canhao[8] = {
B00000,B00110,B01111,B11111,
B01110,B00100,B01110,B00000
};
// ===== PINOS =====
const int botaoUp = 32;
const int botaoDown = 33;
const int botaoA = 25;
const int botaoB = 26;
const int buzzer = 27;
// ===== CONTROLE =====
bool telaInicial = true;
bool frameMaria = false;
bool mostrarTexto = true;
unsigned long tAnim = 0;
unsigned long tMove = 0;
unsigned long tPisca = 0;
const int vel = 120;
int posObs = 19;
int fase = 0;
int posObsAnterior = 19;
int linhaObsAnterior = 1;
int linhaMariaAnterior = 2;
// ===== FUNCOES =====
void criarSprites() {
lcd.createChar(0, maria1);
lcd.createChar(1, maria2);
lcd.createChar(2, caravela);
lcd.createChar(3, canhao);
}
void desenharBase() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ECOS DA RESISTENCIA");
lcd.setCursor(3, 3);
lcd.print("A = INICIAR");
}
void desenharAnimacao() {
int linhaMaria;
int linhaObs;
if (fase == 0) {
linhaMaria = 2;
linhaObs = 1;
} else {
linhaMaria = 1;
linhaObs = 2;
}
// apaga posicoes antigas
lcd.setCursor(2, linhaMariaAnterior);
lcd.print(" ");
if (posObsAnterior >= 0 && posObsAnterior < 20) {
lcd.setCursor(posObsAnterior, linhaObsAnterior);
lcd.print(" ");
}
// desenha Maria
lcd.setCursor(2, linhaMaria);
lcd.write(frameMaria ? byte(0) : byte(1));
// desenha obstaculo
if (posObs >= 0 && posObs < 20) {
lcd.setCursor(posObs, linhaObs);
lcd.write(byte(2 + fase));
}
// atualiza historico
linhaMariaAnterior = linhaMaria;
posObsAnterior = posObs;
linhaObsAnterior = linhaObs;
}
void loopAnimacao() {
if (millis() - tAnim > vel) {
tAnim = millis();
frameMaria = !frameMaria;
}
if (millis() - tMove > vel) {
tMove = millis();
posObs--;
if (posObs < -1) {
posObs = 19;
fase = !fase;
}
}
if (millis() - tPisca > 500) {
tPisca = millis();
mostrarTexto = !mostrarTexto;
lcd.setCursor(3, 3);
if (mostrarTexto) {
lcd.print("A = INICIAR");
} else {
lcd.print(" ");
}
}
desenharAnimacao();
}
void iniciarJogo() {
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("JOGO INICIADO");
tone(buzzer, 1200, 120);
delay(150);
tone(buzzer, 1500, 120);
delay(500);
}
// ===== SETUP =====
void setup() {
pinMode(botaoUp, INPUT_PULLUP);
pinMode(botaoDown, INPUT_PULLUP);
pinMode(botaoA, INPUT_PULLUP);
pinMode(botaoB, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
lcd.init();
lcd.backlight();
criarSprites();
desenharBase();
}
// ===== LOOP =====
void loop() {
if (telaInicial) {
loopAnimacao();
if (digitalRead(botaoA) == LOW) {
delay(200);
telaInicial = false;
iniciarJogo();
}
}
}