unsigned long duracionTotal = 10;//TIMEPO EN SEGUNDOS
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h>
#include <math.h>

#define PIN_LED     5
#define NUM_LEDS    100
#define Boton 15
#define BotonReset 4

Adafruit_NeoPixel strip(NUM_LEDS, PIN_LED, NEO_GRB + NEO_KHZ800);

byte estado_boton = 0;
byte estado_boton2 = 0;
unsigned long tiempo_boton;
bool estado = 0;
int brillo_blanco = 255;
bool estado_brillo = 0;
unsigned long tiempo_ruleta;
bool gira_ruleta = 0;
int color_random;
unsigned long tiempo_color_random;
unsigned long tiempoInicio;
float factor = brillo_blanco / 255.0;
int rojo_f = 255 * factor;
int verde_f = 110 * factor;
int azul_f = 100 * factor;
int brillo = 0;
int velocidad;
int r, g, b;
unsigned long ultimaActualizacion = 0;
const int pasoDelay = 28;
unsigned long tiempoReset = 0;
bool botonResetPresionado = false;

// EEPROM memory addresses
#define ADDR_TOTAL      0
#define ADDR_YELLOW     1
#define ADDR_BLUE       2
#define ADDR_ORANGE     3

// Limits
const int MAX_YELLOW = 125;
const int MAX_BLUE = 45;
const int MAX_ORANGE = 4;
const int MAX_TOTAL = 174;

void setup() {
  EEPROM.begin(8);  // Inicializa EEPROM (8 bytes como mínimo)
  randomSeed(analogRead(32));
  pinMode(Boton, INPUT_PULLUP);
  pinMode(BotonReset, INPUT_PULLUP);
  Serial.begin(115200);
  strip.begin();
  strip.show();
  mostrarConteo();
}

void loop() {
  manejarBotonReset();

  int total = EEPROM.read(ADDR_TOTAL);
  if (total >= MAX_TOTAL) {
    efectoDegrade();
    return;
  }

  if (estado_boton == 0) {
    if (digitalRead(Boton) == 0) {
      estado_boton = 1;
      estado_boton2 = 1;
      tiempo_boton = millis();
    }
  }

  if (estado_boton2 == 1) {
    if (millis() - tiempo_boton >= 1000) {
      estado_boton = 0;
      estado_boton2 = 0;
    }
  }

  if (estado_boton == 0) {
    efectoDegrade();
  }

  if (estado_boton == 1) {
    estado_boton = 2;
    seleccionarColorDisponible();
    tiempoInicio = millis();
    ruleta(duracionTotal * 1000, color_random);
    gira_ruleta = 1;
    tiempo_ruleta = millis();
    tiempo_color_random = millis();
    mostrarConteo();
  }
}

void mostrarConteo() {
  int total = EEPROM.read(ADDR_TOTAL);
  int y = EEPROM.read(ADDR_YELLOW);
  int b = EEPROM.read(ADDR_BLUE);
  int o = EEPROM.read(ADDR_ORANGE);

  Serial.println("------------------------------");
  Serial.print("Total jugadas: "); Serial.println(total);
  Serial.print("Amarillo: "); Serial.println(y);
  Serial.print("Azul: "); Serial.println(b);
  Serial.print("Naranja: "); Serial.println(o);
  Serial.println("------------------------------");
}

void manejarBotonReset() {
  if (digitalRead(BotonReset) == LOW) {
    if (!botonResetPresionado) {
      botonResetPresionado = true;
      tiempoReset = millis();
    } else if (millis() - tiempoReset >= 3000) {
      for (int i = 0; i < 5; i++) {
        rojo(); delay(300);
        apaga(); delay(300);
      }
      EEPROM.write(ADDR_TOTAL, 0);
      EEPROM.write(ADDR_YELLOW, 0);
      EEPROM.write(ADDR_BLUE, 0);
      EEPROM.write(ADDR_ORANGE, 0);
      EEPROM.commit();
      Serial.println("EEPROM reseteada. Juego reiniciado.");
      mostrarConteo();
      botonResetPresionado = false;
    }
  } else {
    botonResetPresionado = false;
  }
}

void seleccionarColorDisponible() {
  int y = EEPROM.read(ADDR_YELLOW);
  int b = EEPROM.read(ADDR_BLUE);
  int o = EEPROM.read(ADDR_ORANGE);
  int total = EEPROM.read(ADDR_TOTAL);

  int colores[174];
  int index = 0;

  for (int i = y; i < MAX_YELLOW; i++) colores[index++] = 1;
  for (int i = b; i < MAX_BLUE; i++) colores[index++] = 2;
  for (int i = o; i < MAX_ORANGE; i++) colores[index++] = 3;

  if (index == 0) {
    color_random = 1;
    return;
  }

  int elegido = random(0, index);
  color_random = colores[elegido];

  if (color_random == 1) {
    EEPROM.write(ADDR_YELLOW, y + 1);
  } else if (color_random == 2) {
    EEPROM.write(ADDR_BLUE, b + 1);
  } else if (color_random == 3) {
    EEPROM.write(ADDR_ORANGE, o + 1);
  }
  EEPROM.write(ADDR_TOTAL, total + 1);
  EEPROM.commit();

  Serial.print("Color elegido: ");
  if (color_random == 1) Serial.println("Amarillo");
  else if (color_random == 2) Serial.println("Azul");
  else if (color_random == 3) Serial.println("Naranja");
}

void ruleta(unsigned long duracion, int colorObjetivo) {
  unsigned long tiempoActual = 0;
  int delayInicial = 50;
  int delayFinal = 500;
  int secuencia[] = {1, 2, 3};
  int index = 0;

  while (tiempoActual < duracion - 1000) {
    int numero = secuencia[index];
    Serial.print("Número: ");
    Serial.println(numero);

    if (numero == 1) amarillo();
    if (numero == 2) azul();
    if (numero == 3) naranja();

    tiempoActual = millis() - tiempoInicio;
    float progreso = (float)tiempoActual / duracion;
    int delayActual = delayInicial + (delayFinal - delayInicial) * progreso;
    delay(delayActual);

    index = (index + 1) % 3;
  }

  byte contador_color = 0;
  delay(300);
  if (colorObjetivo == 1) {
    while (contador_color < 3) {
      apaga(); delay(300);
      amarillo(); delay(300);
      contador_color++;
    }
  } else if (colorObjetivo == 2) {
    while (contador_color < 3) {
      apaga(); delay(300);
      azul(); delay(300);
      contador_color++;
    }
  } else if (colorObjetivo == 3) {
    while (contador_color < 3) {
      apaga(); delay(300);
      naranja(); delay(300);
      contador_color++;
    }
  }

  delay(2000);
  apaga();
  estado_brillo = 1;
  brillo = 0;
  delay(5000); //-------------------------------------------------------------------
}

void efectoDegrade() {
  if (millis() - ultimaActualizacion >= pasoDelay) {
    ultimaActualizacion = millis();

    if (estado_brillo == 0) {
      brillo--;
      if (brillo < 0) {
        brillo = 0;
        estado_brillo = 1;
      }
    } else {
      brillo++;
      if (brillo > 255) {
        brillo = 255;
        estado_brillo = 0;
      }
    }

    float factor = brillo / 255.0;
    float gamma = 2.2;
    float correccion = pow(factor, gamma);

    int r = rojo_f * correccion;
    int g = verde_f * correccion;
    int b = azul_f * correccion;

    if (r <= 4 && g <= 4 && b <= 4) {
      r = 0; g = 0; b = 0;
    }

    for (int i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(r, g, b));
    }

    strip.show();
  }
}

void naranja() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(213, 0, 28));
  }
  strip.show();
}

void azul() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(0, 255, 60));
  }
  strip.show();
}

void amarillo() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(254, 0, 60));
  }
  strip.show();
}

void rojo() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));
  }
  strip.show();
}

void apaga() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0));
  }
  strip.show();
}
RESET
START