Skip to content

Instantly share code, notes, and snippets.

@uozias
Created December 24, 2015 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uozias/cc011fefa5e6f758dc57 to your computer and use it in GitHub Desktop.
Save uozias/cc011fefa5e6f758dc57 to your computer and use it in GitHub Desktop.
change led brightness with arduino
int brightness = 0; // LEDの明るさを入れる
int fadeAmount = 5; // 明るさの変更スピードを設定
void setup() {
Serial.begin(9600); // シリアルポートを9600bpsで開く
pinMode(9, OUTPUT); //9番のピンを使う
}
void loop() {
Serial.println(brightness);
analogWrite(9, brightness); //明るさを設定
brightness = brightness + fadeAmount; // 次の時点での明るさを計算
if (brightness == 0 || brightness == 255) { // 一番明るく、もしくは一番暗くなったら、明るさの変化方向を逆にする
fadeAmount = -fadeAmount ;
}
delay(30); // 30ミリ秒まつ
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment