// Definimos los pines del 74hc595
const int pinLed = 2; // Pin led
const int latchPin = 3; // STCP
const int clockPin = 5; // SHCP
const int dataPin = 4; // DS
bool estadoLed;
uint8_t segundos, minutos;
unsigned long tiempoMedioSegundo;
unsigned long intervaloMedioSegundo = 500;
unsigned long tiempoSegundos;
unsigned long intervaloSegundos = 1000;
const byte salida[] = { // Cramos binarios de los dígitos
0b00000011, // 0
0b10011111, // 1
0b00100101, // 2
0b00001101, // 3
0b10011001, // 4
0b01001001, // 5
0b01000001, // 6
0b00011111, // 7
0b00000001, // 8
0b00001001, // 9
};
void mostrar() {
uint8_t unidadSegundo = segundos % 10;
uint8_t decenaSegundo = (segundos / 10) % 10;
uint8_t unidadMinuto = minutos % 10;
uint8_t decenaMinuto = (minutos / 10) % 10;
digitalWrite(latchPin, LOW); // Aseguramos que latch está en LOW
shiftOut(dataPin, clockPin, MSBFIRST, salida[unidadSegundo]); // Enviamos el byte de datos
shiftOut(dataPin, clockPin, MSBFIRST, salida[decenaSegundo]); // Enviamos el byte de datos
shiftOut(dataPin, clockPin, MSBFIRST, salida[unidadMinuto]); // Enviamos el byte de datos
shiftOut(dataPin, clockPin, MSBFIRST, salida[decenaMinuto]); // Enviamos el byte de datos
digitalWrite(latchPin, HIGH); // Aseguramos que latch está en HIGH para que el 74hc595 actualice las salidas
}
void compruebaTiempo() {
segundos++;
if (segundos == 60) {
segundos = 0;
minutos++;
}
if (minutos == 60) {
minutos = 0;
}
}
void setup() {
Serial.begin(115200);
// Configuramos los pines como salidas
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(pinLed, OUTPUT);
}
void loop() {
unsigned long tiempoActual = millis();
if (tiempoActual - tiempoMedioSegundo >= intervaloMedioSegundo) {
tiempoMedioSegundo = tiempoActual;
estadoLed = !estadoLed;
digitalWrite(pinLed, !estadoLed);
}
if (tiempoActual - tiempoSegundos >= intervaloSegundos) {
tiempoSegundos = millis();
compruebaTiempo();
mostrar();
}
}