LED Cube

※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

LED Cubeを作る

まずは3x3x3。


横にあるmega328pは激安Arduinoの作り方2の方法で内蔵RC8MHzで動くArduino化されています。
ブレッドボードに挿したFT232RLから電源/GND/TX/RX/RESETをつなぐとArduinoIDEからスケッチを書き込めます。



電池をつなげば単体で動作OK。
パスコンもリセットのプルアップも無いいい加減なものですが。


LED Cubeのスケッチ


void setup()
{
  for(int n=2; n<=13; n++){
    pinMode(n, OUTPUT);
    digitalWrite(n, HIGH);
  }
}
 
int layers[3];
char pattern = 0;
 
#define XY_PIN0 2
#define XY_ON(l)     digitalWrite(XY_PIN0+l,    HIGH);
#define XY_OFF(l)    digitalWrite(XY_PIN0+l,    LOW );
 
#define LAYER_PIN0 11
#define LAYER_ON(l)  digitalWrite(LAYER_PIN0+l, LOW );
#define LAYER_OFF(l) digitalWrite(LAYER_PIN0+l, HIGH);
 
#define nop() __asm("nop");
 
void loop()
{
  static int loop_count = 0;
  loop_count++;
 
  pattern = millis()/1000 / 20 % 4 +1;
 
  switch(pattern){
//  case 0: pattern_0(); break;
  case 1: pattern_1(); break;
  case 2: pattern_2(); break;
  case 3: pattern_3(); break;
  case 4: pattern_4(); break;
/*
  case 5: pattern_5(); break;
  case 6: pattern_6(); break;
  case 7: pattern_7(); break;
*/
  }
 
  led_dynamic_drive();
}
 
// ---------------------------------------------------
// OFF
void pattern_0(void)
{
  layers[0] = 0x000;
  layers[1] = 0x000;
  layers[2] = 0x000;
}
 
// ---------------------------------------------------
// 
void pattern_1(void)
{
  static int wait = 0;
  if(wait++<200){ return; }
  wait = 0;
 
  static int counter = 0;
  counter = (counter+1)%27;
  layers[0] = 0x000;
  layers[1] = 0x000;
  layers[2] = 0x000;
  layers[counter/9] = 1<<(counter%9);
}
 
// ---------------------------------------------------
// 2
void pattern_2(void)
{
  static int wait = 0;
  if(wait++<200){ return; }
  wait = 0;
 
  char tbl[] = {0,1,2,5,8,7,6,3};
  static int counter = 0;
  counter = (counter+1)%(4*8);
  layers[0] = 0x000;
  layers[1] = 0x000;
  layers[2] = 0x000;
  int l = counter/8;
  if(l==3){ l = 1; }
  layers[l] = 1<<(tbl[((counter)%8)]);
}
 
 
// ---------------------------------------------------
// 3
void pattern_3(void)
{
  static int wait = 0;
  if(wait++<200){ return; }
  wait = 0;
 
  layers[0] = 0x000;
  layers[1] = 0x000;
  layers[2] = 0x000;
  for(int i=0; i<3; i++){
    int n = random(0, 27);
    int l = n/9;
    layers[l] = 1<<(n%9);
  }
}
 
// ---------------------------------------------------
// 4
void pattern_4(void)
{
  static int wait = 200;
  if(0 < wait--){ return; }
  static double xt_a = 0;
  static double xt_b = 0;
  if(xt_a==0){
    xt_a = 0.1+analogRead(0)/1500;
    xt_b = 0.2+analogRead(0)/1500;
  }
  xt_a = yuragi(xt_a);
  xt_b = yuragi(xt_b);
  wait = 30 + (int)(200*xt_a) + (int)(50*xt_b);
 
  layers[0] = 0x000;
  layers[1] = 0x000;
  layers[2] = 0x000;
 
  static char pos[] = {0,0,0};
  static char state = 0;
  static char dir = 0; // 012=xyz 
  static char vel = 0;
 
  switch(state){
  case 0:
    dir += random(1,3);
    dir = dir%3;
    if(pos[dir] == 0){
      vel = +1;
    }else{
      vel = -1;
    } 
    state = 1;
    break;
  case 1:
    state = 0;
    break;
  }
  pos[dir] += vel;
 
  layers[pos[2]] = 1<<(pos[0]+pos[1]*3);
}
 
// ---------------------------------------------------
// 5
void pattern_5(void)
{
  static int wait = 200;
  if(0 < wait--){ return; }
  static double xt = 0;
  if(xt==0){
    xt = 0.1+analogRead(0)/1500;
  }
  xt = yuragi(xt);
  wait = 20 + (int)(300*xt);
 
  layers[0] = 0x000;
  layers[1] = 0x000;
  layers[2] = 0x000;
 
  static char pos[] = {0,0,0};
  static char state = 0;
  static char dir = 0; // 012=xyz 
  static char vel = 0;
 
  switch(state){
  case 0:
    dir += random(1,3);
    dir = dir%3;
    if(pos[dir] == 0){
      vel = +1;
    }else{
      vel = -1;
    } 
    state = 1;
    break;
  case 1:
    state = 0;
    break;
  }
  pos[dir] += vel;
 
  layers[pos[2]] = 1<<(pos[0]+pos[1]*3);
}
 
 
 
// 1/fゆらぎ 間欠カオス
double yuragi(double xt)
{
  if(xt<0.5){
    return (xt + 2*   xt *   xt );
  }else{
    return (xt - 2*(1-xt)*(1-xt));
  }
}
 
void led_dynamic_drive()
{
  for(int l=0; l<3; l++){
    LAYER_ON(l);    
    for(int n=0; n<9; n++){
      if( layers[l] & (1<<n) ){
        XY_ON(n);
      }else{
        XY_OFF(n);
      }
      delayMicroseconds(1);
      XY_OFF(n);
      delayMicroseconds(10);
    }
    LAYER_OFF(l);
  }
}
 
 
 
最終更新:2009年11月02日 00:20
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。