Código do Cubo
Autor: Ardeoino | Publicado: 2012-06-11T05:36:00-07:00


#include &lt;avr/pgmspace.h&gt; &nbsp; &nbsp; &nbsp; &nbsp;// allows use of PROGMEM to store patterns in flash

#define CUBESIZE 7
#define PLANESIZE CUBESIZE*CUBESIZE
#define PLANETIME 3333 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// time each plane is displayed in us -&gt; 100 Hz refresh
#define TIMECONST 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// multiplies DisplayTime to get ms - why not =100?

prog_uchar PROGMEM PatternTable[] = {
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,
B1111111, B0000000, B1000001, B1100011, B1010101, B1001001, B0000000,10,


&nbsp;
};

/*
** Defining pins in array makes it easier to rearrange how cube is wired
&nbsp;** Adjust numbers here until LEDs flash in order - L to R, T to B
&nbsp;** Note that analog inputs 0-5 are also digital outputs 14-19!
&nbsp;** Pin DigitalOut0 (serial RX) and AnalogIn5 are left open for future apps
&nbsp;*/


//int LEDPin[] = {16, 3, 1, 15, 4, 6, 14, 5, 7};
int LEDPin[] = {0,1,2,3,4,5,6};

//int PlanePin[] = {19, 18, 17};
int PlanePin[] = {12,13,14,15,16,17,18};

int decodificador[]={8,9,10};

// initialization


void downClock(int valor){

&nbsp; int binario[3], j=0, i=0, resto=0;

&nbsp; for(j=2; j&gt;=0;j--){
&nbsp; &nbsp; resto=valor%2; 
&nbsp; &nbsp; valor=valor/2;
&nbsp; &nbsp; printf("%d\n",resto);
&nbsp; &nbsp; binario[j]=resto;
&nbsp; }

&nbsp; for(i=0; i&lt;3;i++){ &nbsp; 
&nbsp; &nbsp; digitalWrite(decodificador[i],binario[i]);
&nbsp; } 

}

void upClock(){
&nbsp; digitalWrite(16,HIGH);
&nbsp; digitalWrite(17,HIGH);
&nbsp; digitalWrite(18,HIGH);
}


void limpaFlip(){

&nbsp; int i=0;

&nbsp; for(int j=1;j&lt;=7;j++){
&nbsp; &nbsp; digitalWrite(PlanePin[j],LOW);

&nbsp; &nbsp; for(i=0; i&lt;7;i++){
&nbsp; &nbsp; &nbsp; digitalWrite(LEDPin[i],LOW);

&nbsp; &nbsp; }
&nbsp; &nbsp; upClock();
&nbsp; &nbsp; downClock(j);
&nbsp; &nbsp;
&nbsp; &nbsp; 
&nbsp; }
&nbsp;


} 

void setup()
{
&nbsp; int pin; &nbsp; &nbsp; &nbsp;// loop counter
&nbsp; // set up LED pins as output (active HIGH)
&nbsp; for (pin=0; pin&lt;PLANESIZE; pin++) {
&nbsp; &nbsp; pinMode( LEDPin[pin], OUTPUT );

&nbsp; }
&nbsp; // set up plane pins as outputs (active LOW)
&nbsp; for (pin=0; pin&lt;CUBESIZE; pin++) {
&nbsp; &nbsp; pinMode( PlanePin[pin], OUTPUT );
&nbsp; }
}

// display pattern in table until DisplayTime is zero (then repeat)
void loop()
{
&nbsp; // declare variables
&nbsp; byte PatternBuf[ PLANESIZE ]; &nbsp; &nbsp; &nbsp;// saves current pattern from PatternTable
&nbsp; int PatternIdx;
&nbsp; byte DisplayTime; &nbsp; &nbsp; &nbsp; &nbsp;// time*100ms to display pattern
&nbsp; unsigned long EndTime;
&nbsp; int plane; &nbsp; &nbsp; &nbsp;// loop counter for cube refresh
&nbsp; int patbufidx; &nbsp; // indexes which byte from pattern buffer
&nbsp; int ledrow; &nbsp; &nbsp;// counts LEDs in refresh loop
&nbsp; int ledcol; &nbsp; &nbsp;// counts LEDs in refresh loop
&nbsp; int ledpin; &nbsp; &nbsp;// counts LEDs in refresh loop

&nbsp; // Initialize PatternIdx to beginning of pattern table
&nbsp; PatternIdx = 0;
&nbsp; // loop over entries in pattern table - while DisplayTime&gt;0
&nbsp; //limpaFlip();



&nbsp; do {


&nbsp; &nbsp; memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );

&nbsp; &nbsp; PatternIdx += PLANESIZE;


&nbsp; &nbsp; DisplayTime = pgm_read_byte_near( PatternTable + PatternIdx++ ); &nbsp; 

&nbsp; &nbsp; EndTime = millis() + ((unsigned long) DisplayTime) * TIMECONST;


&nbsp; &nbsp; // loop while DisplayTime&gt;0 and current time &lt; EndTime
&nbsp; &nbsp; while ( millis() &lt; EndTime ) {

&nbsp; &nbsp; &nbsp; patbufidx = 0; &nbsp; &nbsp;// reset index counter to beginning of buffer
&nbsp; &nbsp; &nbsp; // loop over planes

&nbsp; &nbsp; &nbsp; for (plane=1; plane&lt;=CUBESIZE; plane++) {

&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite( PlanePin[plane], HIGH );
&nbsp; &nbsp; &nbsp; &nbsp; // load current plane pattern data into ports
&nbsp; &nbsp; &nbsp; &nbsp; ledpin = 0;

&nbsp; &nbsp; &nbsp; &nbsp; for (ledrow=0; ledrow&lt;CUBESIZE; ledrow++) {



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (ledcol=0; ledcol&lt;CUBESIZE; ledcol++) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ledpin=+1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite( LEDPin[ledpin], PatternBuf[patbufidx] &amp; (1 &lt;&lt; ledcol) );
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downClock(ledrow);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upClock(); &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ledpin=0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patbufidx++;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // turn current plane on
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite( PlanePin[plane], LOW);
&nbsp; &nbsp; &nbsp; &nbsp; // delay PLANETIME us
&nbsp; &nbsp; &nbsp; &nbsp; delay(10000);
&nbsp; &nbsp; &nbsp; &nbsp; delayMicroseconds( PLANETIME );

&nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;// for plane
&nbsp; &nbsp; } &nbsp; &nbsp;// while &lt;EndTime
&nbsp; }

&nbsp; while (DisplayTime &gt; 0);
&nbsp;
//limpaFlip();
}