1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ATTiny13Aでクリスマスイルミネーションを作ってみた

1
Last updated at Posted at 2020-12-15

#クリスマスですね
 クリスマスツリーを買ったときに付いてきた乾電池で動く中華製のLEDストリングは電池ボックスに点滅用の回路が入っていたのですが、点滅速度が速すぎて家族に不評だったのと、乾電池駆動が個人的に嫌いだったので、USB給電に改造するついでにATTiny13Aを使ってじわっと点滅できるようにしました。
 パターンがいつも同じだと退屈なので空いているピンの浮動電圧を使って点滅周期が一定時間ごとにランダムに変わるようにしました。たまたま別のLEDストリングも余っていたので2チャンネルでそれぞれ独立したタイミングで点滅するようにしました。
コンパイルにはbitDuino13を使用し、9.6MHzで動作するように設計しました。

  • じわっと点滅LEDストリング用回路
  • 独立2ch
  • ランダムな周期

#結線
回路図は付けませんが5,6番ピン(PB0, PB1)からそれぞれ1KΩを介してトランジスタC1815のベースに接続して、VCCとコレクタの間に92Ωの抵抗を介してLEDストリングをつなぎました。明るさが欲しい人はもっと低い抵抗値でもいいかもしれません。LEDの数と相談して決めてください。USBケーブルから電源をとれるようにしているので電源電圧は5Vを想定しています。
点滅周期をランダムに変える乱数取得のために2番ピン(A3)の浮動電圧を取得して適当な数で割った余りを計算しています。

Xmas2chLamp.ino
/* bitDuino13 9.6MHz  */
int led[2];           // the PWM pins the LED is attached to
/*int analogPin=3;      // the sensor pin to generate random number*/
analog_pin_t analogPin=A3; // analog_pin_t is needed to compile using MicroCore
int brightness[2];    // how bright the LED is
int fadeAmount[2];    // how many points to fade the LED by
int threshold[2];
unsigned int j=0;

// the setup routine runs once when you press reset:
void setup() {
  int i;
  for(i=0; i<2; i++){ // declear pins 0, 1 as output
    led[i]=i;
    pinMode(led[i], OUTPUT);
    brightness[i]=0;
  }
  pinMode(analogPin, INPUT); // declear analog pin
  fadeAmount[0]=5;
  fadeAmount[1]=10;
  threshold[0]=0;
  threshold[1]=0;
}

// the loop routine runs over and over again forever:
void loop() {
  int i;
  int k=j % 1000;
  int tmp;
  j++;
  if(k < 3){ /* 約30秒ごとに点滅周期を変更 */
    tmp=analogRead(analogPin) % 20;
    fadeAmount[k % 2]=(tmp*tmp/4+1)*constrain(fadeAmount[k % 2],-1,1);
    if(tmp > 15){ /* 点滅周期が早いのも楽しいがずっと続くと不快なので周期の変更を速めます */
      j+=600;
    }
  }else if(k < 5){ /* じわっと点滅ばかりでは退屈なのでときどきはぱっと消すための設定 */
    tmp=analogRead(analogPin) % 160;
    threshold[k % 2]=tmp - 60; /* thresholdがマイナスの時にはじわっと点滅 */
  }

  for(i=0; i<2; i++){
    if(brightness[i] > threshold[i]){
      analogWrite(led[i], brightness[i]);
    }else{
      digitalWrite(led[i], LOW);
    }

    brightness[i] = brightness[i] + fadeAmount[i];

    if (brightness[i] <= 0 || brightness[i] >= 255){
      fadeAmount[i] = -fadeAmount[i];
    }
    brightness[i]=constrain(brightness[i], 0, 255);
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?