segunda-feira, 11 de junho de 2012

Código do Cubo


#include <avr/pgmspace.h>        // allows use of PROGMEM to store patterns in flash

#define CUBESIZE 7
#define PLANESIZE CUBESIZE*CUBESIZE
#define PLANETIME 3333          // time each plane is displayed in us -> 100 Hz refresh
#define TIMECONST 20          // 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,


 
};

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


//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){

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

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

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

}

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


void limpaFlip(){

  int i=0;

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

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

    }
    upClock();
    downClock(j);
   
   
  }
 


}

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

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

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

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



  do {


    memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );

    PatternIdx += PLANESIZE;


    DisplayTime = pgm_read_byte_near( PatternTable + PatternIdx++ );  

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


    // loop while DisplayTime>0 and current time < EndTime
    while ( millis() < EndTime ) {

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

      for (plane=1; plane<=CUBESIZE; plane++) {

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

        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {



          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            ledpin=+1;

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

      }    // for plane
    }    // while <EndTime
  }

  while (DisplayTime > 0);
 
//limpaFlip();
}


Nenhum comentário:

Postar um comentário