我在许多#define值中都有RGB颜色(HEX)。我想循环每一种颜色 - 我的意思是:第一种颜色显示 - 延迟(1000) - 第二种颜色 - 延迟(1000) - 第三种颜色等。 有这么多#define可以做到吗?以及如何做到这一点......
#define AQUA "#00ffff"
#define AZURE "#f0ffff"
#define BEIGE "#f5f5dc"
#define BLACK "#000000"
#define BLUE "#0000ff"
#define BROWN "#a52a2a"
#define CYAN "#00ffff"
#define DARKBLUE "#00008b"
#define DARKCYAN "#008b8b"
#define DARKGREY "#a9a9a9"
#define DARKGREEN "#006400"
#define DARKKHAKI "#bdb76b"
#define DARKMAGENTA "#8b008b"
#define DARKOLIVEGREEN "#556b2f"
#define DARKORANGE "#ff8c00"
#define DARKORCHID "#9932cc"
#define DARKRED "#8b0000"
#define DARKSALMON "#e9967a"
#define DARKVIOLET "#9400d3"
#define FUCHSIA "#ff00ff"
#define GOLD "#ffd700"
#define GREEN "#008000"
#define KHAKI "#f0e68c"
#define LIGHTBLUE "#add8e6"
#define LIGHTCYAN "#e0ffff"
#define LIGHTGREEN "#90ee90"
#define LIGHTGREY "#d3d3d3"
#define LIGHTPINK "#ffb6c1"
#define LIGHTYELLOW "#ffffe0"
#define LIME "#00ff00"
#define MAGENTA "#ff00ff"
#define MAROON "#800000"
#define NAVY "#000080"
#define OLIVE "#808000"
#define ORANGE "#ffa500"
#define PINK "#ffc0cb"
#define PURPLE "#800080"
#define VIOLET "#800080"
#define RED "#ff0000"
#define SILVER "#c0c0c0"
#define WHITE "#ffffff"
#define YELLOW "#ffff00"
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup() {
Serial.begin(9600);
}
void loop() {
String hexstring = AQUA;
long number = (long) strtol( &hexstring[1], NULL, 16);
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;
setColor(r,g,b);
Serial.print("red is ");
Serial.println(r);
Serial.print("green is ");
Serial.println(g);
Serial.print("blue is ");
Serial.println(b);
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
答案 0 :(得分:0)
使用任何数量的定义无法做到这一点。创建一个包含所需值的数组,然后迭代它。
char *colorarray[] = {AQUA, AZURE, ...};
此外,您应该考虑将颜色存储为十六进制值而不是字符串,因为它们占用的空间更少。