#include <SPI.h>
#include <LiquidCrystal_I2C.h>
// Pines asignados para el protocolo SPI
const uint8_t CS_PIN = 10;
const uint8_t MOSI_PIN = 11;
const uint8_t MISO_PIN = 13;
const uint8_t SCK_PIN = 12;
const int pin_SDA = 8;
const int pin_SCL = 9;
// Registros de la matriz LED MAX7219
#define MAX7219_REG_NOOP 0x00
#define MAX7219_REG_DIGIT0 0x01
#define MAX7219_REG_DIGIT1 0x02
#define MAX7219_REG_DIGIT2 0x03
#define MAX7219_REG_DIGIT3 0x04
#define MAX7219_REG_DIGIT4 0x05
#define MAX7219_REG_DIGIT5 0x06
#define MAX7219_REG_DIGIT6 0x07
#define MAX7219_REG_DIGIT7 0x08
#define MAX7219_REG_DECODEMODE 0x09
#define MAX7219_REG_INTENSITY 0x0A
#define MAX7219_REG_SCANLIMIT 0x0B
#define MAX7219_REG_SHUTDOWN 0x0C
#define MAX7219_REG_DISPLAYTEST 0x0F
LiquidCrystal_I2C miLCD(0x27, 16, 2);
int v;
void setup() {
// Configurar pines SPI
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);
// Configurar el pin CS
pinMode(CS_PIN, OUTPUT); // Pin CS configurado como salida
digitalWrite(CS_PIN, HIGH); // Desactivar el chip select
// Inicializar el MAX7219
// Utilizará las filas o dígitos del 0 al 7
sendToMax7219(MAX7219_REG_SCANLIMIT, 0x07);
// Con decodificación BCD
sendToMax7219(MAX7219_REG_DECODEMODE, 0x01);
// modo operacional
sendToMax7219(MAX7219_REG_SHUTDOWN, 0x01);
// Máxima intensidad (0x00 a 0x0F)
sendToMax7219(MAX7219_REG_INTENSITY, 0x0F);
miLCD.init(); // Inicializamos el módulo LCD
miLCD.home(); // Limpiamos la pantalla y colocamos
// el cursor en (0, 0)
miLCD.print("VUELTAS");
miLCD.setCursor(5, 1); // Ubicamos el cursor en la columna 5, fila 1
miLCD.print(v);
}
void loop() {
// Enviar las filas o digitos
// luego de enviar los leds de cada fila que se encenderan
// se podrá visualizar la formación del numero 3 en la matriz 8x8 LED
for (int i = 0; i < 7; i++) { // Recorremos las 7 filas
if(i == 0 || i == 6){
while(i == 0){
int h = 0;
if(h == 0){
sendToMax7219(i + 1, 12 ); // número 7 en binario a filas 1
sendToMax7219(i + 1, 0); //borra todo
delay(10);
h++;
Serial.println("primera prueba");
}else{
if(h==1){
sendToMax7219(i + 1, 5 ); // número 7 en binario a filas 1
sendToMax7219(i + 1, 0);
delay(10);
h++;
}else{
sendToMax7219(i + 1, 3 ); // número 7 en binario a filas 1
sendToMax7219(i + 1, 0);
delay(10);
h++;
}
i++;
}
// 1100 = 12 , 0110 = 5 , 0011 = 3
}
while(i == 6){
int h = 0;
if(h == 0){
sendToMax7219(i + 1, 3 ); // número 7 en binario a filas 1
sendToMax7219(i + 1, 0); //borra todo
delay(10);
h++;
}else{
if(h==1){
sendToMax7219(i + 1, 5 ); // número 7 en binario a filas 1
sendToMax7219(i + 1, 0);
delay(10);
h++;
}else{
sendToMax7219(i + 1, 12 ); // número 7 en binario a filas 1
sendToMax7219(i + 1, 0);
delay(10);
h++;
}
i++;
}
}
}
else if(i == 1 || i == 2 || i == 3 || i == 4 || i == 5){
if(i == 1 ){
sendToMax7219(1 , 1);
sendToMax7219(i + 1, 1 );
sendToMax7219(1 , 0);
sendToMax7219(i + 1, 0); // borra
delay(10);
}else{
while(i<6){
sendToMax7219(i , 1);
sendToMax7219(i + 1, 1 ); // número 8 en binario a filas 2,3,5 y 6
sendToMax7219(i , 0);
sendToMax7219(i + 1, 0); // borra
delay(10);
i++;
}
}
}else{
for(int v= 13 ; v < 22; v++){
if(v == 13 || v == 16 || v == 17 ){
//para v 13
if(v == 13){
sendToMax7219(6 , 4);
sendToMax7219(7 , 8);
sendToMax7219(6,0);
sendToMax7219(7,0);
}else{
if(v == 16){
sendToMax7219(3 , 2);
sendToMax7219(4 , 4);
sendToMax7219(3,0);
sendToMax7219(4,0);
}else{
sendToMax7219(2 , 1);
sendToMax7219(3 , 2);
sendToMax7219(2,0);
sendToMax7219(3,0);
}
}
}
if(v == 18 ){
i = 1;
sendToMax7219(1 , 1);
sendToMax7219(i + 1, 1 );
sendToMax7219(1 , 0);
sendToMax7219(i + 1, 0); // borra
delay(10);
}
}
}
delay(200);
}
}
// Función para enviar datos al MAX7219 por protocolo PSI
void sendToMax7219(uint8_t registro, uint8_t data) {
digitalWrite(CS_PIN, LOW); // Seleccionar el dispositivo (CS bajo)
SPI.transfer(registro); // Enviar registro a comunicar
SPI.transfer(data); // Enviar dato a escribir en registro
digitalWrite(CS_PIN, HIGH); // Deseleccionar el dispositivo (CS alto)
}