#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define i2c_Address 0x3C
#define BUZZER_PIN 9
#define BOTON_A 4
#define BOTON_B 3
#define BOTON_C 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// --- BITMAPS ---
const unsigned char huevo_centro[] PROGMEM = {
0x03, 0xc0, 0x0f, 0xf0, 0x1f, 0xf8, 0x3f, 0xfc, 0x3f, 0xfc,
0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0x7f, 0xfe,
0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x07, 0xe0, 0x03, 0xc0
};
const unsigned char huevo_roto[] PROGMEM = {
0x03, 0xc0, 0x0f, 0xf0, 0x1f, 0xf8, 0x37, 0xbc, 0x33, 0x9c,
0x71, 0x0e, 0x79, 0x9e, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0x7f, 0xfe,
0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x07, 0xe0, 0x03, 0xc0
};
const unsigned char criatura[] PROGMEM = {
0x00, 0x00, 0x03, 0xc0, 0x07, 0xe0, 0x0f, 0xf0, 0x0f, 0xf0,
0x16, 0x68, 0x16, 0x68, 0x0f, 0xf0, 0x03, 0xc0, 0x07, 0xe0,
0x0f, 0xf0, 0x1f, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0,
0x09, 0x90, 0x09, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
enum Modos { M_JUEGO, M_RELOJ };
Modos modoActual = M_JUEGO;
bool eclosionado = false;
bool yaNacio = false;
unsigned long tiempoUltimoFrame = 0;
unsigned long tiempoHuevo = 0;
unsigned long tiempoInicioHora = 0;
unsigned long inicioPulsacionB = 0;
bool botonB_presionadoAntes = false;
bool necesitaRedibujarReloj = true;
bool accionEjecutadaB = false;
int criaturaY = 22;
bool subiendo = true;
void setup() {
Serial.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BOTON_B, INPUT_PULLUP);
if(!display.begin(SSD1306_SWITCHCAPVCC, i2c_Address)) {
for(;;);
}
display.clearDisplay();
display.display();
tiempoHuevo = millis();
tiempoUltimoFrame = millis();
}
void sonarBuzzer(int frec, int dur) {
tone(BUZZER_PIN, frec, dur);
delay(dur);
}
void revisarBotonB() {
// Ignorar lecturas durante el arranque
if (millis() < 500) return;
bool estadoB = digitalRead(BOTON_B);
// Si usas INPUT_PULLUP, LOW significa pulsado
if (estadoB == LOW) {
// Primera detección de pulsación
if (!botonB_presionadoAntes) {
botonB_presionadoAntes = true;
inicioPulsacionB = millis();
accionEjecutadaB = false;
}
// Comprobar pulsación larga
if (!accionEjecutadaB &&
millis() - inicioPulsacionB >= 1000 &&
modoActual == M_JUEGO) {
modoActual = M_RELOJ;
tiempoInicioHora = millis();
necesitaRedibujarReloj = true;
sonarBuzzer(1000, 100);
accionEjecutadaB = true; // Evita múltiples ejecuciones
}
} else {
// Botón liberado
botonB_presionadoAntes = false;
accionEjecutadaB = false;
}
}
void loop() {
revisarBotonB();
if (modoActual == M_RELOJ) {
if (necesitaRedibujarReloj) {
necesitaRedibujarReloj = false;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(35, 10);
display.print("RELOJ");
display.setCursor(35, 35);
display.print("14:35");
display.display();
}
if (millis() - tiempoInicioHora >= 3000) {
modoActual = M_JUEGO;
display.clearDisplay();
display.display();
tiempoUltimoFrame = millis();
}
}
if (modoActual == M_JUEGO) {
// ---- MODO JUEGO ----
if (!eclosionado) {
if (millis() - tiempoUltimoFrame >= 400) {
tiempoUltimoFrame = millis();
display.clearDisplay();
display.drawBitmap(56, 22, huevo_centro, 16, 20, SSD1306_WHITE);
display.display();
}
if (millis() - tiempoHuevo > 4000) {
eclosionado = true;
}
}
if (!yaNacio) {
display.clearDisplay();
display.drawBitmap(56, 22, huevo_roto, 16, 20, SSD1306_WHITE);
display.display();
sonarBuzzer(300, 100);
display.clearDisplay();
display.drawCircle(64, 32, 10, SSD1306_WHITE);
display.display();
sonarBuzzer(523, 150);
sonarBuzzer(659, 150);
sonarBuzzer(784, 300);
yaNacio = true;
tiempoUltimoFrame = millis();
}
else {
if (millis() - tiempoUltimoFrame >= 60) {
tiempoUltimoFrame = millis();
if (subiendo) {
criaturaY--;
if (criaturaY <= 15) subiendo = false;
} else {
criaturaY++;
if (criaturaY >= 22) subiendo = true;
}
display.clearDisplay();
display.drawBitmap(56, criaturaY, criatura, 16, 20, SSD1306_WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(30, 50);
display.print("ME LLAMO BABYTCHI");
display.display();
}
}
}
}