Está en la página 1de 3

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int TIEMPO_MAXIMO = 300; // 10 minutos en segundos


const int TIEMPO_INICIAL = 60;
const int CREDITOS_INICIALES = 0;
const int ESPERA_REINICIO = 1000;
const int PIN_BUZZER = 9;
const int RELE_JUGADOR_1 = 10;
const int RELE_JUGADOR_2 = 11;
const int BOTON_INICIO_1 = 2;
const int BOTON_PAUSA_1 = 3;
const int BOTON_INICIO_2 = 4;
const int BOTON_PAUSA_2 = 5;
const int BOTON_REINICIO = 6;
const int BOTON_PAUSA_J1 = 7;
const int BOTON_PAUSA_J2 = 8;
const int BOTON_AUMENTAR_TIEMPO = 12; // Nuevo bot�n para aumentar el tiempo

int tiempo_jugador_1 = TIEMPO_INICIAL;


int tiempo_jugador_2 = TIEMPO_INICIAL;
int creditos = CREDITOS_INICIALES;

int tiempoInicialAjustado = TIEMPO_INICIAL;

bool temporizadorActivoJugador1 = false;


bool temporizadorActivoJugador2 = false;
bool pausaJugador1 = false;
bool pausaJugador2 = false;

unsigned long tiempoUltimoReinicio = 0;

void setup() {
lcd.init();
lcd.backlight();
lcd.print("Temporizador");

pinMode(BOTON_INICIO_1, INPUT_PULLUP);
pinMode(BOTON_PAUSA_1, INPUT_PULLUP);
pinMode(BOTON_INICIO_2, INPUT_PULLUP);
pinMode(BOTON_PAUSA_2, INPUT_PULLUP);
pinMode(BOTON_REINICIO, INPUT_PULLUP);
pinMode(BOTON_PAUSA_J1, INPUT_PULLUP);
pinMode(BOTON_PAUSA_J2, INPUT_PULLUP);
pinMode(BOTON_AUMENTAR_TIEMPO, INPUT_PULLUP); // Configurar el nuevo bot�n

pinMode(PIN_BUZZER, OUTPUT);
pinMode(RELE_JUGADOR_1, OUTPUT);
pinMode(RELE_JUGADOR_2, OUTPUT);

digitalWrite(RELE_JUGADOR_1, LOW);
digitalWrite(RELE_JUGADOR_2, LOW);
}

void loop() {
manejarBotones();
actualizarLCD();
contarTiempo();
delay(1000);
}

void manejarBotones() {
if (digitalRead(BOTON_INICIO_1) == LOW && creditos > 0 && !
temporizadorActivoJugador1) {
temporizadorActivoJugador1 = true;
creditos--;
tiempo_jugador_1 = tiempoInicialAjustado;
digitalWrite(RELE_JUGADOR_1, HIGH);
reproducirTono();
}

if (digitalRead(BOTON_INICIO_2) == LOW && creditos > 0 && !


temporizadorActivoJugador2) {
temporizadorActivoJugador2 = true;
creditos--;
tiempo_jugador_2 = tiempoInicialAjustado;
digitalWrite(RELE_JUGADOR_2, HIGH);
reproducirTono();
}

if (digitalRead(BOTON_PAUSA_J1) == LOW && temporizadorActivoJugador1) {


pausaJugador1 = !pausaJugador1;
reproducirTono();
}

if (digitalRead(BOTON_PAUSA_J2) == LOW && temporizadorActivoJugador2) {


pausaJugador2 = !pausaJugador2;
reproducirTono();
}

if (digitalRead(BOTON_REINICIO) == LOW) {
if (millis() - tiempoUltimoReinicio > ESPERA_REINICIO) {
creditos++;
tiempoUltimoReinicio = millis();
if (!temporizadorActivoJugador1 && !temporizadorActivoJugador2) {
tiempo_jugador_1 = tiempoInicialAjustado;
tiempo_jugador_2 = tiempoInicialAjustado;
}
reproducirTono();
}
}

if (digitalRead(BOTON_PAUSA_1) == LOW && temporizadorActivoJugador1) {


pausaJugador1 = !pausaJugador1;
reproducirTono();
}

if (digitalRead(BOTON_PAUSA_2) == LOW && temporizadorActivoJugador2) {


pausaJugador2 = !pausaJugador2;
reproducirTono();
}

// Nuevo bloque para manejar el bot�n de aumento de tiempo


if (digitalRead(BOTON_AUMENTAR_TIEMPO) == LOW) {
aumentarTiempoInicial();
reproducirTono();
}
}

void actualizarLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" CREDITOS: ");
lcd.print(creditos);

lcd.setCursor(0, 1);
if (temporizadorActivoJugador1) {
lcd.print("t1: ");
if (pausaJugador1) {
lcd.print("Pausado");
} else {
lcd.print(tiempo_jugador_1);
}
}

lcd.setCursor(8, 1);
if (temporizadorActivoJugador2) {
lcd.print("t2: ");
if (pausaJugador2) {
lcd.print("Pausado");
} else {
lcd.print(tiempo_jugador_2);
}
}
}

void contarTiempo() {
if (temporizadorActivoJugador1 && !pausaJugador1 && tiempo_jugador_1 > 0) {

tiempo_jugador_1--;
} else if (temporizadorActivoJugador1 && tiempo_jugador_1 == 0) {
temporizadorActivoJugador1 = false;
digitalWrite(RELE_JUGADOR_1, LOW);
reproducirTono();
}

if (temporizadorActivoJugador2 && !pausaJugador2 && tiempo_jugador_2 > 0) {


tiempo_jugador_2--;
} else if (temporizadorActivoJugador2 && tiempo_jugador_2 == 0) {
temporizadorActivoJugador2 = false;
digitalWrite(RELE_JUGADOR_2, LOW);
reproducirTono();
}
}

void reproducirTono() {
tone(PIN_BUZZER, 1000, 1500);
}

void aumentarTiempoInicial() {
if (tiempoInicialAjustado < TIEMPO_MAXIMO) {
tiempoInicialAjustado += 60;
}
}

También podría gustarte