String colourPicker(int r, int g, int b) {
String r1, g1, b1;
r1 = String(r, HEX);
if(r < 16)
r1 = "0"+r1;
g1 = String(g, HEX);
if(g < 16)
g1 = "0"+g1;
b1 = String(b, HEX);
if(b < 16)
b1 = "0"+b1;
String rgbcolor = "#"+r1+g1+b1;
return rgbcolor;
}
int LightRGBLED(int r_pin, int g_pin, int b_pin, String rgb)
{
char *str = (char *)rgb.c_str()+1;
int r, g, b;
sscanf(str, "%02x%02x%02x", &r, &g, &b);
analogWrite(r_pin, r);
analogWrite(g_pin, g);
analogWrite(b_pin, b);
}
String RandomColour( ) {
String r1, g1, b1;
int r = random(0,255);
int g = random(0,255);
int b = random(0,255);
r1 = String(r, HEX);
if(r < 16)
r1 = "0"+r1;
g1 = String(g, HEX);
if(g < 16)
g1 = "0"+g1;
b1 = String(b, HEX);
if(b < 16)
b1 = "0"+b1;
String rgbcolor = "#"+r1+g1+b1;
return rgbcolor;
}
void setup()
{
pinMode(2, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
randomSeed(analogRead(3));
}
void loop()
{
if (digitalRead(2) == 1) {
LightRGBLED(9,10,11,colourPicker(0,0,0));
} else {
LightRGBLED(9,10,11,RandomColour());
}
}
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。