Lanciatore di Dadi casuale con matrice di Led 7×5





Lanciatore di Dadi Casuale

Per far funzionare tale matrice si ha bisogno di una libreria chiamata LedControl reperibile QUI


Schema collegamento con Integrato IC7221 e Board Arduino Uno o Duemilanove





Codice ( Sketch )Arduino, Lanciatore di Dadi Casuale

{codecitation class=”brush:cpp”}

/* We start by including the library */
#include “LedControl.h”

/*
* Now we CREATE a new LedControl.
* We use pins 12,11 and 10 on the Arduino for the SPI interface
* Pin 2 is connected to the DATA IN-pin of the first MAX7221
* Pin 4 is connected to the CLK-pin of the first MAX7221
* Pin 3 is connected to the LOAD(/CS)-pin of the first MAX7221     
* There will only be a single MAX7221 attached to the arduino
*/

int DATA  = 2;
int LATCH = 3;
int CLOCK = 4;
int inputButton = 10;
int outLed = 8;

LedControl lc1=LedControl(DATA,CLOCK,LATCH,1);

int faces[7][3]={{0,0,0},
{0,64,0},
{32,0,128},
{32,64,128},
{160,0,160},
{160,64,160},
{224,0,224}};

int loops[9][3]={{32,0,0},
{64,0,0},
{128,0,0},
{0,128,0},
{0,0,128},
{0,0,64},
{0,0,32},
{0,32,0}};

void setup() {
lc1.setScanLimit(1, 4);
lc1.setIntensity(0, 10);
lc1.shutdown(0,false);
Serial.begin(9600);    
pinMode(inputButton, INPUT);     // declare pushbutton as input
pinMode(outLed, OUTPUT);     // declare pushbutton as input  
digitalWrite(inputButton, HIGH);
testMatrix();
}

void loop() {
// righe 0..4
// colonne 1..7
static int dice=2;
static int button=1;
static int face=1;

button = digitalRead(inputButton);  // read if button is pressed
if (button == LOW)    // if pressed, show animation and value
{
if (dice==1)
dice=2;
else
{dice=1;
lc1.clearDisplay(0);}

launchDice(dice, face);
}

// loop through all 6 face values  (1..6)
face = 1 + (face % 6);

}

// Lancia il dado simulando un ritardo nella visualizzazione
void launchDice(int n, int f)
{
digitalWrite(outLed,LOW);

// Calculate start column
int c=1;
if(n==2)
c=5;

// Light looping
for(int i=0; i<3; i++)
{
for(int j=0; j<9; j++)
{
lc1.setColumn(0, c+0, loops[j][0]);  
lc1.setColumn(0, c+1, loops[j][1]);  
lc1.setColumn(0, c+2, loops[j][2]);      
delay(25);        
}
}

// Flash and show the result
showDice(n, f);
delay(800);
for(int j=1; j<4; j++)
{
showDice(n, 0);
delay(150);
showDice(n, f);
delay(150);    
}
digitalWrite(outLed,HIGH);  
}

// Show the dice “n” (1..2) with the “v” (1..6) face
void showDice(int n, int f)
{
// Calculate start column
int c=1;
if(n==2)
c=5;

lc1.setColumn(0, c+0, faces[f][0]);
lc1.setColumn(0, c+1, faces[f][1]);  
lc1.setColumn(0, c+2, faces[f][2]);  
}

// show a display test
void testMatrix()
{
for(int r=0; r<5; r++)
{lc1.setRow(0, r, 127);
lc1.setIntensity(0, r*2);
delay(120);}

for(int c=0;c<7;c++)
{lc1.setColumn(0, c, 0);
lc1.setIntensity(0, c*2);
delay(120);}

lc1.clearDisplay(0);
lc1.setIntensity(0, 10);    
delay(800);
}

{/codecitation}

 

Video Dimostrativo

 

 

Share This