Last active
May 28, 2019 01:10
-
-
Save ghostoy/933791b04f8a5ed145157b93b7aabf43 to your computer and use it in GitHub Desktop.
Arduino控制全彩LED
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
// 如果是共阳的LED,取消下一行的注释 | |
// #define COMMON_ANODE | |
const int redPin = A0; // 红色引脚 | |
const int greenPin = A1; // 绿色引脚 | |
const int bluePin = A2; // 蓝色引脚 | |
void setup() { | |
} | |
void loop() { | |
setColor(0xff, 0, 0); // 红色 | |
delay(1000); | |
setColor(0, 0xff, 0); // 绿色 | |
delay(1000); | |
setColor(0, 0, 0xff); // 蓝色 | |
delay(1000); | |
setColor(0xff, 0xff, 0); // 黄色 | |
delay(1000); | |
setColor(0, 0xff, 0xff); // 青色 | |
delay(1000); | |
setColor(0xff, 0, 0xff); // 洋红 | |
delay(1000); | |
setColor(0xff, 0xff, 0xff); // 白色 | |
delay(1000); | |
} | |
// 设置LED的颜色 | |
void setColor(int red, int green, int blue) | |
{ | |
#ifdef COMMON_ANODE | |
red = 255 - red; | |
green = 255 - green; | |
blue = 255 - blue; | |
#endif | |
// 设置RGB针脚的电压,有效值0-255,对应0-5V电压 | |
analogWrite(redPin, red); | |
analogWrite(greenPin, green); | |
analogWrite(bluePin, blue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment