Created
December 24, 2015 07:43
-
-
Save uozias/cc011fefa5e6f758dc57 to your computer and use it in GitHub Desktop.
change led brightness with arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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