Publicidade:

sexta-feira, 18 de setembro de 2015

Arduino - Pai Nerd

Olá pessoal, ontem postei na página do facebook,  o vídeo abaixo, mostrando um projetinho que fiz pra comemorar o aniversário de um ano do meu filho.


Pai Nerd. Hoje meu filho, o Nicolas, faz 1 ano, e claro que tem Arduino pra comemorar :D

Pra quem estiver interessado, vou deixar o código-fonte, mas já aviso, que foi algo rápido, sem muito planejamento, e que com certeza dá pra ser melhorado.

Uma das melhorias que poderiam ser feitas, é substituir os delays, pelos millis. Tenho um artigo falando sobre como fazer isso. veja aqui.

Outro detalhe é que utilizo uma classe que criei pra gerar números aleatórios não repetidos. Quem quiser saber mais detalhes sobre essa classe, veja o artigo, clicando aqui.

valeu!


código-fonte:



#include "LedControl.h"
 
/**********************************************************************************
************************************CLASSE UNIQUE RANDOM***************************
**********************************************************************************/
   
class UniqueRandom{
  private:
    int _index;
    int _min;
    int _max;
    int _tam;
    int* _list;
    void _init(int min, int max) {
      _list = 0; 
      if (min < max) { _min = min; _max = max; } else { _min = max; _max = min; }
      _tam = _max - _min; 
      _index = 0;
    }    
  public:
    UniqueRandom(int max)           { _init(0,   max); randomize(); } //construtor com 1 parametro
    UniqueRandom(int min, int max)  { _init(min, max); randomize(); } //construtor com 2 parametros
       
    void randomize() {
      if (_list == 0) { _list = (int*) malloc(_tam * sizeof(int)); }  
      for (int i=0; i<_tam; i++) {   _list[i] = _min+i;  }   //preenche a lista do menor ao maior valor
         
      //embaralha a lista
      for (int i=0; i<_tam; i++) {  
        int r = random(0, _tam);     //sorteia uma posição qualquer
        int aux = _list[i];               
        _list[i] = _list[r];
        _list[r] = aux;
      }
    }
       
    int next() {                                  //retorna o proximo numero da lista
      int n = _list[_index++];
      if (_index >= _tam ) { _index = 0;} //após recuper o ultimo numero, recomeça na posicao 0
      return n;
    }
       
       
    ~UniqueRandom(){ free ( _list ); }  //destrutor
};
/**********************************************************************************
************************************FIM CLASSE UNIQUE RANDOM***********************
**********************************************************************************/
 
 
 
/*
 pin 4 is connected to the DataIn 
 pin 6 is connected to the CLK 
 pin 5 is connected to LOAD 
 */
LedControl   lc(4,6,5, 2); //2 max7219
UniqueRandom ur(64); //declaracao do objeto unique random
 
/* we always wait a bit between updates of the display */
unsigned long delaytime=500;
 
void setup() {
  int seed = 0;
     
  for (int i=0; i<10; i++) {
    seed += ( analogRead(A0) + analogRead(A1) + analogRead(A2) + analogRead(A3) + analogRead(A4) + analogRead(A5) ) ;
    delay(10);
  }
  randomSeed(seed);
     
  lc.shutdown(0,false);
  lc.setIntensity(0,1);
  lc.clearDisplay(0);
    
  lc.shutdown(1,false);
  lc.setIntensity(1,1);
  lc.clearDisplay(1);
   
  int lin = 0;
  lc.setColumn(1, lin++, B11111111);
  lc.setColumn(1, lin++, B11111111);
  lc.setColumn(1, lin++, B01110000);
  lc.setColumn(1, lin++, B00111000);
  lc.setColumn(1, lin++, B00011100);
  lc.setColumn(1, lin++, B00001110);
  lc.setColumn(1, lin++, B11111111);
  lc.setColumn(1, lin++, B11111111);
   
  delay(500);
}
 
byte leds_0[8] = {
  B00111000, 
  B00111000, 
  B00011000, 
  B00011000, 
  B00011000, 
  B00011000, 
  B01111110, 
  B01111110
};
 
byte leds_1[8] = {
  B11000011, 
  B11100011, 
  B11110011, 
  B11111011, 
  B11011111, 
  B11001111, 
  B11000111, 
  B11000011
};
 
void loop() { 
 
  int aleatorio = random(0, 9);
  //aleatorio = 8;
  Serial.println(aleatorio);
   
  if (aleatorio == 0) {
    //linhas de cima para baixo
    lc.clearDisplay(0);
    delay(delaytime/2);
    for(int row=0;row<8;row++) { lc.setRow(0,row, leds_0[row]);delay(50); }
    delay(delaytime*5);
  }
   
  if (aleatorio == 1) {
    //linhas de baixo para cima
    lc.clearDisplay(0);
    delay(delaytime/2);
    for(int row=7;row>=0;row--) { lc.setRow(0,row, leds_0[row]);delay(50); }
    delay(delaytime*5);
  }
   
  if (aleatorio == 2) {
    //direita para a esquerda
    lc.clearDisplay(0);
    delay(delaytime/2);
    for (int col=0;col<8;col++){
      for(int row=0;row<8;row++) {  lc.setLed(0, row, col,   (leds_0[row] & (1 << (7-col) )) != 0    ); }
      delay(50); 
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 3) {  
    //esquerda para a direita
    lc.clearDisplay(0);
    delay(delaytime/2);
    for (int col=7;col>=0;col--){
      for(int row=0;row<8;row++) {  lc.setLed(0, row, col,   (leds_0[row] & (1 << (7-col) )) != 0    ); }
      delay(50); 
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 4) {
    //direita para a esquerda
    lc.clearDisplay(0);
    delay(delaytime/2);
    for(int row=0;row<8;row++) {
      for (int col=0;col<8;col++){  
        lc.setLed(0, row, col,   (leds_0[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_0[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 5) {  
    //direita para a esquerda
    lc.clearDisplay(0);
    delay(delaytime/2);
    for(int row=0;row<8;row++) {
      for (int col=7;col>=0;col--){  
        lc.setLed(0, row, col,   (leds_0[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_0[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 6) {    
    //------------------------
    lc.clearDisplay(0);
    delay(delaytime/2);
    for (int col=0;col<8;col++){
      for(int row=0;row<8;row++) {  
        lc.setLed(0, row, col,   (leds_0[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_0[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
     
  }
   
  if (aleatorio == 7) {  
    //--------------------------
    lc.clearDisplay(0);
    delay(delaytime/2);
    for (int col=7;col>=0;col--){
      for(int row=0;row<8;row++) {  
        lc.setLed(0, row, col,   (leds_0[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_0[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 8) {  
    //--------------------------
    lc.clearDisplay(0);
    delay(delaytime/2);
     
    for(int i=0;i<64;i++) {  
      int r = ur.next();
      int l = r / 8;
      int c = r % 8;
      int val = (leds_0[l] & (1 << (7-c) )) != 0 ;
        
      if ( val ) { delay(100); }
      lc.setLed(0, l, c, val );
    }
     
    ur.randomize();
    delay(delaytime*5);
  }
 
 
 
 
 
 
 
 
 
 
 
 
  aleatorio = random(0, 9);
   
  if (aleatorio == 0) {
    //linhas de cima para baixo
    lc.clearDisplay(1);
    delay(delaytime/2);
    for(int row=0;row<8;row++) { lc.setRow(1,row, leds_1[row]);delay(50); }
    delay(delaytime*5);
  }
   
  if (aleatorio == 1) {
    //linhas de baixo para cima
    lc.clearDisplay(1);
    delay(delaytime/2);
    for(int row=7;row>=0;row--) { lc.setRow(1,row, leds_1[row]);delay(50); }
    delay(delaytime*5);
  }
   
  if (aleatorio == 2) {
    //direita para a esquerda
    lc.clearDisplay(1);
    delay(delaytime/2);
    for (int col=0;col<8;col++){
      for(int row=0;row<8;row++) {  lc.setLed(1, row, col,   (leds_1[row] & (1 << (7-col) )) != 0    ); }
      delay(50); 
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 3) {  
    //esquerda para a direita
    lc.clearDisplay(1);
    delay(delaytime/2);
    for (int col=7;col>=0;col--){
      for(int row=0;row<8;row++) {  lc.setLed(1, row, col,   (leds_1[row] & (1 << (7-col) )) != 0    ); }
      delay(50); 
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 4) {
    //direita para a esquerda
    lc.clearDisplay(1);
    delay(delaytime/2);
    for(int row=0;row<8;row++) {
      for (int col=0;col<8;col++){  
        lc.setLed(1, row, col,   (leds_1[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_1[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 5) {  
    //direita para a esquerda
    lc.clearDisplay(1);
    delay(delaytime/2);
    for(int row=0;row<8;row++) {
      for (int col=7;col>=0;col--){  
        lc.setLed(1, row, col,   (leds_1[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_1[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 6) {    
    //------------------------
    lc.clearDisplay(1);
    delay(delaytime/2);
    for (int col=0;col<8;col++){
      for(int row=0;row<8;row++) {  
        lc.setLed(1, row, col,   (leds_1[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_1[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
     
  }
   
  if (aleatorio == 7) {  
    //--------------------------
    lc.clearDisplay(1);
    delay(delaytime/2);
    for (int col=7;col>=0;col--){
      for(int row=0;row<8;row++) {  
        lc.setLed(1, row, col,   (leds_1[row] & (1 << (7-col) )) != 0    ); 
        if ( (leds_1[row] & (1 << (7-col) )) != 0 ) {
          delay(100);  
        }
      }
    }
    delay(delaytime*5);
  }
   
  if (aleatorio == 8) {  
    //--------------------------
    lc.clearDisplay(1);
    delay(delaytime/2);
     
    for(int i=0;i<64;i++) {  
      int r = ur.next();
      int l = r / 8;
      int c = r % 8;
      int val = (leds_1[l] & (1 << (7-c) )) != 0 ;
        
      if ( val ) { delay(100); }
      lc.setLed(1, l, c, val );
    }
     
    ur.randomize();
    delay(delaytime*5);
  }
 
 
 
 
   
 
}


Um comentário: